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).
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.
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.
booleanParam ( name: 'TEST', defaultValue: false, description: 'Test?' )
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() );
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With