Passing data between build steps in Jenkins
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=timestampTIME_LAPSED=$END-$START
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}" } }[...]
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.