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

Jenkins declarative pipeline - How to assign an expression to a shell variable

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")
}

有人可以帮忙吗?

您需要 return 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()