Jenkins Pass custom variable to downstream jobs - jenkins

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!!

Related

Jenkins: having problems passing environment variable for use in another job (maybe a bug)

I seem to have found a bug with trying to pass environment variables from one Jenkins job to another.
I have a Jenkins job which contains a Powershell build step. My question is not about the Powershell script, as that does exactly what I want (it goes to Artifactory, finds a list of all the builds and then gets the build number of the latest one). The script ends up with the Artifactory build number as a text string '$LATEST_BUILD_NO_SLASH' (for clarity, this is not the Jenkins build number). This is eventually stored as an environment variable called 'LATEST_BUILD_NUM_VAL'
This is definitely creating an environment variable with my value stored in it, as it can be seen in the 'Environment Variables' list.
This environment variable is passed in the standard way in the parameterized build step.
My issue is that when I use this environment variable in a downstream build having passed it using 'LATEST_BUILD_NUM = ${LATEST_BUILD_NUM_VAL}', I get '${LATEST_BUILD_NUM_VAL}' as the value passed to the downstream job:
But, if I pass a Jenkins created environment variable i.e.'LATEST_BUILD_NUM = ${JOB_BASE_NAME}' I get the correct variable in the downstream job:
I have spent all day banging my head around this and don't really know where to go from here. I seem to be creating the environment variable correctly, as it is in the environment variables list and it works if I use a standard environment variable. I have declared 'LATEST_BUILD_NUM' as a parameter in my downstream build.
Is there any other way of achieving what I am trying to do?
I have checked in the 'Jenkins Issues' log for issues with parameterised builds and I can't find anything similar to my issue.
In case it is of any relevance, the Jenkins Environment Injector plugin is v2.1.6 and the Parameterized Trigger plugin is v2.35.2.
This is easy to achieve in Jenkins Pipeline:
Your second job (JobB) is called from your first job (JobA) as a downstream job. Thus somewhere, (probably the end of your JobA pipeline) you will have:
build job: 'CloudbeeFolder1/Path/To/JobB', propagate: false, wait: false, parameters: [[$class: 'StringParameterValue', name: 'MY_PARAM', value: "${env.SOME_VALUE}"]]
Then in JobB on the "other side" you have:
environment {
PARAM_FROM_PIPELINE = "${params.MY_PARAM}"
}
This gets the value of your parameter into an environment variable in JobB.
in first job in post build action--> Trigger Parameterized build on other projects select this
In that project build --> give name of downward jobname
select Add parameter in that add Predefined parameter give parameter in key value format e.g Temp=${BUILD_ID}
In second job select project is parameterized in select any option e.g string parameter and put name as Temp and used this parameter in shell or anywhere as $Temp ...

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.

Reference groovy variables over multiple script parts in jenkins

In a Jenkins job, I have a groovy script, that is split in two parts. It does something before SCM and does some other thing at the end of the job as last build instruction.
Now, I need to access a variable in the second part, that I set in the first part.
How to do that?
I tried to mark a variable as a field with #Field Boolean myFlag = false, but still myFlag cannot be accessed in the second part of the script.
Interpreter says:
groovy.lang.MissingPropertyException: No such property: myFlag for class: Script1
Does anyone know how to accomplish accessing a variable from the first part of the script in the second?
Thanks!
you can pass these variables as the Jenkins Job's parameters. That way you have access to the variables throughout.
It was not clear from your question if you're running a single script in the job or if you're executing multiple scripts.

How to select the parameter coming from script output and pass it to next script or job in Bamboo?

I am trying to do below things in bamboo.I have script which basically shows the branches of hg repository.I know there is a plugin in Jenkins where you can run the script and get the output and use that output as a parameter to the job/script but I am not sure how to achieve this in Bamboo.Is there any plugin or some way to achieve this?
script abc.py which gives the below output
a
b
c
d
e
f
g
I want this script will run and it will give me the above output and then I can select anyone of them and pass it to my script.The value of the createBranch should come from the script so I can select any one of them
With Bamboo, it is impossible to create/change a variable value from within tasks due to concurrency issues it might create. There is a feature request open for this.
As a workaround, how about writing the script result out to a text file, then read it in from the file using Inject Variables plugin task. You can use this as a variable within the same stage to create a branch in your case.
Hope that helps.

Execute Build Step Based On Parameter 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.

Resources