Home > Software design >  jenkins multiple selection choice input
jenkins multiple selection choice input

Time:01-28

{  ... script {  ....
    if (isStartedByUser) {
        try {
          timeout(time: 60, unit: 'SECONDS') {
            def userInput = input(
              id: 'userInput', message: 'Please Enter Environment', parameters:
                [choice(name: 'environment', choices: tf.join("\n"), description: 'Choose environment you want to deploy this build'),
                 choice(name: 'debuglevel', choices: "\nINFO\nTRACE\nDEBUG\nWARN\nERROR", description: 'Choose debug level')
                ]
            )
            mydir = userInput['environment']

here

I have this in a Jenkins pipeline. tf is a List imported from a json. Currently this code snippet only allows a single choice to be selected. I would like to be able to select multiple choices for the environment drop down selector.

Ok, So now I have this and it works! but need a slight optimization:

 def multiSelect= new ExtendedChoiceParameterDefinition("Envs",
            "PT_MULTI_SELECT",
            tf.join(","),
            "project name",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            tf.join(","),
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            false,
            false,
            15,
            "Available Environments",
            ",")


            def userInput = input(
              id: 'userInput', message: 'Please Select terraform params', parameters:
                [multiSelect]
            )
            
            def userInput2 = input(
              id: 'userInput', message: 'Please Select terraform params', parameters:
                [choice(name: 'debuglevel', choices: "\nINFO\nTRACE\nDEBUG\nWARN\nERROR", description: 'Choose debug level')
                ]
            )
            
            terraformdir = userInput.split(',')
            println "dir ${terraformdir}"
            env.TF_LOG = userInput2['debuglevel']

I was hoping to get both inputs on the same screen.

When I did

def userInput = input(
              id: 'userInput', message: 'Please Select terraform params', parameters:
                [multiSelect,
                 choice(name: 'debuglevel', choices: "\nINFO\nTRACE\nDEBUG\nWARN\nERROR", description: 'Choose debug level')
                ]
            )
            

I wasn't sure how to access the value of multiSelect.

CodePudding user response:

Yes, you should be able to, take a look at the example here. Following is a sample Declarative Pipeline.

import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition

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

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script {
                     def userInput = input  id: 'customID', message: 'Let\'s promote?', ok: 'Release!', parameters:  [multiSelect]
                }
                echo 'Hello World'
            }
        }
    }
}
  • Related