Jenkins Pipeline阶段跳过基于管道中定义的groovy变量

Har*_*ran 7 groovy continuous-deployment jenkins jenkins-groovy jenkins-pipeline

我试图跳过stage基于常规变量,该变量值将在另一个阶段计算。

在下面的示例中,Validate阶段是基于环境变量有条件地跳过的VALIDATION_REQUIRED,我将在构建/触发作业时传递该环境变量。---这按预期工作。

Build而即使isValidationSuccess变量设置为 ,阶段也始终运行false。我尝试更改when条件表达式,例如{ return "${isValidationSuccess}" == true ; } or{ return "${isValidationSuccess}" == 'true' ; }但没有成功。打印变量时它显示为“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}"
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)
  1. 将在哪个阶段when评估条件。
  2. 是否可以使用 跳过基于变量的阶段when
  3. 基于一些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')
       }
}
Run Code Online (Sandbox Code Playgroud)

参考文献: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/

mke*_*erz 7

stage("Build") {
        when {
            expression { isValidationSuccess == true }
        }
        steps {
             // do stuff
        }
    }
Run Code Online (Sandbox Code Playgroud)

when验证布尔值,因此应评估为 true 或 false。

来源