Set environment variables in Jenkins

+3 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,550 points

edited Apr 22, 2019 by Kalgi 90,801 views

12 answers to this question.

+2 votes
Best answer

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

selected Dec 14, 2020 by MD
you made my day! Thanks!
+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
• 52,310 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
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, 2020 by SRI
0 votes


You can try with the below code.

environment {
    SOME_NAME = "some value"

Then it can be accessed using the following syntax:

pipeline {
   agent none
   environment {
       field = 'some'
   stages {
       stage ('Preparation') {
           agent { label 'master'}
           environment {
               JENKINS_PATH = sh(script: 'pwd', , returnStdout: true).trim()
           steps {
               echo "Hello world"
               echo "PATH=${JENKINS_PATH}"
               sh 'echo "JP=$JENKINS_PATH"'
answered Dec 14, 2020 by MD
• 95,180 points

Related Questions In Jenkins

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
• 52,310 points
0 votes
1 answer

How to set the Sender address in Jenkins?

Hi@akhtar, In Jenkins, we need to set the ...READ MORE

answered Oct 24, 2020 in Jenkins by MD
• 95,180 points
0 votes
1 answer

How to set permissions for a user in Jenkins?

Hi@akhtar, You can give access to your user ...READ MORE

answered Dec 6, 2020 in Jenkins by MD
• 95,180 points
+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
• 7,020 points
+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
+2 votes
5 answers
0 votes
1 answer

sonar XML plugin installation failure in jenkins

You can try this link which SonarQube XML ...READ MORE

answered Jun 20, 2018 in Jenkins by Atul
• 10,240 points