0

如何訪問environment塊內現有的環境變量SOME_VAR?我想用新名稱重新定義它NEW_VARJenkinsfile訪問現有環境變量

pipeline { 
    agent { 
     label any 
    } 
    environment { 
     SECRET = credentials('secret-credential') // works as expected 
     NEW_VAR = ? // get from different env variable 
    } 
    stages { 
     stage('Test') { 
      steps { 
       echo "SECRET=${env.SECRET}" 
       echo "NEW_VAR=${env.NEW_VAR}" 
      } 
     } 
    } 
} 

我試過到目前爲止:

NEW_VAR = SOME_VAR  // fails build 
NEW_VAR = env.SOME_VAR // fails build 
NEW_VAR = ${env.SOME_VAR} // fails build 
NEW_VAR = env('SOME_VAR') // does not fail, but NEW_VAR is null 
NEW_VAR = env(SOME_VAR) // does not fail, but NEW_VAR is null 

回答

1

使用此:

environment { 
    NEW_VAR = "${env.SOME_VAR}" 
} 

這樣你做string interpolation,然後到新的變種指定字符串值。
剛剛測試過,這應該工作。