Jenkins Pipeline - set and use environment variables
How to list the environment variables available to Jenkins Pipeline
examples/jenkins/list_environment.Jenkinsfile
pipeline { agent none environment { color = "blue" } stages { stage('first') { agent { label 'master' } steps { sh "printenv | sort" } } } }
In this example we list the environment variables using the printenv command of Unix/Linux which we pipe through the Unix sort command so we'll see the environment variables in a sorted list.
We invoke it using the sh command of the Jenkins Pipeline.
Before we do that we set a new variable called "color" in the environment section of the Jenkins Pipeline.
On Unix/Linux:
sh('printenv | sort')
On Windows you could run:
bat('set')
The output will looks something like this:
BUILD_DISPLAY_NAME=#18 BUILD_ID=18 BUILD_NUMBER=18 BUILD_TAG=jenkins-list_environment_variables-18 BUILD_URL=http://localhost:8080/job/list_environment_variables/18/ DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/110/bus EXECUTOR_NUMBER=1 HOME=/var/lib/jenkins HUDSON_COOKIE=40cd788e-91bd-4ebd-86c9-c10333fa27a9 HUDSON_HOME=/var/lib/jenkins HUDSON_SERVER_COOKIE=912830efeb6e2316 HUDSON_URL=http://localhost:8080/ JENKINS_HOME=/var/lib/jenkins JENKINS_NODE_COOKIE=dbf878e6-0ae5-4ffe-a32c-aa7876f975ce JENKINS_SERVER_COOKIE=durable-f6e3ca8e5d2310d4d5695d128db1ea2f JENKINS_URL=http://localhost:8080/ JOB_BASE_NAME=list_environment_variables JOB_DISPLAY_URL=http://localhost:8080/job/list_environment_variables/display/redirect JOB_NAME=list_environment_variables JOB_URL=http://localhost:8080/job/list_environment_variables/ LANG=C.UTF-8 LOGNAME=jenkins MAIL=/var/mail/jenkins NODE_LABELS=master NODE_NAME=master PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin PWD=/var/lib/jenkins/workspace/list_environment_variables RUN_CHANGES_DISPLAY_URL=http://localhost:8080/job/list_environment_variables/18/display/redirect?page=changes RUN_DISPLAY_URL=http://localhost:8080/job/list_environment_variables/18/display/redirect SHELL=/bin/bash SHLVL=1 STAGE_NAME=example USER=jenkins WORKSPACE=/var/lib/jenkins/workspace/list_environment_variables XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop XDG_RUNTIME_DIR=/run/user/110 XDG_SESSION_ID=c1 _=/usr/bin/daemon color=blue
List the environment variables without running the shell
The env object is of type class org.jenkinsci.plugins.workflow.cps.EnvActionImpl.
examples/jenkins/list_environment_internally.Jenkinsfile
pipeline { agent none environment { color = 'blue' } stages { stage('example') { agent { label 'master' } steps { script { def fields = env.getEnvironment() fields.each { key, value -> println("${key} = ${value}"); } println(env.PATH) } } } } }
We use the getEnvironment method that returns a hudson.EnvVars object. This is a Groovy map so we can already use the each method to go over the keys and values.
This however only seem to list the internal variables of Jenkins and for example PATH was not in the list even though we can access it as env.PATH.
How to set environment variables in Jenkins?
As also seen above, a section called environment can be added to the pipeline
environment { SOME_NAME = "some value" }
Then it can be accessed using the following syntax:
env.SOME_NAME
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"' } } } }
How to use environment variables in the environment section of Jenkins?
environment { PATH = "/path/to/dir:${env.PATH}" JNK_PATH = "${env.WORKSPACE}\\subdir" }
The above only works when the environment section is inside a "stage" but would yield "null" for WORKSPACE outside the stages.
Set environment executing code
environment { field = func() } def func() { ... return "value" }
A working example:
examples/jenkins/set_environment_by_script.Jenkinsfile
pipeline { agent none environment { first_path = get_first() } stages { stage('example') { agent { label 'master' } steps { print(env.first_path) } } } } def get_first() { node('master') { return env.PATH.split(':')[0] } }

Published on 2019-02-15