Pritish
Pritish

Reputation: 774

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

I am trying to add radio buttons in Jenkins instead of checkboxes when running Build with Parameters. In my existing code, I am getting checkboxes. What changes should I make in my existing code to get the radio buttons. Below is my groovy code:

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

Please find the below screenshot: enter image description here

Upvotes: 0

Views: 8466

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6824

You can achieve that easily using the Extended Choice Parameter which has a built in support for all kinds of selection options including radio buttons.
You can use it as follows:

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

The result will look like:
enter image description here

You can also use the Active Choices plugin, which is a bit more complicated but has more configuration options, it will also allow you to generate the values from a groovy script.
For example:

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']"]]]]
)])

Upvotes: 4

Related Questions