使用“When"的 Jenkins 流水线条件阶段用于选择参数 [英] Jenkins pipeline conditional stage using "When" for choice parameters

查看:75
本文介绍了使用“When"的 Jenkins 流水线条件阶段用于选择参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to build a jenkins pipeline where I have a choice parameter with n choices and want to create a stage which does something when some values from the choice parameter are selected我有类似下面的东西,但似乎不起作用.

I am trying to build a jenkins pipeline where I have a choice parameter with n choices and want to create a stage which does something when some values from the choice parameter are selected I have something like below but doesn't seem to work.

#!/usr/bin/env groovy

pipeline {

agent any

    parameters {

        choice(
                choices: 'a
b
c
d
e
f',
                description: 'name of the student',
                name: 'name'
        )
    }
 stages {
       stage ('callNames') {

        when {
             expression { params.name == 'a|d|f' }
        }
        steps{
        echo "selected name is: ${name}"
        //do something

            }
        }        
    }
}

所以,当参数 name 的选定值是 ad 时,我想 做某事> f对于上述内容,我没有收到任何错误,但我在控制台输出中看到了这一点

So, I want to do something when the selected values for the parameter name are either a or d of f For the above, I get no errors but I am seeing this in the console output

当我在构建期间选择值 a/d/f 时,由于条件而跳过了阶段 'callNames'

Stage 'callNames' skipped due to when conditional when I select the value a/d/f during the build

请告诉我这里缺少什么提前致谢

Please let me know what's missing here Thanks in advance

推荐答案

你的 when 表达式有错误.如果您的参数的 name 值为 'a',则您正在比较代码中的字符串 'a' == 'a|d|f',即 false.

Your when expression has an error. If your parameter's name value is 'a', you are comparing strings 'a' == 'a|d|f' in your code, which is false.

你可能想做

when {
    expression { 
        params.name == 'a' ||
        params.name == 'd' ||
        params.name == 'f' 
    }
}

或者,如果你更喜欢oneliner,你可以使用正则表达式

Or, if you prefer oneliner, you can use regular expression

when {
    expression { 
        params.name ==~ /a|d|f/
    }
}

这篇关于使用“When"的 Jenkins 流水线条件阶段用于选择参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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