Execute Build Step Based On Parameter Value - jenkins

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.

Related

Jenkins multi job project choice parameter

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.

Jenkins Pass custom variable to downstream jobs

I want to pass a custom variable from Job A to Job B. I have tried achieving this using the "Parameterized Trigger" plugin but I didnt work for me.
I am doing it the following way:
On Job A:
execute shell --> export VAR=1
echo $VAR --> is returning 1
Trigger parameterized build on other projects:
PARAM=${VAR}
On JobB:
I have selected this project is parameterized and declared a variable as PARAM. But when I do execute shell --> echo ${PARAM} it returns be ${VAR} instead of 1.
Am I missing anything here ? Any pointers please ? Thanks in advance!!
That's due to the lifetime of VAR is limited within the Execute shell step. If you want variable cross step, even cross from Build to Post Action, you can output the variable to a file in Key = Value pattern, then read it back in Parameterized Trigger
I found a neat way to pass custom variables to downstream job here:
https://sathishpk.wordpress.com/2016/03/01/how-to-passget-parameters-from-shell-command-into-jenkins-other-places/
It worked for me!!

Jenkins Addon in Jenkins Pipeline

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.

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 Conditional Step Check if ENV variable is set

How can I use the Conditional Step Jenkins plugin, to check if an environment variable is set? If it is set, proceed as normal. If it is not set, skip the post-build actions and mark build as a pass (not a failure).
Or if there is a way to do this without using the Conditional Step plugin, I would be open to that as well.
You could do this with the Flexible Publish plugin. Under Conditional action select Not, then under that select Strings match. Put your environment variable as String 1 and leave String 2 empty. Then add your post-build actions below.

Resources