Search code examples
jenkinsjenkins-pluginsjenkins-pipeline

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


Solution

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