Search code examples
jenkinsparametersjenkins-groovyextended-choice-parameter

Looping over parameters added by Jenkins extended choice parameters


I've added an extendedChoice param on my .groovy file as below

extendedChoice(name: 'Subscription', defaultValue: 'demo_standard', description: 'Check mark on subscription/Part number', multiSelectDelimiter: ',', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'demo_standard,demo_advanced,trial_standard,trial_advanced,standard,advanced', visibleItemCount: 6),

for calling this param I have below line.

sh "./create.js ${params.Username} ${params.Subscription} "

From the above line what I want to do is. If I select 2 | 3 | 4 | till 6 checks for subscriptions in params then above sh line should run those multiple times.

Example:

If I select "demo_advanced,trial_standard" then it should call sh command twice with both subscriptions.

sh "./create.js ${params.Username} ${params.Subscription} -->(demo_advanced will replace)"
sh "./create.js ${params.Username} ${params.Subscription} -->(trial_standard will replace)"

How can I write this on my .groovy file??


Solution

  • Here is a full sample of how you can do this in your Pipeline.

    properties([
        parameters([
            extendedChoice(name: 'Subscription', defaultValue: 'demo_standard', description: 'Check mark on subscription/Part number', multiSelectDelimiter: ',', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'demo_standard,demo_advanced,trial_standard,trial_advanced,standard,advanced', visibleItemCount: 6)
        ])
    ])
    
    pipeline {
      agent any
      stages {
          stage ("Example") {
            steps {
             script{
              script {
                echo "${params.Subscription}" 
                for(def sub : params.Subscription.split(',')) {
                    sh "./create.js ${params.Username} ${sub}"
                }
              }
              
            }
            }
          }
      }
    }