📖 Scenario: You are creating a Jenkins pipeline script to build and deploy a simple application. To make your pipeline flexible and easy to maintain, you want to avoid hard-coding values like the branch name and build environment directly in the script.
🎯 Goal: Build a Jenkins pipeline script that uses variables for branch name and environment instead of hard-coded values. This will help you reuse the pipeline for different branches and environments easily.
📋 What You'll Learn
Create variables for branch name and environment
Use these variables in the pipeline stages
Print the values of these variables during the build
Avoid any hard-coded strings for branch or environment in the pipeline steps
💡 Why This Matters
🌍 Real World
In real projects, hard-coded values make pipelines rigid and hard to update. Using variables allows teams to reuse pipelines for multiple branches and environments without changing the code.
💼 Career
DevOps engineers and Jenkins administrators often create pipelines that must work across many projects and environments. Knowing how to avoid hard-coded values is essential for writing maintainable and scalable CI/CD pipelines.
Progress0 / 4 steps
1
Create variables for branch and environment
Create two variables called branchName and environment and set them to "main" and "production" respectively.
Jenkins
Hint
Use def to declare variables in Jenkins scripted pipeline.
2
Use variables in the pipeline stages
Add a pipeline block with an agent any. Inside stages, create a stage called Build that echoes the message "Building branch: ${branchName}" using the branchName variable.
Jenkins
Hint
Use pipeline and stage blocks. Use echo to print messages.
3
Add a Deploy stage using the environment variable
Inside the existing pipeline block, add a new stage called Deploy that echoes the message "Deploying to environment: ${environment}" using the environment variable.
Jenkins
Hint
Add another stage block inside stages with echo step.
4
Print both variables in the pipeline
Add a final stage called Print Info inside the pipeline that echoes both branchName and environment in the format: "Branch: ${branchName}, Environment: ${environment}".
Jenkins
Hint
Use echo to print both variables in one message inside a new stage.
Practice
(1/5)
1. Why should you avoid hard-coded values in Jenkins pipelines?
easy
A. Because Jenkins does not support hard-coded values
B. Because hard-coded values run faster
C. To reduce the number of pipeline steps
D. To make pipelines flexible and easier to maintain
Solution
Step 1: Understand pipeline flexibility
Hard-coded values fix data in one place, making changes difficult and pipelines less reusable.
Step 2: Recognize benefits of environment variables
Using environment variables allows easy updates and reuse without changing pipeline code.
Final Answer:
To make pipelines flexible and easier to maintain -> Option D
Quick Check:
Flexibility = Avoid hard-coded values [OK]
Hint: Remember: environment variables make pipelines flexible [OK]
Common Mistakes:
Thinking hard-coded values improve speed
Believing Jenkins disallows hard-coded values
Confusing fewer steps with better practice
2. Which syntax correctly uses an environment variable MY_VAR in a Jenkins pipeline script?
easy
A. echo 'Value is $MY_VAR'
B. echo "Value is ${MY_VAR}"
C. echo "Value is %MY_VAR%"
D. echo "Value is MY_VAR"
Solution
Step 1: Identify correct variable syntax in Groovy
Jenkins pipeline scripts use Groovy, where environment variables are accessed as ${VAR_NAME} inside strings.
Step 2: Compare options
echo "Value is ${MY_VAR}" uses ${MY_VAR}, which is the correct Groovy syntax for environment variables in strings.
Final Answer:
echo "Value is ${MY_VAR}" -> Option B
Quick Check:
Groovy env var syntax = ${VAR} [OK]
Hint: Use ${VAR} to access env variables in Jenkins Groovy scripts [OK]