如何避免重复适用于 Jenkinsfile 管道中多个步骤的条件?
How can I avoid duplication of conditions that apply to multiple steps in my Jenkinsfile pipeline?
目前我有一个如下所示的 Jenkinsfile :
stage('A'){
agent{docker{image A}}
when{branch = 'master'}
step{do blah}
}
stage('B'){
when{branch = 'master'}
agent{docker{image B}}
step{do blah}
}
担心这个条件到处都是重复的,我想提取它 - 但无法识别正确的语法(如果有的话)。
您可以先使用环境变量(设置条件):
https://jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables-dynamically.
然后就可以使用环境了when
:
https://jenkins.io/doc/book/pipeline/syntax/#built-in-conditions
Execute the stage when the specified environment variable is set to
the given value, for example:
when { environment name: 'DEPLOY_TO', value: 'production' }
目前我有一个如下所示的 Jenkinsfile :
stage('A'){
agent{docker{image A}}
when{branch = 'master'}
step{do blah}
}
stage('B'){
when{branch = 'master'}
agent{docker{image B}}
step{do blah}
}
担心这个条件到处都是重复的,我想提取它 - 但无法识别正确的语法(如果有的话)。
您可以先使用环境变量(设置条件): https://jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables-dynamically.
然后就可以使用环境了when
:
https://jenkins.io/doc/book/pipeline/syntax/#built-in-conditions
Execute the stage when the specified environment variable is set to the given value, for example:
when { environment name: 'DEPLOY_TO', value: 'production' }