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 vari
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}"
}
}
[...]
@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:
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
None of:
works (as of v1.656). You are able to read each of them, of course, but new values that are assigned to them are not available in subsequent build steps.
Hence, JediMasterCoder's answer and handling via file like Destroyica's are the only options so far.
We use inject environment variables plugin extensively and it works great. The solution is:
On top of what @Gurubaran suggested (which is what I'd do if had no other option), I'd just opt for joining the build steps to a single one, which will greatly simplify this need.
You will need to care for the error handling logic and exit conditions, but your environment will be solid!
I hope this helps.