lowly_junior_sysadmin
lowly_junior_sysadmin

Reputation: 161

How can I get the output of a command into an environmental variable in a Jenkinsfile?

So, I can capture a variable in a step like this:

stage('blah') {
  script {
    INVENTORY_FILE = sh(returnStdout: true, script: 'echo $(date +%Y%m%d).yml')
  }
}

And this works. Except I need this variable to be in scope for the entire Jenkinsfile, for all stages, not just this one. But I can't seem to use sh() outside of a stage. Any ideas?

Upvotes: 0

Views: 87

Answers (1)

yong
yong

Reputation: 13712

You can define a variable at the top of Jenkinsfile, then you can access this variable in entire Jenkinsfile.

def INVENTORY_FILE

pipeline {

    stages {

        stage('blah') {
          script {
            INVENTORY_FILE = sh(returnStdout: true, script: 'echo $(date +%Y%m%d).yml').trim()
          }
        }

    }
}

Upvotes: 2

Related Questions