fahri
fahri

Reputation: 21

jenkins pipeline - how to set ChoiceType Condition options

I've two deployment categories, namely install and upgrade with the following code below and in the next parameters using Active choice reactive parameters called "services" i want to create separate ChoiceType which if the Category "Install" the choiceType: 'PT_MULTI_SELECT' (which means we can choose more than one service ) and if the Category "Upgrade" the choiceType: 'PT_SINGLE_SELECT', is that possible?

properties([
    parameters([
        [$class: 'ChoiceParameter', 
        choiceType: 'PT_SINGLE_SELECT', 
        description: 'Select Deployment Type', 
        filterLength: 1, filterable: false, 
        name: 'deployment', 
        script: [$class: 'GroovyScript', fallbackScript: 
                                         [classpath: [], 
                                         sandbox: false, 
                                         script: "return['error']"], 
                                         script: [classpath: [], sandbox: false, 
                                            script: "return['Install','Upgrade']"]]],

something like this

if (deployment.equals("Install"))
{
[$class: 'CascadeChoiceParameter', 
        choiceType: 'PT_MULTI_SELECT',
...
...
}
if (deployment.equals("Upgrade"))
{
[$class: 'CascadeChoiceParameter', 
        choiceType: 'PT_SINGLE_SELECT',
...
...
}

Upvotes: 0

Views: 919

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6824

You can't conditionally create the parameters with different attributes, however there is a simple solution for your need: You can define an Active Choices Reactive Reference Parameter and set the choice type to Formatted HTML, in the HTML define a select with a multiple attribute that will be defined according to your deployment parameter.
The groovy script for the services parameter will look like the following:

return """
<select name="value" ${deployment.equals("Install") ? 'multiple' : ''}>
   <option value="Service1">Service1</option>
   <option value="Service2">Service2</option>
   <option value="Service3">Service3</option>
   <option value="Service4">Service4</option>
   <option value="Service5">Service5</option>
</select>
"""

Now when deployment value will be set to Install the select will be a multi select, and for other values it will be a single select.
You can of course create the options section dynamically using groovy code.
Also important to know that the values that will be passed to your job are the option values (value="XXX") not the option text itself.

Pipeline script will look like:

    properties([ 
        parameters([
                [$class: 'DynamicReferenceParameter', 
                 choiceType: 'ET_FORMATTED_HTML', 
                 name: 'services', 
                 omitValueField: false, 
                 referencedParameters: 'deployment', 
                 script: [$class: 'GroovyScript', script: [sandbox: false, script: '''
                        return """
                        <select name="value" ${deployment.equals("Install") ? "multiple" : ""}>
                           <option value="Service1">Service1</option>
                           <option value="Service2">Service2</option>
                           <option value="Service3">Service3</option>
                           <option value="Service4">Service4</option>
                           <option value="Service5">Service5</option>
                        </select>
                        """''']]]
        ]) 
])

UI config will look like This

Update

Example for setting the options dynamically - if you want to set the options according to another parameter, lets say branch you just need to add it to the Referenced parameters of your deployment parameter and add the relevant logic according to the value of the parameter.
Here is an example for the new groovy script:

services = ['Service1','Service2']
if(branch =='master'){.   // create any logic you need acodring to the brach value
     services = ['Service3','Service4']
}
options = services.collect{"<option value='${it}'>${it}</option>"}.join('\n')
return "<select name='value' ${deployment.equals('Install') ? 'multiple' : ''}>${options}</select>"

Upvotes: 1

Related Questions