Passing data between build steps in Jenkins
Solution 1
One thing remains between shells: the workspace.
Simple and stupid solution: use file(s)!
Huge additional advantage: it works when you split your job in several jobs and use the Clone Workspace plugin
Build Step #1 - Execute shell
START=timestamp
...
echo $START > env_start.txt
...
Build Step #3 - Execute Shell
START=`cat env_start.txt`
END=timestamp
TIME_LAPSED=$END-$START
Solution 2
If you are using the declarative pipeline syntax defining a variable in the environment
section and using a script
step to set its value may be useful.
I'm doing something like this with a declarative pipeline and it works for passing a variable (both inside one stage and between stages):
pipeline {
agent any
environment {
variable = ''
}
stages {
stage('Some stage') {
steps {
script {
if (some condition){
variable = 'some value'
} else { variable = 'else value' }
}
sh '${somepath}/bin/script ' +
"-parameter=${variable}"
}
}
}
stage('Dummy print') {
steps {
sh "echo ${variable}"
}
}
[...]
Solution 3
We use inject environment variables plugin extensively and it works great. The solution is:
- Set your variable myenv=value1
- print to file in workspace: echo "myenv=$myenv" > tmp.myenv
- Inject after every change: Use envinject to read environment from file tmp.myenv -> myenv is now known as part of the job environment.
Solution 4
@Gurubaran's solution works for me, you need to install "Environment Injector" plugin in jenkins, then
Step1: use shell/powershell/windows batch/etc. to create generate a properties(key=value) file. for example: the file path is $WORKSPACE/env.properties.
Step2: Add an "Inject environment variables" component and set "Properties File Path" to $WORKSPACE/env.properties
After Step2: You can use these environment variables in the following steps.
Example:
Solution 5
One way to work with Jenkins variables is to use jenkins-cli.jar
in the build step, it takes some work but this will add FOO=1
in the parameters list, since it's running in a build step it knows which build to set the parameter for.
java -jar ${JENKINS_HOME}/war/WEB-INF/jenkins-cli.jar -s ${JENKINS_URL} set-build-parameter FOO 1
Related videos on Youtube
jcol
Updated on June 07, 2020Comments
-
jcol almost 4 years
I'd like to do something along the lines of:
This is overly simple and just demonstrates what I'd like to do. Basically, I want to be able to store and access variables within a single job scope between multiple build steps. Also, I can get around by storing the data to a file and reading it later, but I'd like something easier and less 'hacky'
Build Step #1 - Execute shell
$START=timestamp
Build Step #2 - Run another job
Build Step #3 - Execute Shell
$END=timestamp TIME_LAPSED=$END-$START (post lapsed time somewhere)
-
jcol about 10 yearsDoes this give me the ability to do inject during the numerous build steps? Ie can I dynamically set these at the time the data is available. Remember, I'm only showing start/stop times as an example. There are many things I want to do beyond just this.
-
jcol about 10 yearsEhh, this doesn't solve my problem. I have external jobs between my shell build steps. In the example given above, I want to calculate dynamic factors at different points of the build. Having numerous jobs allows me to independently run various steps as well as version contorl them individually for different reasons.
-
Gurubaran about 10 years@jcol : I do not think you can inject the env variables after you have set them in the "Build Environment" Phase. But you would be able to manipulate them when the data becomes available.
-
warvariuc over 8 yearsIs this a built-in functionality? Or a plugin is needed? Edit: this plugin is needed: wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin
-
Gerold Broser about 8 yearsI had the same idea – and even more, I tried it. It doesn't work. See my answer.
-
Gerold Broser about 8 yearsNice solution! It works if the user has overall-read, job-read and run-update permissions.
-
Szymon Sadło over 6 yearsWorked like a charm. Thanks!
-
beka about 5 yearsYou can also in Stage steps set or update a env variable with env.myvar=„new-value“
-
Sasha Bond almost 5 years
set-build-parameter
does not work anymore... looks deprecated -
Patrick Szalapski over 4 yearsThis only works for strings. Of course, you can represent nearly anything as a string but it isn't that convenient.
-
JRichardsz about 3 years2021 april - plugins.jenkins.io/envinject - The current version of this plugin contains a vulnerability: Exposure of sensitive build variables stored by EnvInject 1.90 and earlier
-
Jean Bob about 2 yearsIf you use the env variable in conditions, i.e. check if defined or empty, you might want to use the echo option
-n
to prevent having a file filled with a blank line:echo -n $START > env_start.txt
then in step 3if [ -s env_start.txt ]; then echo "Unset" ; fi