一千萬個為什麽

搜索

如何使用Path設置和獲取var?



我通過Jenkins為ssh編寫部署文件的腳本

#!groovy

node('superhost01'){
    String HOSTNAME="host01"
    String USERNAME="tech_user"
    withEnv(['PATH=/data/jdbc_connector'])
    stage('Prepare') {
        checkout scm
    }
    stage('Deploy') {
        sh """
        scp -r config.yaml ${USERNAME}@${HOSTNAME}:$PATH
        """
}

}

但詹金斯回歸錯誤

withEnv(['PATH=/data/jdbc_connector'])

錯誤:

[Pipeline] node
Running on superhost01 in /data/jenkins/workspace/jdbc_connector
[Pipeline] {
[Pipeline] withEnv
[Pipeline] }
[Pipeline]//node
[Pipeline] End of Pipeline
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
java.lang.IllegalStateException: There is no body to invoke
    at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283)

如何正確分配變量的值?

轉載註明原文: 如何使用Path設置和獲取var?

一共有 1 個回答:

withEnv doesn't persist environment variables. Instead, you need to tell withEnv what steps to run with that environment by passing it a block. Here is what I think you want your code to look like:

#!groovy

node('superhost01'){
  String HOSTNAME="host01"
  String USERNAME="tech_user"
  withEnv(['PATH=/data/jdbc_connector']) {
    stage('Prepare') {
      checkout scm
    }

    stage('Deploy') {
      sh """
      scp -r config.yaml ${USERNAME}@${HOSTNAME}:$PATH
      """
    }
  }
}

作為旁註,我懷疑你確實想要將 PATH 設置為/data/jdbc_connector:$ PATH ,否則你的結賬步驟將無法找到git和你的部署步驟將無法找到scp。