Dynamic parameter selection on jenkins - jenkins

I have a build/deploy job on jenkins, and user can select multiple items to be deployed with multi select parameter. I retrieve those values dynamically from db table with a groovy script.
Problem is that some of the variables should be linked. Meaning; when user selects item X, item A and item B also should be selected.
Is there a way of triggering a selection event on jenkins multi select? or should I use something else?
Thanks.

After spending some time I found a way to do this using Active Choices Plugin
1- I kept my initial extended choice parameter lets name it COMPONENTLIST
2- Then I created another paramateter as Active Choices Reactive Reference Parameter lets name it COMPONENT_IDS
2a- I added COMPONENTLIST as referenced parameter in COMPONENT_IDS. And set the Choice Type as Formatted HTML and also selected Omit value field
2b- I used following groovy script to collect initial selection and make modifications on it and returned as in step 2c
def output = COMPONENTLIST.split(',').collect{it as int}
2c- tricky part here! It is different how you pass the paramater to build stage. Following line helped me to pass COMPONENT_IDS to build.
output = output.join(",")
return "<b>${output}</b><input type=\"hidden\" name=\"value\" value=\"${output}\" />"

Related

Parameterized Jenkins job with dependent parameter

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.

Jenkins control on multiple choice parameter to list the apps

I have a requirement in Jenkins as below.
1) Select environment in choice parameter i.e. prod/staging/sit
2) Based on the above environment value, I need to run execute .SH file to fetch the list of deployed applications from my runtime.
3) Populate the retrieved data again in to a choice parameter and allow user to submit for any start/stop action.
Is it possible in Jenkins to trigger an action based on user selected value in choice parameter? something like AJAX....
it is possible in Jenkins to do a choice active based choices.
you need to add a parameter named "Active Choices Reactive Parameter"
in this parameter there is a field named "Referenced Parameter",
whenever this value is changed the Parameter get's reevaluated based on the changed value

Need to select multiple values as default dynamically in jenkins

In a prameter need to select multiple values as default dynamically in jenkins. Because need to run a scheduler on which multiple values should be selected as default. Any suggestion please.
You can use Extended Choice Parameter Plugin to select multiple valued for a variable.
After installing the plugin, in your project configuration page select This Project is Parameterized option and then from the drop-down choose Extended Choice Parameter.
Then add a name for your parameter and choose the parameter type as Multi Select. Then under Choose Source for Value section choose from where you want to take the vales for your parameter and save your job. In the following image I am providing the value in the job itself. You can choose alternate methods.
Now when you build your project you can select multiple values.
you can try to use source for default value
I would suggest Active Choices instead, using for example a simple checkbox configuration

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

Resources