0
0
Jenkinsdevops~10 mins

Environment variables in builds in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Environment variables in builds
Start Build
Load Global Env Vars
Load Job-Specific Env Vars
Execute Build Steps
Use Env Vars in Scripts
Build Completes
This flow shows how Jenkins loads environment variables globally and per job, then uses them during build steps.
Execution Sample
Jenkins
pipeline {
  agent any
  environment {
    GREETING = 'Hello'
  }
  stages {
    stage('Build') {
      steps {
        echo "${GREETING}, Jenkins!"
      }
    }
  }
}
A Jenkins pipeline that sets an environment variable GREETING and prints it during the build.
Process Table
StepActionEnvironment Variables LoadedCommand ExecutedOutput
1Start Build{}N/ABuild started
2Load Global Env Vars{"PATH":"/usr/bin","JAVA_HOME":"/usr/lib/jvm"}N/AGlobal vars loaded
3Load Job-Specific Env Vars{"PATH":"/usr/bin","JAVA_HOME":"/usr/lib/jvm","GREETING":"Hello"}N/AJob vars loaded
4Execute Build Step{"PATH":"/usr/bin","JAVA_HOME":"/usr/lib/jvm","GREETING":"Hello"}echo "${GREETING}, Jenkins!"Hello, Jenkins!
5Build Completes{"PATH":"/usr/bin","JAVA_HOME":"/usr/lib/jvm","GREETING":"Hello"}N/ABuild finished successfully
💡 Build completes after executing all steps with environment variables available.
Status Tracker
VariableStartAfter Global LoadAfter Job LoadFinal
PATHundefined/usr/bin/usr/bin/usr/bin
JAVA_HOMEundefined/usr/lib/jvm/usr/lib/jvm/usr/lib/jvm
GREETINGundefinedundefinedHelloHello
Key Moments - 3 Insights
Why does GREETING only appear after job-specific env vars load?
Because GREETING is defined in the job environment block, it is not available until the job-specific variables load step (see execution_table row 3).
Can global environment variables be overwritten by job-specific ones?
Yes, job-specific environment variables can overwrite global ones. The variable tracker shows the final values after job load (see variable_tracker).
Why does the echo command print 'Hello, Jenkins!'?
Because the GREETING variable is set to 'Hello' before the build step runs, so the echo command expands it correctly (see execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of GREETING at step 2?
AHello
B/usr/bin
Cundefined
DJenkins
💡 Hint
Check the 'Environment Variables Loaded' column at step 2 in the execution_table.
At which step does the build print the greeting message?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the 'Command Executed' and 'Output' columns in the execution_table.
If the GREETING variable was removed from the job environment, what would the echo output be at step 4?
A", Jenkins!"
B"Hello, Jenkins!"
C"${GREETING}, Jenkins!"
D"undefined, Jenkins!"
💡 Hint
Consider what happens when a variable is not defined but referenced in a shell echo command.
Concept Snapshot
Jenkins loads environment variables globally first, then job-specific ones.
Variables set in the environment block are available during build steps.
Use ${VAR_NAME} syntax to access variables in shell commands.
Job variables can overwrite global variables.
Environment variables help customize builds without changing code.
Full Transcript
This visual execution shows how Jenkins handles environment variables during a build. First, the build starts with no variables. Then global environment variables like PATH and JAVA_HOME load. Next, job-specific variables such as GREETING are added. During the build step, the echo command uses the GREETING variable to print a message. Finally, the build completes successfully with all variables available. The variable tracker confirms how variables appear and change. Key points include when variables become available and how they affect commands. The quiz tests understanding of variable values at different steps and the effect of missing variables.