📖 Scenario: You are setting up a Jenkins pipeline to build a simple project. You want to use environment variables to store important values like the project name and build number. This helps keep your build script clean and easy to update.
🎯 Goal: Build a Jenkins pipeline script that defines environment variables and uses them in the build steps to print messages with those variables.
📋 What You'll Learn
Create environment variables in the Jenkins pipeline script
Use the environment variables inside the pipeline steps
Print the values of the environment variables during the build
💡 Why This Matters
🌍 Real World
Environment variables in Jenkins pipelines are used to store configuration values like project names, build numbers, and deployment targets. This makes builds flexible and easier to maintain.
💼 Career
Knowing how to use environment variables in Jenkins is essential for DevOps engineers and build managers to create reusable and configurable build pipelines.
Progress0 / 4 steps
1
Define environment variables in the pipeline
Create a Jenkins pipeline script with an environment block that defines two variables: PROJECT_NAME set to MyApp and BUILD_NUMBER set to 42.
Jenkins
Hint
Use the environment block inside the pipeline to define variables.
2
Use environment variables in a build step
Inside the Build stage's steps, add a script block that assigns a variable called message with the value Building project: ${PROJECT_NAME} - Build number: ${BUILD_NUMBER}.
Jenkins
Hint
Use def message = "Building project: ${PROJECT_NAME} - Build number: ${BUILD_NUMBER}" inside a script block.
3
Print the message variable
Still inside the script block, add a println(message) statement to print the message to the build log.
Jenkins
Hint
Use println(message) to print the message inside the script block.
4
Run the pipeline and check output
Run the Jenkins pipeline script and verify that the build log shows the line: Building project: MyApp - Build number: 42.
Jenkins
Hint
Look at the Jenkins build console output to see the printed message.
Practice
(1/5)
1. What is the main purpose of environment variables in Jenkins builds?
easy
A. To write the build script itself
B. To display the build logs on the screen
C. To store data like secrets or settings without changing the build code
D. To install Jenkins plugins automatically
Solution
Step 1: Understand environment variables role
Environment variables hold data such as secrets, settings, or dynamic values needed during the build without modifying the build code.
Step 2: Identify the correct purpose in Jenkins context
Jenkins uses environment variables to pass information to builds safely and flexibly.
Final Answer:
To store data like secrets or settings without changing the build code -> Option C
Quick Check:
Environment variables = store build data [OK]
Hint: Environment variables hold build data, not code or logs [OK]
Common Mistakes:
Confusing environment variables with build scripts
Thinking environment variables show logs
Assuming environment variables install plugins
2. Which syntax correctly defines an environment variable named VERSION with value 1.0 in a Jenkins pipeline?
But the build fails with an error about SECRET_KEY. What is the problem?
medium
A. You must quote the value: API_KEY = "$SECRET_KEY"
B. SECRET_KEY is not defined in Jenkins environment
C. You cannot use environment variables inside environment block directly
D. You should use single quotes instead of double quotes
Solution
Step 1: Analyze variable usage in environment block
The environment block tries to assign API_KEY the value of SECRET_KEY. This requires SECRET_KEY to be defined in Jenkins environment or injected.
Step 2: Identify cause of error
If SECRET_KEY is not defined anywhere, Jenkins cannot resolve it, causing the build to fail.
Final Answer:
SECRET_KEY is not defined in Jenkins environment -> Option B
Quick Check:
Undefined variables cause build errors [OK]
Hint: Ensure referenced env vars exist before using them [OK]
Common Mistakes:
Assuming quoting fixes undefined variable errors
Thinking environment block cannot use variables at all
Using single quotes which prevent variable expansion
5. You want to pass a dynamic build number as an environment variable BUILD_NUM to your Jenkins pipeline and print it. Which snippet correctly achieves this?
hard
A. environment { BUILD_NUM = "${env.BUILD_NUMBER}" }
B. environment { BUILD_NUM == "${env.BUILD_NUMBER}" }
C. environment { BUILD_NUM = env.BUILD_NUMBER; }
D. environment { BUILD_NUM: env.BUILD_NUMBER; }
Solution
Step 1: Recall correct syntax for environment variables with dynamic values
In Jenkins declarative pipelines, environment variables are defined with equals sign syntax and string values. To use a dynamic value like env.BUILD_NUMBER, it must be inside quotes and referenced with ${}.
Step 2: Identify correct option
environment { BUILD_NUM = "${env.BUILD_NUMBER}" } uses BUILD_NUM = "${env.BUILD_NUMBER}" which correctly sets the environment variable to the current build number.
Final Answer:
environment { BUILD_NUM = "${env.BUILD_NUMBER}" } -> Option A
Quick Check:
Use equals sign and ${} for dynamic env vars [OK]
Hint: Use equals sign and ${env.VAR} inside quotes for dynamic vars [OK]
Common Mistakes:
Using double equals instead of equals sign
Not quoting the dynamic variable
Omitting ${} around env variable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.