在默認的情況下,一旦設置了新的環境變量,那麼這個環境變量在整個Pipeline生命週期裏都不會改變,但是有時我們需要在Stage階段臨時修改變量值,傳遞給其他命令使用,那麼如何實現呢?
So easy!
使用withEnv
命令即可,它會讓環境變量在withEnv
語句塊臨時被修改,一旦不在withEnv
語句塊則還是原始值
pipeline {
agent any
environment {
Name = 'null'
}
stages {
stage('Stage 1'){
steps{
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE'){
script {
echo env.Test_Category_Name // 輸出 null
withEnv(["Name =Test"]) {
echo env.Test_Category_Name // 輸出 Test
}
echo env.Test_Category_Name // 輸出 null
}
}
}
}
}
}
參考資料: Updating environment global variable in Jenkins pipeline from the stage level - is it possible?