Creating multi-select dropdown (Extended Choice) UI boxes in Jenkins pipeline script

Posted on March 6th, 2018 at 19:00 PM


We had a number of legacy, non-pipeline Jenkins jobs that used a number of dropdown Extended Choice (EC) boxes to allow selection of runtime parameters. Growth in the parameter selection, and jobs, over several years was causing maintainence headaches especially with Jenkins' GUI. Until recently adding EC parameters within a pipeline script seemed impossible since there is still only native support for a small number of parameter types eg boolean, string and simple choice.

Creating an EC parameter in its own class proved to be the solution:

class dropDown
{
    def static newInst(name)
    {
        def com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition test = 
          new com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition(
            name,
            "PT_MULTI_SELECT",
            "A,B,C,D,E,F",  // displayed selection values
            null,//project name
            null,null,null,
            null,// bindings
            null,
            null, // propertykey
            "B", //default value
            null,null,null,
            null, //default bindings
            null,null,
            null, //descriptionPropertyValue
            null,null,null,null,null,null,
            null,// javascript file
            null, // javascript
            false, // save json param to file
            false, // quote
            5, // visible item count
            null,
            "," // separator
        )
        return test
}

The following assumes you have the Extended Choice plugin installed. Multiple instances of EC parameters can be added to 'parameters' and mixed with other parameters, eg boolean, as the first stage of a pipeline. Simply add the class definition above to your pipeline script before the following 'node':

node("nodex")
{
    stage ("UI")
    {
        List params = []
        List props = []
    
        Inst1 = dropDown.newInst("DDX")
        Inst2 = dropDown.newInst("DDY")
    
        params << booleanParam(name: 'BOOLX', defaultValue: true, description: '')
        params << Inst1
        params << Inst2

        props << parameters(params)
        properties(props)
    }

    stage("T1")
    {
        echo "Value ${DDX}"
    }
    
    stage("T2")
    {
        echo "Value ${DDY}"
    }                        
}

Remember that the job UI is updated after a run so if you add a new parameter to the script it will not appear until after you have run the job.


The job console after multi selection is shown below:

[Pipeline] {
[Pipeline] properties
[Pipeline] stage
[Pipeline] { (T1)
[Pipeline] echo
Value B,D
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (T2)
[Pipeline] echo
Value C,E
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

The full EC definition descriptor is shown below as taken from here (and you can also find PARAMETER_TYPEs eg PT_MULTI_SELECT there too):

ExtendedChoiceParameterDefinition(
name, 
type, 														
propertyValue, 
projectName,
propertyFile, 
groovyScript, 
groovyScriptFile, 
bindings, 
groovyClasspath, 
propertyKey, 
defaultPropertyValue, 
defaultPropertyFile, 
defaultGroovyScript, 
defaultGroovyScriptFile, 
defaultBindings, 
defaultGroovyClasspath, 
defaultPropertyKey, 
descriptionPropertyValue, 
descriptionPropertyFile, 
descriptionGroovyScript, 
descriptionGroovyScriptFile, 
descriptionBindings, 
descriptionGroovyClasspath, 
descriptionPropertyKey,
javascriptFile,
javascript,
saveJSONParameterToFile,
quoteValue, 
visibleItemCount, 
description, 
multiSelectDelimiter);