Discover how a simple directive can save you hours of debugging and wasted builds!
Why Stage conditions with when directive in Jenkins? - Purpose & Use Cases
Imagine you have a Jenkins pipeline with many stages, and you want some stages to run only under certain conditions, like only on a specific branch or when a file changes.
Without stage conditions, you have to write complex scripts inside each stage to check these conditions manually.
Manually checking conditions inside stages makes your pipeline scripts long and confusing.
It's easy to make mistakes, like running stages when you don't want to or skipping important ones.
This slows down your builds and wastes resources.
The when directive lets you declare conditions clearly and simply right at the stage level.
Jenkins then automatically decides whether to run the stage or skip it, keeping your pipeline clean and efficient.
stage('Build') { steps { script { if (env.BRANCH_NAME == 'main') { // build steps } } } }
stage('Build') { when { branch 'main' } steps { // build steps } }
You can create smarter pipelines that run exactly what you need, saving time and avoiding errors.
For example, only run deployment stages when code is merged into the main branch, and skip them for feature branches automatically.
Manual condition checks clutter pipeline scripts and cause errors.
The when directive simplifies conditional stage execution.
This leads to cleaner, faster, and more reliable pipelines.
Practice
when directive in a Jenkins pipeline stage?Solution
Step 1: Understand the role of
Thewhendirectivewhendirective is used to decide if a stage should run or be skipped based on conditions.Step 2: Compare with other stage directives
Other directives likeenvironment,agent, andtimeoutserve different purposes unrelated to conditional execution.Final Answer:
To control whether a stage runs based on specified conditions -> Option DQuick Check:
whencontrols stage execution [OK]
when decides if stage runs or skips [OK]- Confusing
whenwithenvironment - Thinking
whensets agent or timeout - Assuming
whenalways runs the stage
when directive?Solution
Step 1: Recall correct
The correct syntax useswhensyntax for branch conditionbranch 'branchName'insidewhenblock without operators or colons.Step 2: Identify syntax errors in other options
Options B and C use invalid operators or expressions; D uses colon which is incorrect in this context.Final Answer:
when { branch 'main' } -> Option AQuick Check:
Usebranch 'name'syntax [OK]
branch 'name' without operators in when [OK]- Using comparison operators inside
when - Adding colons or equal signs incorrectly
- Writing
ifstatements insidewhen
stage('Deploy') {
when {
branch 'main'
}
steps {
echo 'Deploying to production'
}
}Solution
Step 1: Understand the
The stage runs only if the branch is 'main'. Since the branch is 'feature', the condition is false.whenconditionStep 2: Determine stage behavior on false condition
Whenwhencondition is false, Jenkins skips the stage and does not execute steps.Final Answer:
The stage will be skipped and nothing will print -> Option BQuick Check:
Branch mismatch skips stage [OK]
- Assuming stage runs regardless of branch
- Expecting syntax error for valid
when - Thinking stage runs but steps don't print
when:
stage('Test') {
when {
branch == 'dev'
}
steps {
echo 'Running tests'
}
}Solution
Step 1: Check
Thewhensyntax for branch conditionwhendirective expectsbranch 'name'without operators like '=='.Step 2: Validate other parts of the stage
Missingagentis allowed if inherited;stepsmust be outsidewhen; stage name is valid.Final Answer:
Using '==' operator insidewhenis invalid syntax -> Option CQuick Check:
No operators inwhenbranch condition [OK]
when branch check [OK]- Using comparison operators inside
when - Placing
stepsinsidewhen - Assuming stage name restrictions
DEPLOY is set to 'true'. Which when directive correctly implements this?Solution
Step 1: Understand combining conditions in
To require both conditions, usewhenallOfblock combiningbranchandenvironment.Step 2: Analyze each option's logic
when { allOf { branch 'release' environment name: 'DEPLOY', value: 'true' } }correctly usesallOf.when { branch 'release' && environment name: 'DEPLOY', value: 'true' }uses invalid syntax with '&&'.when { branch 'release' environment 'DEPLOY': 'true' }uses invalid syntax forenvironmentcondition (missingname:andvalue:).when { anyOf { branch 'release' environment name: 'DEPLOY', value: 'true' } }usesanyOf, meaning either condition suffices.Final Answer:
when { allOf { branch 'release' environment name: 'DEPLOY', value: 'true' } } -> Option AQuick Check:
UseallOfto combine multiplewhenconditions [OK]
allOf to require all conditions true [OK]- Using '&&' inside
whenblock - Using environment
'VAR': 'value'shorthand syntax - Using
anyOfwhen all conditions are needed
Ready to prove it?
Pass the test and this pattern moves unknown
