# Using Environment Variables in Jenkins

In 
Published 2022-12-03

This tutorial explains to you how we can use environment variables in Jenkins.

First of all I want to highlight that a prerequisite for this article is Create Declarative Pipeline tutorial.

Now we modify the Jenkinsfile in GitHub. The new Jenkinsfile will be like this:

pipeline {
    agent any
    
    environment { 
        MY_ENV_VAR1 = "Hello 1"
        
        // Using returnStdout
        MY_ENV_VAR2 = """${bat(
                returnStdout: true,
                script: 'echo "clang"'
            )}"""
            
       // Using returnStatus
        MY_ENV_VAR3 = """${bat(
                returnStatus: true,
                script: 'exit 1'
            )}"""
            
    }

    stages {
        stage('Build') {
            environment { 
               MY_ENV_VAR4 = 'Hello 4'
            }
            steps {
                echo 'Building..'
                echo "MY_ENV_VAR1= ${env.MY_ENV_VAR1}"
                echo "MY_ENV_VAR2= ${env.MY_ENV_VAR2}"
                echo "MY_ENV_VAR3= ${env.MY_ENV_VAR3}"
                echo "MY_ENV_VAR4= ${env.MY_ENV_VAR4}"
                
                echo "JAVA_HOME= ${env.JAVA_HOME}"
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

When we run the pipeline, we will get:

We go to the execution output, and we can see:

Building..
[Pipeline] echo
MY_ENV_VAR1= Hello 1
[Pipeline] echo
MY_ENV_VAR2= 
C:\ProgramData\Jenkins\.jenkins\workspace\DeclarativePipeline>echo "clang" 
"clang"

[Pipeline] echo
MY_ENV_VAR3= 1
[Pipeline] echo
MY_ENV_VAR4= Hello 4
[Pipeline] echo
JAVA_HOME= C:\MySOFT\Java\jdk-11.0.2

Conclusions:

  • the environment variables could be set up at pipeline or stage level
  • we can read environment variables which are set up before the pipeline is started
  • we can run an operating system script and get the output of it using returnStdout
  • we can run an operating system script and get the exit status of it using returnStatus

Here are some environment variables we can have access at:

BUILD_ID The current build ID, identical to BUILD_NUMBER for builds created in Jenkins versions 1.597+

BUILD_NUMBER The current build number, such as "153"

BUILD_TAG String of jenkins-{JOB_NAME}-. Convenient to put into a resource file, a jar file, etc for easier identification

BUILD_URL The URL where the results of this build can be found.

EXECUTOR_NUMBER The unique number that identifies the current executor (among executors of the same machine) performing this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1

JAVA_HOME If your job is configured to use a specific JDK, this variable is set to the JAVA_HOME of the specified JDK. When this variable is set, PATH is also updated to include the bin subdirectory of JAVA_HOME

JENKINS_URL Full URL of Jenkins (NOTE: only available if Jenkins URL set in "System Configuration")

JOB_NAME Name of the project of this build, such as "foo" or "foo/bar".

NODE_NAME The name of the node the current build is running on. Set to 'master' for the Jenkins controller.

WORKSPACE The absolute path of the workspace