Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a default choice in jenkins pipeline?

quite frustrating I can't find an example of this. How do I set the default choice?

parameters {     choice(         defaultValue: 'bbb',         name: 'param1',         choices: 'aaa\nbbb\nccc',         description: 'lkdsjflksjlsjdf'     ) } 

defaultValue is not valid here. I want the choice to be optional and a default value to be set if the pipeline is run non-manually (via a commit).

like image 848
red888 Avatar asked Dec 18 '17 17:12

red888


People also ask

How do I add a choice parameter in Jenkins?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.

How does Jenkins Pipeline define choice parameter?

Unlike default parameter types, the Active choice parameter type gives you more control over the parameters using a groovy script. You can have dynamic parameters based on user parameter selection. To use the active choice parameter, you need to have an Active Choices plugin installed in Jenkins.

What is the default value of Boolean parameter in Jenkins?

booleanParam ( name: 'TEST', defaultValue: false, description: 'Test?' )


2 Answers

You can't specify a default value in the option. According to the documentation for the choice input, the first option will be the default.

The potential choices, one per line. The value on the first line will be the default.

You can see this in the documentation source, and also how it is invoked in the source code.

return new StringParameterValue(   getName(),    defaultValue == null ? choices.get(0) : defaultValue, getDescription() ); 
like image 54
mkobit Avatar answered Sep 21 '22 03:09

mkobit


As stated by mkobit it doesn't seem possible with the defaultValue parameter, instead I reordered the list of choices based on the previous pick

defaultChoices = ["foo", "bar", "baz"] choices = createChoicesWithPreviousChoice(defaultChoices, "${params.CHOICE}")  properties([     parameters([         choice(name: "CHOICE", choices: choices.join("\n"))     ])    ])   node {     stage('stuff') {         sh("echo ${params.CHOICE}")     } }  List createChoicesWithPreviousChoice(List defaultChoices, String previousChoice) {     if (previousChoice == null) {        return defaultChoices     }     choices = defaultChoices.minus(previousChoice)     choices.add(0, previousChoice)     return choices } 
like image 43
Chris Tompkinson Avatar answered Sep 23 '22 03:09

Chris Tompkinson