Search code examples
jenkinsjenkins-pipelinejenkins-declarative-pipeline

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

I have tried echo "$tempPass" as well but does not work.

I tried this as well but variable value is coming in as null.

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

Can anyone help here?


Solution

  • You need to return something from the sh step either returnStatus or returnStdout https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

    e.g.

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