Jenkins 管道阶段跳过基于管道中定义的 groovy 变量
Jenkins Pipeline stage skip based on groovy variable defined in pipeline
我正在尝试跳过基于 groovy 变量的 stage
,该变量值将在另一个阶段计算。
在下面的示例中,Validate
阶段根据环境变量 VALIDATION_REQUIRED
有条件地跳过,我将在 building/triggering 作业时传递它。 --- 这按预期工作。
而 Build
阶段始终运行,即使 isValidationSuccess
变量设置为 false
。
我尝试更改 when
条件表达式,如 { return "${isValidationSuccess}" == true ; }
或 { return "${isValidationSuccess}" == 'true' ; }
,但 none 有效。
打印变量时显示为 'false'
def isValidationSuccess = true
pipeline {
agent any
stages(checkout) {
// GIT checkout here
}
stage("Validate") {
when {
environment name: 'VALIDATION_REQUIRED', value: 'true'
}
steps {
if(some_condition){
isValidationSuccess = false;
}
}
}
stage("Build") {
when {
expression { return "${isValidationSuccess}"; }
}
steps {
sh "echo isValidationSuccess:${isValidationSuccess}"
}
}
}
when
条件将在哪个阶段进行评估。
- 是否可以使用
when
跳过基于变量的阶段?
- 基于一些 SO 答案,我可以考虑如下添加条件块,但是
when
选项看起来很干净。此外,stage view
在跳过该特定阶段时显示得很好。
script {
if(isValidationSuccess){
// Do the build
}else {
try {
currentBuild.result = 'ABORTED'
} catch(Exception err) {
currentBuild.result = 'FAILURE'
}
error('Build not happened')
}
}
参考资料:
https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/
stage("Build") {
when {
expression { isValidationSuccess == true }
}
steps {
// do stuff
}
}
when
验证布尔值,因此应评估为 true 或 false。
我正在尝试跳过基于 groovy 变量的 stage
,该变量值将在另一个阶段计算。
在下面的示例中,Validate
阶段根据环境变量 VALIDATION_REQUIRED
有条件地跳过,我将在 building/triggering 作业时传递它。 --- 这按预期工作。
而 Build
阶段始终运行,即使 isValidationSuccess
变量设置为 false
。
我尝试更改 when
条件表达式,如 { return "${isValidationSuccess}" == true ; }
或 { return "${isValidationSuccess}" == 'true' ; }
,但 none 有效。
打印变量时显示为 'false'
def isValidationSuccess = true
pipeline {
agent any
stages(checkout) {
// GIT checkout here
}
stage("Validate") {
when {
environment name: 'VALIDATION_REQUIRED', value: 'true'
}
steps {
if(some_condition){
isValidationSuccess = false;
}
}
}
stage("Build") {
when {
expression { return "${isValidationSuccess}"; }
}
steps {
sh "echo isValidationSuccess:${isValidationSuccess}"
}
}
}
when
条件将在哪个阶段进行评估。- 是否可以使用
when
跳过基于变量的阶段? - 基于一些 SO 答案,我可以考虑如下添加条件块,但是
when
选项看起来很干净。此外,stage view
在跳过该特定阶段时显示得很好。
script {
if(isValidationSuccess){
// Do the build
}else {
try {
currentBuild.result = 'ABORTED'
} catch(Exception err) {
currentBuild.result = 'FAILURE'
}
error('Build not happened')
}
}
参考资料: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/
stage("Build") {
when {
expression { isValidationSuccess == true }
}
steps {
// do stuff
}
}
when
验证布尔值,因此应评估为 true 或 false。