Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variable from a job and use it in next job in jenkins?

I want to have a job to set the environment variable and use these environment variables in all the next jobs. How can I set environment variable through Jenkins ?

like image 873
Sreevalsa E Avatar asked Sep 30 '15 05:09

Sreevalsa E


1 Answers

Technically, you can't pass env variables from one job to the next, and I'm not aware of a plugin to do that out of the box.

There is a technique however. The idea is to create a properties file in the first job (e.g. exported.properties), add that file to the job artifacts, and then import this file via the EnvInject plugin in the second job.

This pre-supposes that you have some link between the first and second job, which is typically achieved with the Copy Artifact plugin, but a number of workflow-like plugins can help you as well.

For example, for creating the properties file, add a step "Execute shell", with e.g.

echo "# Saving some version properties
BUILD_VERSION=${BuildVersion}
BUILD_NODE_NAME=${NODE_NAME}
SOURCE_JOB=${JOB_NAME}
" > ${WORKSPACE}/BuildVersion.properties

Of course, you can use other build steps, e.g. Windows shell, groovy script, etc... each with their specific syntax of course.

like image 121
Patrice M. Avatar answered Sep 28 '22 05:09

Patrice M.