簡體   English   中英

如何將命令的輸出轉換為Jenkinsfile中的環境變量?

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

因此,我可以按照以下步驟捕獲變量:

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

這可行。 除了我需要這個變量在整個Jenkinsfile的作用域內,在所有階段,而不僅僅是這個階段。 但是我似乎無法在舞台之外使用sh()。 有任何想法嗎?

您可以在Jenkinsfile的頂部定義一個變量,然后可以在整個Jenkinsfile中訪問此變量。

def INVENTORY_FILE

pipeline {

    stages {

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

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM