0
0
Jenkinsdevops~20 mins

Environment variables access in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing Environment Variables in Jenkins Pipeline
📖 Scenario: You are setting up a Jenkins pipeline to automate a build process. You need to access environment variables inside the pipeline script to customize the build steps.
🎯 Goal: Build a Jenkins pipeline script that accesses and prints specific environment variables.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Access the environment variable BUILD_NUMBER
Access the environment variable MY_VAR
Print both environment variables using echo steps
💡 Why This Matters
🌍 Real World
Jenkins pipelines often need environment variables to customize builds, pass secrets, or configure deployment settings.
💼 Career
Understanding environment variables in Jenkins is essential for DevOps engineers to automate and manage CI/CD pipelines effectively.
Progress0 / 4 steps
1
Create a basic Jenkins pipeline skeleton
Write a Jenkins pipeline script starting with a pipeline block and a agent any directive.
Jenkins
Need a hint?

Start by writing pipeline { and inside it add agent any to run on any available agent.

2
Add an environment block to define environment variables
Inside the pipeline block, add an environment block that defines a variable MY_VAR with the value hello.
Jenkins
Need a hint?

Use the environment block inside pipeline and define MY_VAR = 'hello'.

3
Access and print environment variables in a stage
Add a stage named 'Print Env' with a steps block that uses echo to print BUILD_NUMBER and MY_VAR environment variables using env.BUILD_NUMBER and env.MY_VAR.
Jenkins
Need a hint?

Inside stages, add a stage('Print Env') with steps that use echo to print env.BUILD_NUMBER and env.MY_VAR.

4
Print the environment variables output
Run the pipeline script and ensure the console output shows the lines Build number is: followed by the build number and My variable is: hello.
Jenkins
Need a hint?

Run the pipeline and check the console output for the expected lines.