How to do a multiselect input in a pipeline

Issue

How to do a multiselect input in a pipeline using Extended Choice Parameter plugin

Environment

Resolution

Extended Choice Parameter plugin offers many options, which can be easily explored by using The snippet Generator.

Following a couple of examples:

  • Scripted Pipeline
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition

node {
def multiSelect= new ExtendedChoiceParameterDefinition("name", 
            "PT_MULTI_SELECT", 
            "blue,green,yellow,blue", 
            "project name",
            "", 
            "",
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "blue,green,yellow,blue", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            "", 
            false,
            false, 
            3, 
            "multiselect", 
            ",") 

   def userInput = input  id: 'customID', message: 'Let\'s promote?', ok: 'Release!', parameters:  [multiSelect]


echo "Hello: "+ userInput
}

  • Declarative Pipeline
pipeline {
    agent any
    stages {
        stage('Example') {
            input {
                message "Let's promote?"
                ok 'Release!'
                parameters {
                    extendedChoice defaultValue: 'blue,green,yellow,blue', description: '', descriptionPropertyValue: 'blue,green,yellow,blue', multiSelectDelimiter: ',', name: 'favColor', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_SELECT', value: 'blue,green,yellow,blue', visibleItemCount: 5
                }
            }
            steps {
                echo "Your favorite color is ${favColor}"
            }
        }
    }
}

Tested product/plugin versions

The latest update of this article has been tested and verified with:

Have more questions?

4 Comments

  • 0
    Avatar
    Diego Vitorino

    Hi, great example, I’m using in my scenario

    Is there the possibility to limit number of choices? I have more than 10 values and I would like to limit only 3 selections options , in case of more than 3 show the echo “”Number of choices exceded”

    Thank you so much

    Edited by Diego Vitorino
  • 0
    Avatar
    Adrian Blakey

    Is there any support for multi-select in a declarative pipeline?

  • 0
    Avatar
    Tim Black

    Is there any support for multi-select in a declarative pipeline?

    UPDATE: yes, apparently there is. Through viewing pipeline error codes, browsing stack overflow and eventually, the plugin source code, I see that after installing the plugin, extendedChoice is another valid type you can use in your declarative "parameters {" section, as in:

     

    extendedChoice(name: "favorite_letters",
    type: "PT_CHECKBOX",
    multiSelectDelimiter: " ", // this only defines delimiter used in the output string value, not used for parsing value input, which must be comma-separated!
    value: """a, b, c, d, e, f""",
    defaultValue: "d, f",
    description: "Select your favorite letter(s)"
    )

    There is woefully little documentation, however, on how all the various "type"s of extendedChoice parameters actually work. The best docs I could find was by looking at my pipeline job configuration through the web interface, which of course shows UI for all the possible types:

    ..and their subparams:

    So far, the JSON types look most interesting and extensible. I hope I can figure out how those work without spending the rest of the day sleuthing..

    Edited by Tim Black
  • 0
    Avatar
    Puneeth Nanjundaswamy

    This is not useable with blue ocean interface

Please sign in to leave a comment.