Operations 9 min read

Master Jenkins Pipeline Environment Variables: From Basics to Advanced Tricks

This tutorial walks you through setting up Jenkins with Docker, exploring built‑in and custom pipeline environment variables, overriding rules, Boolean handling, and capturing shell command output, providing practical code examples for each step.

Programmer DD
Programmer DD
Programmer DD
Master Jenkins Pipeline Environment Variables: From Basics to Advanced Tricks

Introduction

Jenkins is a core DevOps tool; pipelines rely on environment variables. This guide explains built‑in, custom, and overridden variables and shows a practical Docker setup.

Setup Jenkins with Docker

Run a Jenkins container in one command:

docker container run --rm -p 8080:8080 -p 50000:50000 --name=jenkins -v $(pwd):/var/jenkins_home jenkins/jenkins

Open localhost:8080 in a browser, obtain the admin password, install suggested plugins, and create a Pipeline job.

Viewing Built‑in Environment Variables

Two ways:

Visit ${YOUR_JENKINS_HOST}/env-vars.html (e.g., http://localhost:8080/env-vars.html) to see a table of variables.

Execute printenv in a stage and view the console output.

pipeline{
    agent any
    stages{
        stage("Env Variables"){
            steps{
                sh "printenv"
            }
        }
    }
}

Reading Variables in a Pipeline

Use env.VAR_NAME or ${VAR_NAME}. Example with BUILD_NUMBER:

pipeline{
    agent any
    stages{
        stage("Read Env Variables"){
            steps{
                echo "With env: ${env.BUILD_NUMBER}"
                echo "Without env: ${BUILD_NUMBER}"
                sh 'echo "shell read: $BUILD_NUMBER"'
            }
        }
    }
}

Defining Custom Environment Variables

Declarative pipelines support an environment block at the top level or inside a stage. Scripted pipelines can assign env.KEY = "value" or use withEnv([...]).

pipeline{
    agent any
    environment{
        FOO = "bar"
    }
    stages{
        stage("Custom Env Variables"){
            environment{
                NAME = "RGYB"
            }
            steps{
                echo "FOO = ${env.FOO}"
                echo "NAME = ${env.NAME}"
                script{
                    env.SCRIPT_VARIABLE = "Thumb Up"
                }
                echo "SCRIPT_VARIABLE = ${env.SCRIPT_VARIABLE}"
                withEnv(["WITH_ENV_VAR=Come On"]){
                    echo "WITH_ENV_VAR = ${env.WITH_ENV_VAR}"
                }
            }
        }
    }
}

Overriding Variables

Three rules: withEnv([...]) can override any variable.

Variables defined in environment {} cannot be overridden by env.key = "value".

Script‑defined variables can only be overridden by other script‑defined variables.

Using Boolean Values

Jenkins stores values as strings; convert with toBoolean() for proper logical checks.

pipeline{
    agent any
    environment{
        IS_BOOLEAN = false
    }
    stages{
        stage("Env Variables"){
            steps{
                script{
                    if (env.IS_BOOLEAN){
                        echo "Hello"
                    }
                    if (env.IS_BOOLEAN.toBoolean() == false){
                        echo "Condition met"
                    }
                    if (!env.IS_BOOLEAN.toBoolean()){
                        echo "RGYB"
                    }
                }
            }
        }
    }
}

Assigning Shell Results to Variables

Capture command output with sh(script: 'cmd', returnStdout: true).trim() and store it in an environment variable.

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

Conclusion

Understanding Jenkins environment variables—built‑in, custom, overridden, Boolean handling, and shell result assignment—covers most real‑world scenarios.

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.

Dockerci/cdDevOpsPipelineGroovyJenkinsEnvironment Variables
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.