基于管道中定义的Groovy变量的Jenkins管道阶段跳过 [英] Jenkins Pipeline stage skip based on groovy variable defined in pipeline

查看:108
本文介绍了基于管道中定义的Groovy变量的Jenkins管道阶段跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试跳过基于常规变量的阶段,该变量值将在另一个阶段进行计算。

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
        }
    }

 }




  1. 何时条件将被评估。

  2. 是否可以使用何时?

  3. 基于一些SO答案,我可以考虑如下添加条件块,但是何时选项看起来很干净。此外,当跳过特定阶段时,阶段视图会很好地显示。

  1. At what phase does the when condition will be evaluated.
  2. Is it possible to skip the stage based on the variable using when?
  3. Based on a few SO answers, I can think of adding conditional block as below, But when options look clean approach. Also, the stage 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆