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

14,491

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.

Share:
14,491
Sreevalsa E
Author by

Sreevalsa E

...

Updated on June 24, 2022

Comments

  • Sreevalsa E
    Sreevalsa E almost 2 years

    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 ?