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
Related
I have a multi job project which accepts some parameters and one of them is a choice parameter, because
I'm new to Jenkins it is defined manually through UI and without using groovy.
When the parameters selected or passed, there is a single build that will run for the defined parameters.
I would like to apply some changes and achieve the following behavior:
Execute this same multi job project with all parameters per selected options in the choice parameter.
e.g If selected 2 options in the choice parameter - It will trigger the build twice sequentially or in parallel, some sort of a loop with the parameters it received.
I tried to get some information regarding this online but because I'm not familiar with the proper terminology to search for, all I get is groovy scripts or answers which not related to what I need.
How I can achieve this?
Thanks in advance.
After additional search I came with the following solution:
change the Choice parameter to Extended choice parameter to allow multiple selection.
Create another job that will do the following:
a. Parse the received parameters from the Extended Choice parameter using shell script with the delimiter that is set in the 'Extended choice parameter options'
b. Execute build on the desired job from a loop by using API and passing the relevant parameters.
echo $OPTIONS
IFS=',' read -ra options_array <<< "$OPTIONS"
for option in "${options_array[#]}"
do
echo $option
curl -X POST "https://<user>:<password>#<jenkins_host>/job/<job_name>/buildWithParameters?parameter=${option}"
sleep 5
done
In case there are no free executors - increase the number of executors to allow multiple job executions
Edit job configuration that needs to be executed multiple times and enable the 'Execute concurrent builds if necessary' option.
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.
I have a parameterized project. With the variable VAR1.
I'm using the the Xray for JIRA Jenkins Plugin for Jenkins. There you can fill four parameters:
JIRA Instance
Issues
Filter
File Path
I'm new to Jenkins but what I have learned so far, that you can't fill this fields with environment variables. Something like
Issues: ${VAR1} - doesn't work.
So I thought I can do this with a pipeline. When I click on Pipeline Syntax and chose step: General Build Step I can choose Xray: Cucumber Features Export Task. Then I fill the fields with my environment variable and click Generate Pipeline Script The output is as follows:
step <object of type com.xpandit.plugins.xrayjenkins.task.XrayExportBuilder>
That doesn't work. What I'm doing wrong?
All you're doing is OK, but what you want is not supported by Jenkins whether it is pipeline or not, since the parameters' load is happening prior to the pipeline-flow or the definition of the ${VAR1}.
You can try to overcome this by defining the 'Issues' value as a pipeline internal value instead of a parameter and base it on the ${VAR1} value.
If it must be a parameter, use 2 jobs where one defines the value of 'Issues' based on a the ${VAR1} and pass it to the other job that gets the 'Issues' as a fixed value.
I have a Jenkins job in which I have defined 2 Execute Shell Build Steps (call them Step1 and Step2. I also have as part of this job a Choice Parameter with 2 choices (for the sake of this example let's also call those values Step1 and Step2). What I am trying to do is if the user selects Step1 as the choice parameter then the job would only execute the Step1 build step, if user selects Step2 it would only select the Step2 build step. I was thinking that I could put a groovy step in front of these to control this behavior but not sure how to construct this. I could also just define a single choice parameter and condition such that if choice = step1 then execute step1 else execute step2......still not sure how to construct that syntax. I'll keep trying a few different things but if anyone has done this any assistance would be appreciated.
From Parameterized Build:
The parameter are available as environment parameters variables. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.
[Correction by me.]
Use the environment variable defined in your Choice Parameter in two Conditional BuildSteps, one for each of your steps.
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