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

How to add radio buttons in Jenkins using groovy when building a job using "Build with parameters"?

当 运行 Build with Parameters 时,我试图在 Jenkins 中添加 radio 按钮而不是 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)])
}

请查看以下截图:

您可以使用 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']"]]]]
)])