Jenkins 声明式管道 - 如何将表达式分配给 shell 变量

时间:2021-06-09 07:46:22

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline

stage ('Build image') {
    steps {
    withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'user', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
            sh "tempPass=\$(aws ecr get-login-password --region us-west-2)"
            echo 'Hello------------------'
            echo "${tempPass}"
            echo 'Hello AFTER------------------'
        }
    }
}

Exception : groovy.lang.MissingPropertyException: No such property: pass for class: groovy.lang.Binding

我也试过 echo "$tempPass" 但没有用。

我也试过这个,但变量值为空。

script {
        tempPass=sh(script: "aws ecr get-login-password --region us-west-2")
}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您需要从 sh 步骤返回 returnStatusreturnStdout https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

例如

latest_tag = sh(script: "aws ecr get-login-password --region us-west-2", returnStdout: true).trim()
相关问题