Operations 7 min read

Comprehensive Guide to Using Jenkins Pipeline Environment Variables

This article provides a detailed tutorial on Jenkins Pipeline environment variables, covering how to list, read, set, override, store boolean values, and capture shell command output within pipelines, complete with practical code examples and best‑practice tips.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Comprehensive Guide to Using Jenkins Pipeline Environment Variables

In this guide we focus on effectively using Jenkins Pipeline environment variables. You will learn how to define, update, and correctly use them in boolean expressions.

1. List Environment Variables

You can view all available variables either by opening ${YOUR_JENKINS_HOST}/env-vars.html on the Jenkins master or by running the printenv shell command.

pipeline {<br/>    agent any<br/>    stages {<br/>        stage("Env Variables") {<br/>            steps {<br/>                sh "printenv"<br/>            }<br/>        }<br/>    }<br/>}

Using printenv | sort can give you a sorted list.

2. Read Environment Variables

Access variables via the env object, e.g., env.BUILD_NUMBER. The short form BUILD_NUMBER works but may be confusing because it lacks the env. context.

pipeline {<br/>    agent any<br/>    stages {<br/>        stage("Env Variables") {<br/>            steps {<br/>                echo "The build number is ${env.BUILD_NUMBER}"<br/>                echo "You can also use ${BUILD_NUMBER}"<br/>                sh 'echo "I can access $BUILD_NUMBER in shell command as well."'<br/>            }<br/>        }<br/>    }<br/>}

3. Set Environment Variables

Declare variables declaratively with an environment {} block or imperatively with env.VAR = "value" or withEnv(["VAR=value"]) {}.

pipeline {<br/>    agent any<br/>    environment {<br/>        FOO = "bar"<br/>    }<br/>    stages {<br/>        stage("Env Variables") {<br/>            environment {<br/>                NAME = "Alan"<br/>            }<br/>            steps {<br/>                echo "FOO = ${env.FOO}"<br/>                echo "NAME = ${env.NAME}"<br/>                script { env.TEST_VARIABLE = "some test value" }<br/>                echo "TEST_VARIABLE = ${env.TEST_VARIABLE}"<br/>                withEnv(["ANOTHER_ENV_VAR=here is some value"]) {<br/>                    echo "ANOTHER_ENV_VAR = ${env.ANOTHER_ENV_VAR}"<br/>                }<br/>            }<br/>        }<br/>    }<br/>}

4. Override Environment Variables

Jenkins Pipeline allows overriding variables, but there are rules:

The withEnv(["env=value"]) block can override any variable.

Variables set in an environment {} block cannot be overridden by the imperative env.VAR = "value" syntax.

The imperative env.VAR = "value" can only override variables that were created imperatively.

Example Jenkinsfile demonstrating all three cases:

pipeline {<br/>    agent any<br/>    environment {<br/>        FOO = "bar"<br/>        NAME = "Joe"<br/>    }<br/>    stages {<br/>        stage("Env Variables") {<br/>            environment {<br/>                NAME = "Alan" // overrides pipeline level NAME<br/>                BUILD_NUMBER = "2" // overrides default BUILD_NUMBER<br/>            }<br/>            steps {<br/>                echo "FOO = ${env.FOO}" // prints "FOO = bar"<br/>                echo "NAME = ${env.NAME}" // prints "NAME = Alan"<br/>                echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" // prints "BUILD_NUMBER = 2"<br/>                script { env.SOMETHING = "1" } // creates env.SOMETHING<br/>            }<br/>        }<br/>        stage("Override Variables") {<br/>            steps {<br/>                script {<br/>                    env.FOO = "IT DOES NOT WORK!" // cannot override pipeline-level FOO<br/>                    env.SOMETHING = "2" // can override imperative variable<br/>                }<br/>                echo "FOO = ${env.FOO}" // prints "FOO = bar"
                echo "SOMETHING = ${env.SOMETHING}" // prints "SOMETHING = 2"
                withEnv(["FOO=foobar"]) { echo "FOO = ${env.FOO}" } // prints "FOO = foobar"
                withEnv(["BUILD_NUMBER=1"]) { echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" } // prints "BUILD_NUMBER = 1"
            }<br/>        }<br/>    }<br/>}

5. Store Boolean Values in Environment Variables

All environment variable values are stored as strings. To use a boolean false correctly in a conditional, call .toBoolean() on the string.

pipeline {<br/>    agent any<br/>    environment {<br/>        IS_BOOLEAN = false<br/>    }<br/>    stages {<br/>        stage("Env Variables") {<br/>            steps {<br/>                script {<br/>                    if (env.IS_BOOLEAN) { echo "This prints because the string \"false\" is truthy" }<br/>                    if (env.IS_BOOLEAN.toBoolean() == false) { echo "This prints because \"false\".toBoolean() is false" }<br/>                }<br/>            }<br/>        }<br/>    }<br/>}

6. Capture Shell Output into an Environment Variable

Use the sh(script: 'cmd', returnStdout: true) form to capture command output and assign it to an environment variable.

pipeline {<br/>    agent any<br/>    environment {<br/>        LS = "${sh(script:'ls -lah', returnStdout: true)}"<br/>    }<br/>    stages {<br/>        stage("Env Variables") {<br/>            steps {<br/>                echo "LS = ${env.LS}"<br/>            }<br/>        }<br/>    }<br/>}

The article concludes with a brief author bio, noting the author’s focus on DevOps practice and enterprise‑level automation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdDevOpsPipelineJenkinsEnvironment Variables
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.