Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multi select value parameter in Jenkins file(Groovy)

E.g. Below code is used for single select value

        choice{
           choices: 'Box\nOneDrive\nSharePointOnline\nGmail\nGDrive\nGenericS3',
           defaultValue: 'box', 
           description:  'Connector to build',
           name: 'On_Cloud_Devices_To_Test'
         }
like image 841
Aditya Avatar asked Feb 21 '18 05:02

Aditya


2 Answers

I would use booleanParam's. Then the user can tick all the required options.

booleanParam(defaultValue: false, name: 'ALL', description: 'Process all'),
booleanParam(defaultValue: false, name: 'OPTION_1', description: 'Process option 1'),
booleanParam(defaultValue: false, name: 'OPTION_2', description: 'Process options 2'),
like image 163
metalisticpain Avatar answered Sep 20 '22 07:09

metalisticpain


You could use "Extended Choice Parameter" plugin, as this page suggests.

Sinse the parameter list is very long, you may wrap it into a function:

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

def checkBox (String name, String values, String defaultValue,
              int visibleItemCnt=0, String description='', String delimiter=',') {

    // default same as number of values
    visibleItemCnt = visibleItemCnt ?: values.split(',').size()
    return new ExtendedChoiceParameterDefinition(
            name, //name,
            "PT_CHECKBOX", //type
            values, //value
            "", //projectName
            "", //propertyFile
            "", //groovyScript
            "", //groovyScriptFile
            "", //bindings
            "", //groovyClasspath
            "", //propertyKey
            defaultValue, //defaultValue
            "", //defaultPropertyFile
            "", //defaultGroovyScript
            "", //defaultGroovyScriptFile
            "", //defaultBindings
            "", //defaultGroovyClasspath
            "", //defaultPropertyKey
            "", //descriptionPropertyValue
            "", //descriptionPropertyFile
            "", //descriptionGroovyScript
            "", //descriptionGroovyScriptFile
            "", //descriptionBindings
            "", //descriptionGroovyClasspath
            "", //descriptionPropertyKey
            "", //javascriptFile
            "", //javascript
            false, //saveJSONParameterToFile
            false, //quoteValue
            visibleItemCnt, //visibleItemCount
            description, //description
            delimiter //multiSelectDelimiter
            )
}

Then use it as follows:

def testParam = checkBox("opt", // name
                "opt1,opt2,opt3", // values
                "opt1", //default value
                0, //visible item cnt
                "Multi-select", // description
                )

properties(
  [parameters([testParam])]
)

node {
    echo "${params.opt}"
}

Jenkins Build with Parameters:

enter image description here

This is scripted pipeline syntax by the way.

like image 26
Hsu Hau Avatar answered Sep 20 '22 07:09

Hsu Hau