基于管道中定义的Groovy变量的Jenkins管道阶段跳过 [英] Jenkins Pipeline stage skip based on groovy variable defined in pipeline
问题描述
我正尝试跳过基于常规变量的阶段
,该变量值将在另一个阶段进行计算。
I'm trying to skip a stage
based a groovy variable and that variable value will be calculated in another stage.
在下面的示例中,根据环境变量 VALIDATION_REQUIRED
有条件地跳过了 Validate
阶段,我将在构建时通过该变量/触发工作。 --- 这按预期方式工作。
In the below example, Validate
stage is conditionally skipped based Environment variable VALIDATION_REQUIRED
which I will pass while building/triggering the Job. --- This is working as expected.
而 Build
阶段始终运行即使将 isValidationSuccess
变量设置为 false
。
我尝试更改条件条件表达式,例如
{return $ {isValidationSuccess} == true; }
或 {返回 $ {isValidationSuccess} ==‘true’; }
,但没有一个起作用。
打印变量时,它显示为'false'
Whereas the Build
stage always runs even though isValidationSuccess
variable is set as false
.
I tried changing the when
condition expression like { return "${isValidationSuccess}" == true ; }
or { return "${isValidationSuccess}" == 'true' ; }
but none worked.
When printing the variable it shows as '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}"
// Perform some action
}
}
}
-
何时
条件将被评估。 - 是否可以使用
何时
? - 基于一些SO答案,我可以考虑如下添加条件块,但是
何时
选项看起来很干净。此外,当跳过特定阶段时,阶段视图
会很好地显示。
- At what phase does the
when
condition will be evaluated. - Is it possible to skip the stage based on the variable using
when
? - Based on a few SO answers, I can think of adding conditional block as below, But
when
options look clean approach. Also, thestage view
shows nicely when that particular stage is skipped.
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
}
}
何时
验证布尔值,因此应将其评估为true或false。
when
validates boolean values so this should be evaluate to true or false.
这篇关于基于管道中定义的Groovy变量的Jenkins管道阶段跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!