Jenkins Pipeline - set and use environment variables

Jenkins Pipeline - set and use environment variables

How to list the environment variables available to Jenkins Pipeline

examples/jenkins/list_environment.Jenkinsfile

  1. pipeline {
  2. agent none
  3. environment {
  4. color = "blue"
  5. }
  6. stages {
  7. stage('first') {
  8. agent { label 'master' }
  9. steps {
  10. sh "printenv | sort"
  11. }
  12. }
  13. }
  14. }
  15.  

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

  1. pipeline {
  2. agent none
  3. environment {
  4. color = 'blue'
  5. }
  6. stages {
  7. stage('example') {
  8. agent { label 'master' }
  9. steps {
  10. script {
  11. def fields = env.getEnvironment()
  12. fields.each {
  13. key, value -> println("${key} = ${value}");
  14. }
  15.  
  16. println(env.PATH)
  17. }
  18. }
  19. }
  20. }
  21. }
  22.  

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

  1. pipeline {
  2. agent none
  3. environment {
  4. first_path = get_first()
  5. }
  6. stages {
  7. stage('example') {
  8. agent { label 'master' }
  9. steps {
  10. print(env.first_path)
  11. }
  12. }
  13. }
  14. }
  15.  
  16. def get_first() {
  17. node('master') {
  18. return env.PATH.split(':')[0]
  19. }
  20. }
  21.  

联系我们

联系电话

4000-640-466

联系邮箱

service@f-li.cn

办公地址

上海黄浦区外滩源1号

谢谢,您的信息已成功发送。
请填写信息。