Parameterized Jenkins job with dependent parameter - jenkins

I am trying to create a Jenkins job that has dependent parameters.
Firstly I want to be able to choose a main parameter:
And then secondly to be able to choose from a set of options that are dependent parameters of the main parameter.
If I select a different main parameter:
I then want to have a different set of options as the dependencies to the second main parameter.
Please, can you help me with how I can achieve this?

I would suggest the Active Choices plugin (also known as "uno-choice"). (This question has references to both, though they're not the accepted answer.)
For your specific use case, you'll want to add two parameters to your job:
Active Choices Parameter
Name: MainOption
Script: Groovy Script
return ['A','B']
Active Choices Reactive Parameter
Name: DependentOption
Script: Groovy Script
def choices
switch(MainOption){
case 'A':
choices = ['Blue','Green','Yellow']
break
case 'B':
choices = ['Black','White','Grey']
break
default:
choices = ['N/A']
break
}
return choices
Fallback Script: Groovy Script
return ['Option error']
Referenced parameters:
MainOption
The "Referenced parameters" setting is the key - when that value is changed, the plugin will re-evaluate the Groovy script, giving you the dependent parameter effect.

For all of you who stumble upon the same kind of problem (like I did). There is a fairly new Jenkins plugin available that does exactly what is desired here: display a dependent set of drop-downs, updating dependent boxes when a main box changes selection.
For your job you just need to follow the following steps:
Install "Multiselect Parameter Plugin"
The "multiselect parameter plugin" can be installed from Jenkins plugin management, the documentation is available on its Jenkins Plugin page.
Add new parameter
Use "Multiselect Parameter" type
Set name to a sensible value
give a short description
configure like shown below:
H,Main option,Dependent option
V,SELECTED_MAIN,SELECTED_DEPENDENT
C,A,Blue
C,A,Green
C,A,Yellow
C,B,Black
C,B,White
C,B,Grey
Use selected values
When you run "build with parameters" in your job, the following boxes are displayed for selection:
In your build script you can simply use the configured environment variables SELECTED_MAIN and SELECTED_DEPENDENT, which contain the selected values from both select boxes.

Related

How to run all the component at once in choice parameters in jenkins?

Here i am using two choice parameter one is Branch and other is Component, Here i am able to run specific component but i need to run all the component at once?
1.Choices Parameter
Name: Branch
choices:trunk
branch/3.2.0
2.Choice Parameter:
Name: Component
Choices:loadbalance
alert
generic-report
backend-tool
You could declare 4 stages, each one with the second parameter already set (meaning not part of your parameters), and called using the parallel syntax
That way:
you don't have to deal with an "extended choice parameter"
you can differentiate each stage
you can run them in parallel

Trigger Jenkins Job for every parameter

I have created a Global choice Parameter using Extensible Choice Parameter plugin.
I am using this parameter list in one of my parametrized jenkins job.
Is there a way in jenkins, where I can execute the job with each of the parameters in the Global choice Parameter list?
I have had a look on Build Flow job in jenkins, as suggested in this answer, but it seems it accepts hardcoded parameters only, and not dynamic.
I finally managed to resolve this using the following steps (with great help from this post) -
As my parameters list is dynamic in nature, it could be added or modified according to other jobs, we have managed it in a text file.
Next, We have used Extensible Choice Parameter plugin to display the parameters, using the groovy script -
def list = [];
File file = new File("D:/JenkinJob/parameterList.txt")
file.eachLine { line ->
list.add("$line")
}
return list
Now I want to call this jenkins job for each of the parameter.
For this, I have installed, BuildFlow plugin, and crated a new jenkins job of BuildFlow type -
Next, get the Extended Choice Parameter plugin, and configure it as follows -
Now in the flow step of this job, write this script, where "Feature" is the parameter, that is just created above, and within call to "build" parameter, pass in the name of job which we want to call for each parameter -
def features = params['Features'].split(',')
for (feature in features ) {
build("JobYouWantToCall", JobParameter: feature,)
}

Jenkins pipeline parameter code for multiple active choices

For Active choice parameter Jenkins pipeline code
Project:
[]project1
[]project2
[]project3
[]project4
[] is a checkbox, we can select single or multiple projects.
I need the pipeline parameter code to bring this in Jenkins build parameter tab.
If you have already installed Active choice parameter plugin, please refer this article on medium which can tell you step by step how to build using checkboxes under active choice parameter.
Jenkins Active Choice parameter usecase
You can use bash shell to define the logic of the user input, by just using the variable as the parameter name. Put them inside a loop if you running a build script which has to applied to all the options selected.
IFS=","
#user choices value will be a comma separated value
domain=$YOUR_ACTIVE_CHOICE_PARAMETER_NAME
# get length of an array
names=($domain);
arraylength=${#names[#]}
# use for loop to read all values and indexes
for (( i=0; i<${arraylength}; i++ )); do
echo "==========="
<Your logic for build process>
echo "==========="
done

Jenkins Parameterized build to use key/value pairs

I have a Jenkins parameterized build. I tick the "this build is parameterized" and I set a "Choice" environment name to be "ENVIRONMENT" and then as choices I define human readable names such as "Test env1", "Test env2", etc. However I want these keys to actually contain different values, for example "Test env1" key would container a file path as its value. How can this be done?
I have managed to get the keys/values with a dropdown select parameter working with the Active Choices Plugin, the answer was actually buried in the comments on the plugin page itself.
You simply need to use a map rather than a list when writing your groovy script to define the dropdown options. The map key is what the parameter will be set to if the user selects this option. The map value is what will be actually displayed to the user (i.e something human readable) in the dropdown list.
Here are the steps.
Ensure you have the Active Choices Plugin installed.
Open the configuration of your Jenkins job, select This project is parameterised.
Click Add Parameter and select Active Choices Parameter.
Name your parameter ENVIRONMENT and click the Groovy Script check box.
In Groovy Script enter content: return ['env1 file path value':'Test env1', 'env2 file path value':'Test env2'] For this example the user will see a dropdown with 2 options: Test env1 and Test env2. The keys: env1 file path value and env2 file path value are what the Jenkins build parameter will be set to if the option is selected. Modify these as necessary.
In this case ENVIRONMENT is the key and "Test env1", "Test env2", etc. are the possible values. Choice parameter is to restrict the possible inputs.
Based on the value of %ENVIRONMENT% you can execute multiple pathways in your batch scripts or whichever scripts you are executing

How to store last value of parameter in parameterized job as a default value for next build in Jenkins?

I have been using Jenkins for a few weeks and I have one small problem. I can't find any plugin or solution for storing the last value of a parameter in a parametrized job as a default value for the next build.
For example:
My parameter takes build version (1.0.0.01) in the first build. In the next build it will be changed to 1.0.0.02, but I want to have a 1.0.0.01 in the default value field as a hint.
Does anybody have a solution or advice?
The Persistent Parameter Plugin is exactly what you are looking for!
You just need to download it from the official Jenkins repository and install it, no need for any additional setup.
Then on your job, you just need to add a "Persistent Parameter" in order to have default values used and saved between builds.
You can add a System groovy build step to your job (or maybe a post build Groovy step) using the Jenkins API to directly modify the project setting the default parameter value.
Here is some code that may be useful to get you started:
import hudson.model.*
paramsDef = build.getParent().getProperty(ParametersDefinitionProperty.class)
if (paramsDef) {
paramsDef.parameterDefinitions.each{ param ->
if (param.name == 'FOO') {
println("Changing parameter ${param.name} default value was '${param.defaultValue}' to '${param.defaultValue} BAR'")
param.defaultValue = "${param.defaultValue} BAR"
}
}
}
Have a look at the class ParameterDefinition in the Jenkins model.
You probably need to modify the default param value based on the current build executing. Some code to get that would look like this:
def thisBuildParamValue = build.buildVariableResolver.resolve('FOO')
The Extended Choice Parameter plugin provides this capability by using default parameter values from a properties file. A default parameter can be selected from a specified property key and this key can be programmatically modified in your current build. I would then use a groovy script in the current build to set the value of the default property key for the next build.
As an example you would have an Extended Choice Parameter whose default value is defined by a properties file version.properties with keys as follows:
versions=1.0.0.02, 1.0.0.01, 1.0.0.00
default.version=1.0.0.02
The parameter definition would include:
Property File=version.properties
Property Key=versions
Default Property File=version.properties
Default Property Key=default.versions
The GUI for your parameter in the next build would show a selection list with 1.0.0.02 selected by default. This feature is also very useful for pipeline builds where you would want the parameters of a downstream build stage to be set by an earlier build.
The only drawback to this approach might be that the parameter UI will be a drop-down selection. You may opt to have a single value in the versions property key so not to confuse your users.
Similar to thiagolr's answer, but for those of you using pipelines! It appears the persistent-parameter-plugin doesn't work for those using pipeline 2.0. But there is a patched version at https://github.com/ashu16815/persistent-parameter-plugin which seems to work for me.
Clone it locally:
git clone https://github.com/ashu16815/persistent-parameter-plugin.git
Build it:
mvn clean install
Install it in Jenkins:
1) Navigate to Jenkins > Manage Jenkins > Manage Plugins
2) Click Advanced tab
3) Scroll down to Upload Plugin
4) Click Choose file and select the persistent-parameter.hpi in the target directory of the maven build above
Now it should persist.

Resources