這是一個宣告性的 Jenkins-Pipeline。
使用可鎖定資源插件 ( https://plugins.jenkins.io/lockable-resources/ ) 我想動態鎖定多個階段,具體取決于用戶在引數部分選擇的環境。這就是我希望這樣做的方式:
pipeline {
parameters {
choice choices: ['---', 'prod', 'test'], description: 'Environment', name: 'environment'
}
stage('MY_APPLICATION') {
options{
lock('resource': "${params.environment}")
}
stages {
stage('TEST') {
when { expression { "${params.environment}" == 'prod' } }
steps { ... }
}
stage('PROD') {
when { expression { "${params.environment}" == 'test' } }
steps { ... }
}
}
}
}
但是我無法訪問選項塊中的引數,它始終使用默認值。有沒有人知道如何根據環境變數動態鎖定資源?
uj5u.com熱心網友回復:
我設法這樣解決它:
在選項塊中,有可用的 $currentBuild 變數,這樣就可以動態鎖定資源:
pipeline {
parameters {
choice choices: ['---', 'prod', 'test'], description: 'Environment', name: 'environment'
}
stage('MY_APPLICATION') {
options{
lock('resource': "${currentBuild.getRawBuild().getEnvironment(TaskListener.NULL).environment}")
}
stages {
stage('TEST') {
when { expression { "${params.environment}" == 'prod' } }
steps { ... }
}
stage('PROD') {
when { expression { "${params.environment}" == 'test' } }
steps { ... }
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318831.html
