red888
red888

Reputation: 31550

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).

Upvotes: 61

Views: 105491

Answers (4)

t0r0X
t0r0X

Reputation: 4772

I use this solution for some years now. It's similar to the one from @chris-tompkinson, but maybe with a twist. The idea is to move the choice value selected in current build to the beginning of the choice values list, thus being offered at next build as new "default value".

import groovy.transform.Field

@Field
List<String> TARGET_SYS_VALUES = ['ci', 'dev', 'test', 'staging']

pipeline {
    agent { label any }

    parameters {
        choice(name: 'TARGET_SYS',
                choices: (params.TARGET_SYS ? [params.TARGET_SYS] : []) +
                             (TARGET_SYS_VALUES - 
                                 (params.TARGET_SYS ? [params.TARGET_SYS] : [])),
                description: 'Some fictive target systems')
    }
...
}

Upvotes: 0

rokpoto.com
rokpoto.com

Reputation: 10736

Another option is using extendedChoice parameter of type PT_RADIO. It supports setting default values.

You'd have to install this plugin

extendedChoice description: '', multiSelectDelimiter: ',', name: 'PROJ_NAME',
                quoteValue: false, saveJSONParameterToFile: false, type: 'PT_RADIO',
                value: 'a,b', visibleItemCount: 2, defaultValue: 'a'

It looks like below:

enter image description here

Upvotes: 1

Chris Tompkinson
Chris Tompkinson

Reputation: 596

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
}

Upvotes: 8

mkobit
mkobit

Reputation: 47259

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()
);

Upvotes: 94

Related Questions