如何在Jenkins中添加单选按钮使用groovy时,使用“Build with parameters”构建一个作业?

9wbgstp7  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(355)

我尝试在Jenkins中添加radio按钮,而不是在运行Build with Parameters时添加checkboxes。在我现有的代码中,我得到了复选框。我应该在现有的代码中做些什么修改来得到单选按钮。下面是我的groovy代码:

def call(Map config = [:]) {

    paramsList = []

    paramsList << booleanParam(name: 'deployToDev', defaultValue: false, description: 'Set to true to deploy to Dev K8s Environment')
    paramsList << booleanParam(name: 'deployToTest', defaultValue: false, description: 'Set to true to deploy to Test K8s Environment')
    paramsList << booleanParam(name: 'deployToStage', defaultValue: false, description: 'Set to true to deploy to Stage K8s Environment')
    properties([parameters(paramsList)])
}

请查看以下截图:

insrf1ej

insrf1ej1#

您可以使用Extended Choice Parameter轻松实现这一点,Extended Choice Parameter内置了对包括单选按钮在内的各种选择选项的支持。
您可以按如下方式使用它:

def call(Map config = [:]) {

    paramsList = []

    // You can also set the default value using the 'defaultValue' option
    paramsList << extendedChoice(name: 'DeployTo', description: 'Select the environment to deploy to', type: 'PT_RADIO', value: 'Dev,Test,Stage', visibleItemCount: 5)

    properties([parameters(paramsList)])
}

结果如下所示:

您也可以使用Active Choices插件,它稍微复杂一些,但是有更多的配置选项,它还允许您从groovy脚本生成值。
例如:

properties([parameters(
   [[$class: 'ChoiceParameter', name: 'DeployTo', choiceType: 'PT_RADIO', description: 'Select the environment to deploy to', script: [$class: 'GroovyScript', script: [classpath: [], sandbox: false, script: "return ['Dev','Test','Stage']"]]]]
)])

相关问题