Set environment variables in Jenkins

+2 votes

Announcement! Career Guide 2019 is out now. Explore careers to become a DevOps Engineer!

I'm trying to make mu Jenkins evaluate a shell command and assign the output to an environment variable. Also, will it be possible to assign the executor of a job to an environment variable?

Aug 13, 2018 in Jenkins by Hannah
• 18,410 points

edited Apr 22, 2019 by Kalgi 69,946 views

11 answers to this question.

+1 vote

You can use the EnvInject plugin. This plugin makes it possible to have an isolated environment for your jobs.

Use the plugin in the following way:

  1. Create an "Execute shell" build step that runs:
  2. echo AOEU=$(echo aoeu) > propsfile
  3. Create an Inject environment variables build step and set "Properties File Path" to propsfile.
answered Aug 13, 2018 by Kalgi
• 51,950 points
0 votes

You can use environment injector plugin. First let me breif you about this plugin's properties:

  • Removes inherited environment variables by the Jenkins Java process
  • Injects environment variables at node (master/slave) startup
  • Executes a setup script before or/and after a SCM checkout for a run
  • Injects environment variables before or/and after a SCM checkout for a run
  • Injects environment variables as a build step for a run
  • Injects password values for a run
  • Exports environment variables at the end of the build in order to to know the set of environment variables used for each build
1) Install the plugin (manage jenkins-> manage pligins->install environment injector plugin)
2) Go to your job -> configure
3) Find Add build step in Build section and select Inject environment variables
4) Set the desired environment variable as VARIABLE_NAME=VALUE pattern. In my case, I changed value of USERPROFILE variable


answered Aug 13, 2018 by lina
• 8,220 points
0 votes

Add JMETER_HOME environment variable to be available via your Ant build scripts across all projects on your Jenkins server

answered Dec 10, 2018 by Omkar
Can you please explain a detail?
Hey! Check out @Omkar's, @Zoo's and @Neha's answer. It gives the entire explanation.
+1 vote

To add to @Omkar's answer, I would like to explain how to add the environment variable.

Manage Jenkins - Configure System - Global properties

answered Dec 10, 2018 by Zoo
0 votes

Adding to @Omkar's and @Zoo's answer, 

The environment variable is then available in Ant via:

<property environment="env" />
<property name="jmeter.home" value="${env.JMETER_HOME}" />

This can be verified to works by adding:

<echo message="JMeter Home: ${jmeter.home}"/>

Which produces:

JMeter Home: ~/.jmeter

answered Dec 10, 2018 by Neha
0 votes

By selecting Inject environment variables to the build process you will get:

  • Properties File Path
  • Properties Content
  • Script File Path

  • Script Content

  • and finally Evaluated Groovy script.

 Evaluated Groovy script gives you possibility to set environment variable based on result of executed command:

  • with execute method:
    return [HOSTNAME_SHELL: 'hostname'.execute().text, 
        DATE_SHELL: 'date'.execute().text,
        ECHO_SHELL: 'echo hello world!'.execute().text
  • or with explicit Groovy code:
    return [HOSTNAME_GROOVY:,
        DATE_GROOVY: new Date()
answered Dec 10, 2018 by Haseeb
0 votes

You can use the Build Env Propagator Plugin. This plugin lets you add new build environment variables, override a pre-defined and existing variables at each build step. Set of variables defined at a build step is carried over to the sub-sequent build steps. Any subsequent build step can override any variable defined by the previous build step.

answered Dec 10, 2018 by Krishti
+1 vote

You can try something like this

stages {
        stage('Build') {
            environment { 
                    AOEU= sh (returnStdout: true, script: 'echo aoeu').trim()
            steps {
                sh 'env'
                sh 'echo $AOEU'
answered Dec 10, 2018 by Celia
you made my day! Thanks!
0 votes

Follow these steps:

  1. Log-in as Jenkins: sudo su - jenkins or sudo su - jenkins -s /bin/bash
  2. Create a shell script, e.g.:

    echo 'export VM_NAME="$JOB-NAME"' > ~/
    echo "export AOEU=$(echo aoeu)" >> ~/
    chmod 750 ~/
  3. In Jenkins Build (Execute shell), invoke the script and its variables before anything else, e.g.

    source ~/
answered Dec 10, 2018 by Sushit
0 votes

You can use groovy job file:

steps {
    environmentVariables {
answered Dec 10, 2018 by Galelio
0 votes


I hope this information may help you-how to Set environment variables in Jenkins

This can be done via the EnvInject plugin in the following way:

  1. Create an "Execute shell" build step that runs:

    echo AOEU=$(echo aoeu) > propsfile
  2. Create an Inject environment variables build step and set "Properties File Path" props file


answered Sep 12 by SRI

Related Questions In Jenkins

+1 vote
2 answers

What possible return values are there in Jenkins plugin “Sonarqube scanner”

Use Webhooks in SonarQube Administration setup to ...READ MORE

answered Aug 3, 2018 in Jenkins by Nilesh
• 6,980 points
0 votes
1 answer

How can I create global macros and templates in jenkins job builder?

you can append the path to the ...READ MORE

answered Apr 2, 2018 in Jenkins by ajs3033
• 7,280 points
0 votes
1 answer

unable to compile java code in Jenkins

If the code is compiling and running ...READ MORE

answered Apr 25, 2018 in Jenkins by DareDev
• 6,850 points
+2 votes
5 answers
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
+2 votes
1 answer
0 votes
1 answer

Set a Jenkins environment variable based on a job parameter

So if you give choice parameter the ...READ MORE

answered Aug 14, 2018 in Jenkins by Kalgi
• 51,950 points
+2 votes
3 answers

Permission denied error while running a simple job in Jenkins

Hey @Hannah, try changing the permissions using ...READ MORE

answered Oct 25, 2018 in Jenkins by krishti