58

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

mkobit
  • 43,979
  • 12
  • 156
  • 150
red888
  • 27,709
  • 55
  • 204
  • 392

4 Answers4

89

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()
);
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 3
    interesting so if its triggered non-interactively will param.param1 be the first choice or will it be null? – red888 Dec 18 '17 at 17:47
  • @red888 my understanding is first value for both scripted and declarative. I haven't tested out this answer yet, but I do remember being affected by [JENKINS-35698 - _Initial run of parameterized pipeline build should return properties default value_](https://issues.jenkins-ci.org/browse/JENKINS-35698). – mkobit Dec 18 '17 at 17:56
  • Thanks, the docs are not clear about why there is no integer parameter type for example, is that on the cards, to make parameters be feature-complete? –  Mar 28 '19 at 11:07
  • 1
    @ConradB I don't work on much of Jenkins OSS stuff, so I couldn't be the one to tell you. I haven't been working in Jenkins for some time, so I'm not sure if things have changed. – mkobit Mar 28 '19 at 22:40
8

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
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
Chris Tompkinson
  • 588
  • 7
  • 18
1

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

rok
  • 9,403
  • 17
  • 70
  • 126
  • 1
    You should add this is a non-native plugin which looks semi-stale? Got some old PRs and hasn't been updated in 6 months https://github.com/jenkinsci/extended-choice-parameter-plugin/pulls – red888 Sep 14 '21 at 13:16
0

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')
    }
...
}
t0r0X
  • 4,212
  • 1
  • 38
  • 34