0
0
Jenkinsdevops~30 mins

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

Choose your learning style9 modes available
Environment variables in builds
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Look at the Jenkins build console output to see the printed message.