0
0
Jenkinsdevops~30 mins

Environment directive in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Add a post { always { echo "Greeting is: ${GREETING}" echo "Target is: ${TARGET}" } } block after stages.