Jenkins使用“何时"管道条件阶段.用于选择参数 [英] Jenkins pipeline conditional stage using "When" for choice parameters

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

问题描述

我正在尝试构建Jenkins管道,其中我有一个选择参数,其中n个选择,想要创建一个阶段,它在选择选项参数中的某些值时会产生某些东西我有类似下面的内容,但似乎无法正常工作.

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\nb\n\c\n\d\ne\nf',
                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 的选定值是 a d 执行某些操作 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

阶段"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

推荐答案

您的何时表达式有错误.如果参数的 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/
    }
}

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

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