Jenkins cascading parameters using Active Choice Parameter Plugin and Groovy

Sravan K
3 min readApr 15, 2020

Hey Net-Heads!!

Of late, I have been working on CICD pipelines (though not as colorful pipelines as in the pic :))and have come across a task to build a job with parameters that provisions dynamic population of drop down list on selection of another drop down value.

Jenkins do not outline in-built plugins to support this feature. I started searching for options and found this Active Choice Parameter Plugin.

In this post, we will establish an approach to implement the cascading feature using a Jenkinsfile (Declarative Pipeline)

Behind the scenes:

Download and Install Active Choice Plugin: Jenkin’s plugin manager is the hot-spot to upgrade/install/uninstall required plugins. Navigate to Plugin Manager and click on Download now and install after restart to install the plugin.

Jenkin’s Plugin Manager UI

Using a Jenkinsfile…

Let’s toss around a rendering example:

Active Choice Reactive Parameter — Jenkins UI

In the example above the value options for the ‘Items’ parameter get updated when the ‘Categories’ parameter changes.

Creating a Jenkinsfile:

#!/usr/bin/env groovy// Define variables
List category_list = ["\"Select:selected\"","\"Vegetables\"","\"Fruits\""]
List fruits_list = ["\"Select:selected\"","\"apple\"","\"banana\"","\"mango\""]List vegetables_list = ["\"Select:selected\"","\"potato\"","\"tomato\"","\"broccoli\""]List default_item = ["\"Not Applicable\""]String categories = buildScript(category_list)
String vegetables = buildScript(vegetables_list)
String fruits = buildScript(fruits_list)
String items = populateItems(default_item,vegetables_list,fruits_list)
// Methods to build groovy scripts to populate data
String buildScript(List values){
return "return $values"
}
String populateItems(List default_item, List vegetablesList, List fruitsList){return """if(Categories.equals('Vegetables')){
return $vegetablesList
}
else if(Categories.equals('Fruits')){
return $fruitsList
}else{
return $default_item
}
"""
}
// Properties step to set the Active choice parameters via
// Declarative Scripting

properties([
parameters([
[$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', name: 'Categories', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: 'return ["ERROR"]'], script: [classpath: [], sandbox: false,
script: categories]]],
[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT',name: 'Items', referencedParameters: 'Categories', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: 'return ["error"]'], script: [classpath: [], sandbox: false, script: items]]]
])

])
pipeline { agent anystages {
stage('Build'){
steps {
echo 'Building..'
}
}
}
}

We are ready with the script. Now, It’s showtime!!!

Click on the video to know how to use the pipeline script and simulate the above rendering example:

Thank you for reading!!
Stay tuned for more productionised solutions!!!

Happy Coding!!!

Interested to read more stories, you are always welcome to have a look at my writings here:

--

--

Sravan K
Sravan K

Responses (4)