Set environment variables in Jenkins

+1 vote

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
• 14,080 points

edited Apr 22 by Kalgi 16,576 views

10 answers to this question.

0 votes

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
• 37,430 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,100 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.
0 votes

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: java.net.InetAddress.getLocalHost().getHostName(),
        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

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
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"' > ~/load_env.sh
    echo "export AOEU=$(echo aoeu)" >> ~/load_env.sh
    chmod 750 ~/load_env.sh
    
  3. In Jenkins Build (Execute shell), invoke the script and its variables before anything else, e.g.

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

You can use groovy job file:

description('')
steps {
    environmentVariables {
        envs(PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true)
    }
}
answered Dec 10, 2018 by Galelio

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,900 points
1,379 views
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,240 points
251 views
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,810 points
140 views
+2 votes
5 answers
+13 votes
2 answers
0 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
• 37,430 points
794 views
+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
4,549 views