0
0
Jenkinsdevops~10 mins

Environment directive in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Environment directive
Start Pipeline
Read environment directive
Set environment variables
Run pipeline steps with env vars
Pipeline completes
The pipeline starts, reads the environment directive to set variables, then runs steps using those variables.
Execution Sample
Jenkins
pipeline {
  environment {
    GREETING = 'Hello'
    TARGET = 'World'
  }
  stages {
    stage('Example') {
      steps {
        echo "${GREETING}, ${TARGET}!"
      }
    }
  }
}
This Jenkins pipeline sets two environment variables and prints a greeting using them.
Process Table
StepActionEnvironment Variables SetOutput
1Start pipelineNone yetNo output
2Read environment directiveGREETING='Hello', TARGET='World'No output
3Run stage 'Example' stepsGREETING='Hello', TARGET='World'Prints: Hello, World!
4Pipeline completesEnvironment variables clearedPipeline finished successfully
💡 Pipeline ends after all stages run with environment variables available
Status Tracker
VariableStartAfter Step 2After Step 3Final
GREETINGundefined'Hello''Hello'undefined
TARGETundefined'World''World'undefined
Key Moments - 2 Insights
Why do environment variables disappear after the pipeline finishes?
Environment variables set in the environment directive exist only during the pipeline run (see execution_table step 4). After completion, Jenkins cleans up the environment.
Can I use environment variables in all stages once set in the environment directive?
Yes, environment variables set in the environment directive are available in all stages and steps (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step are the environment variables first set?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check the 'Environment Variables Set' column in the execution_table rows
According to the variable tracker, what is the value of GREETING after step 3?
A'Hello'
B'World'
Cundefined
Dnull
💡 Hint
Look at the 'GREETING' row and 'After Step 3' column in variable_tracker
If you add a new variable in the environment directive, when will it be available?
AOnly in the first stage
BIn all stages and steps during the pipeline run
COnly after the pipeline finishes
DOnly in the last stage
💡 Hint
Refer to key_moments about variable availability and execution_table step 3
Concept Snapshot
Jenkins environment directive sets variables for the entire pipeline run.
Syntax: environment { VAR = 'value' }
Variables are available in all stages and steps.
They exist only during pipeline execution.
Use ${VAR} to access them in steps.
Full Transcript
This visual execution shows how Jenkins pipeline uses the environment directive. The pipeline starts with no environment variables. Then it reads the environment directive and sets GREETING to 'Hello' and TARGET to 'World'. These variables are available in all stages and steps, as shown when the example stage prints 'Hello, World!'. After the pipeline finishes, the environment variables are cleared. This helps keep variables scoped to the pipeline run only. Beginners often wonder why variables disappear after the run or if variables are available everywhere. This trace clarifies those points by showing variable states and pipeline steps.