📖 Scenario: You are managing a Jenkins pipeline that deploys applications to different environments based on the branch name. For example, code from the main branch should deploy to production, while code from the develop branch should deploy to staging.
🎯 Goal: Build a Jenkins pipeline script that checks the branch name and conditionally deploys to the correct environment.
📋 What You'll Learn
Create a variable to hold the branch name
Add a variable to define the production branch name
Use conditional logic to decide deployment environment
Print the deployment environment based on the branch
💡 Why This Matters
🌍 Real World
In real projects, deployment pipelines must decide where to deploy code based on the branch or tag. This prevents accidental production deployments.
💼 Career
DevOps engineers often write Jenkins pipelines with conditional steps to automate safe and efficient deployments.
Progress0 / 4 steps
1
Set the branch name variable
Create a variable called branchName and set it to the string develop.
Jenkins
Hint
Use def branchName = 'develop' to create the variable.
2
Define the production branch name
Create a variable called productionBranch and set it to the string main.
Jenkins
Hint
Use def productionBranch = 'main' to define the production branch.
3
Add conditional deployment logic
Use an if statement to check if branchName equals productionBranch. If yes, set a variable deployEnv to production. Otherwise, set deployEnv to staging.
Jenkins
Hint
Use if (branchName == productionBranch) { ... } else { ... } to set deployEnv.
4
Print the deployment environment
Write a println statement to display the text Deploying to: followed by the value of deployEnv.
Jenkins
Hint
Use println("Deploying to: ${deployEnv}") to print the deployment environment.
Practice
(1/5)
1. What is the main purpose of using a when block in a Jenkins pipeline?
easy
A. To define environment variables for the pipeline
B. To run a stage only if certain conditions are met
C. To specify the agent where the pipeline runs
D. To declare pipeline parameters
Solution
Step 1: Understand the role of when in Jenkins
The when block controls whether a stage runs based on conditions.
Step 2: Compare options with when usage
Only To run a stage only if certain conditions are met describes conditional execution, which matches when functionality.
Final Answer:
To run a stage only if certain conditions are met -> Option B
Quick Check:
when controls stage execution = A [OK]
Hint: Remember: when means "only if" for stages [OK]
Common Mistakes:
Confusing when with environment setup
Thinking when sets agents
Assuming when declares parameters
2. Which of the following is the correct syntax to run a stage only on the 'main' branch in a declarative Jenkins pipeline?
easy
A. when { branch is 'main' }
B. when { branch == 'main' }
C. when { branch 'main' }
D. when { branch equals 'main' }
Solution
Step 1: Recall correct when syntax for branch condition
The correct syntax uses branch 'main' inside when block.
Step 2: Check each option's syntax
Only when { branch 'main' } matches Jenkins declarative syntax; others use invalid expressions.
Final Answer:
when { branch 'main' } -> Option C
Quick Check:
Correct branch syntax = C [OK]
Hint: Use branch 'name' exactly inside when [OK]
Common Mistakes:
Using comparison operators inside when
Writing invalid expressions like 'branch =='
Confusing Groovy syntax with declarative syntax
3. Given this Jenkins pipeline snippet, what will happen if the branch is 'develop'?
stage('Deploy') {
when {
branch 'main'
}
steps {
echo 'Deploying to production'
}
}
medium
A. The stage will be skipped and nothing prints
B. The stage will run and print 'Deploying to production'
C. The pipeline will fail with a syntax error
D. The stage will run but print nothing
Solution
Step 1: Analyze the when condition
The stage runs only if branch is 'main'. Here branch is 'develop', so condition fails.
Step 2: Determine stage behavior on condition failure
When when condition fails, the stage is skipped and no steps run.
Final Answer:
The stage will be skipped and nothing prints -> Option A
Quick Check:
Branch mismatch skips stage = A [OK]
Hint: If branch doesn't match when, stage skips silently [OK]
Common Mistakes:
Assuming stage runs anyway
Expecting error on branch mismatch
Thinking stage runs but prints nothing
4. Identify the error in this Jenkins pipeline snippet that tries to deploy only if environment variable DEPLOY is 'true':
Step 1: Check when syntax for environment variable
The correct syntax uses environment name: 'DEPLOY', value: 'true' or expression blocks, not direct comparison.
Step 2: Identify invalid usage
Using environment DEPLOY == 'true' is invalid and causes syntax error.
Final Answer:
The environment condition is invalid syntax -> Option D
Quick Check:
Invalid environment condition syntax = D [OK]
Hint: Use correct when { environment name: 'VAR', value: 'val' } syntax [OK]
Common Mistakes:
Writing direct comparisons inside when
Placing when inside steps
Misusing stage names
5. You want to deploy only if the branch is 'release' and the environment variable DEPLOY is 'yes'. Which when block correctly implements this in a declarative Jenkins pipeline?
hard
A. when { allOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } }
B. when { branch 'release' && environment DEPLOY == 'yes' }
C. when { branch 'release' environment DEPLOY == 'yes' }
D. when { anyOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } }
Solution
Step 1: Understand combining conditions in when
Use allOf to require both conditions true simultaneously.
Step 2: Check syntax for branch and environment conditions
when { allOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } } correctly uses allOf with branch 'release' and environment name: 'DEPLOY', value: 'yes'.
Step 3: Identify errors in other options
Options A and C use invalid syntax; D uses anyOf which means either condition, not both.
Final Answer:
when { allOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } } -> Option A
Quick Check:
Use allOf for AND conditions = B [OK]
Hint: Use allOf to combine multiple when conditions [OK]
Common Mistakes:
Using anyOf instead of allOf
Writing invalid combined expressions
Misusing environment condition syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.