Using the Environment Directive in Jenkins Pipeline
📖 Scenario: You are setting up a Jenkins pipeline to automate a simple build process. You want to define environment variables that can be used throughout the pipeline stages.
🎯 Goal: Build a Jenkins pipeline script that defines environment variables using the environment directive and uses them in a stage.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block
Use the environment directive to define variables
Use the environment variables inside a stage
Print the environment variables in the console output
💡 Why This Matters
🌍 Real World
Defining environment variables in Jenkins pipelines helps manage configuration values like API keys, paths, or messages that can be reused across multiple build steps.
💼 Career
Understanding the environment directive is essential for Jenkins pipeline scripting, a common skill required for DevOps engineers and automation specialists.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with a pipeline block and an empty agent any section.
Jenkins
Hint
Start with pipeline { and add agent any inside it.
2
Add environment variables using the environment directive
Inside the pipeline block, add an environment directive that defines two variables: GREETING with value "Hello" and TARGET with value "World".
Jenkins
Hint
Use environment { GREETING = "Hello" TARGET = "World" } inside the pipeline.
3
Add a stage that uses the environment variables
Add a stage named Print Message with a steps block that runs a shell command to echo the message combining GREETING and TARGET variables.
Jenkins
Hint
Use stage('Print Message') { steps { sh 'echo "$GREETING $TARGET"' } } inside stages.
4
Print the environment variables in the console output
Add a post block with always that prints GREETING and TARGET using echo steps to confirm the variables are set.
Jenkins
Hint
Add a post { always { echo "Greeting is: ${GREETING}" echo "Target is: ${TARGET}" } } block after stages.
Practice
(1/5)
1. What is the main purpose of the environment directive in a Jenkins pipeline?
easy
A. To define variables that can be used throughout the pipeline steps
B. To specify the build agent or node where the pipeline runs
C. To set the pipeline's timeout duration
D. To declare the stages of the pipeline
Solution
Step 1: Understand the role of environment directive
The environment directive is used to set variables accessible in all pipeline steps.
Step 2: Compare with other pipeline directives
Other directives like agent set the build node, and stages define pipeline stages, not environment variables.
Final Answer:
To define variables that can be used throughout the pipeline steps -> Option A
Quick Check:
Environment directive = set variables [OK]
Hint: Environment directive sets variables for all steps [OK]
Common Mistakes:
Confusing environment with agent directive
Thinking environment sets pipeline stages
Assuming environment controls timeout
2. Which of the following is the correct syntax to set an environment variable GREETING with value Hello in a declarative Jenkins pipeline?
easy
A. environment { GREETING 'Hello' }
B. environment { GREETING: Hello }
C. environment { GREETING = Hello }
D. environment { GREETING = "Hello" }
Solution
Step 1: Recall environment variable syntax
In Jenkins declarative pipelines, environment variables are set using environment { VAR = "value" } with quotes around string values.
What will be the output when the Print stage runs?
medium
A. Build started
/usr/local/bin
B. $MESSAGE
$PATH
C. Build started
$PATH
D. Syntax error due to environment variables
Solution
Step 1: Understand environment variable usage
The environment directive sets MESSAGE and PATH variables accessible in steps.
Step 2: Check echo and shell commands
echo "$MESSAGE" prints the value of MESSAGE, which is "Build started". The shell command sh 'echo $PATH' prints the PATH variable value "/usr/local/bin".
Final Answer:
Build started
/usr/local/bin -> Option A
Quick Check:
Environment variables expand correctly in echo and sh [OK]
Hint: Environment variables expand in echo and shell commands [OK]
Common Mistakes:
Expecting literal variable names instead of values
Confusing shell and Groovy variable syntax
Assuming syntax error without cause
4. You wrote this environment block in your Jenkins pipeline:
environment {
SECRET_KEY = secret123
}
But the pipeline fails with a syntax error. What is the most likely cause?
medium
A. Using an invalid variable name SECRET_KEY
B. Environment block must be inside a stage
C. Missing quotes around the string value
D. SECRET_KEY cannot contain numbers
Solution
Step 1: Check environment variable value syntax
String values in environment variables must be quoted, e.g., SECRET_KEY = "secret123".
Step 2: Validate other options
Variable names with underscores and numbers are valid. Environment block can be at pipeline level. Numbers in variable names are allowed.
Final Answer:
Missing quotes around the string value -> Option C
Quick Check:
Quote string values in environment variables [OK]
Hint: Always quote string values in environment directive [OK]
Common Mistakes:
Forgetting quotes around strings
Thinking variable names can't have underscores
Placing environment block incorrectly
5. You want to set an environment variable VERSION dynamically based on a shell command output in a Jenkins declarative pipeline. Which approach correctly sets VERSION for use in later stages?
hard
A. Use environment { VERSION = sh(script: 'git describe --tags', returnStdout: true).trim() } directly in pipeline
B. Set VERSION inside a script block and use env.VERSION = ...
C. Declare VERSION as a parameter and assign shell output to it
D. Use environment { VERSION = "git describe --tags" } to run the command
The environment directive cannot run shell commands directly; it only sets static values.
Step 2: Use script block to assign dynamic value
Inside a script block, you can run shell commands and assign the output to env.VERSION for later use.
Step 3: Evaluate other options
Use environment { VERSION = sh(script: 'git describe --tags', returnStdout: true).trim() } directly in pipeline tries to run shell in environment block which is invalid. Declare VERSION as a parameter and assign shell output to it misuses parameters. Use environment { VERSION = "git describe --tags" } to run the command assigns command string literally.
Final Answer:
Set VERSION inside a script block and use env.VERSION = ... -> Option B
Quick Check:
Dynamic env vars need script block assignment [OK]
Hint: Use script block to set dynamic environment variables [OK]
Common Mistakes:
Trying to run shell commands directly in environment block
Confusing parameters with environment variables
Assigning command strings instead of output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.