简体   繁体   中英

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?

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM