Stage conditions with when directive in Jenkins - Time & Space Complexity
We want to understand how the time it takes to run a Jenkins pipeline changes when using the when directive in stages.
Specifically, we ask: How does adding conditions affect the number of operations Jenkins performs?
Analyze the time complexity of the following Jenkins pipeline snippet using when conditions.
pipeline {
agent any
stages {
stage('Build') {
when {
branch 'main'
}
steps {
echo 'Building...'
}
}
stage('Test') {
when {
expression { return params.RUN_TESTS }
}
steps {
echo 'Testing...'
}
}
}
}
This pipeline runs stages only if their when conditions are true, controlling execution flow.
Look for repeated checks or evaluations in the pipeline.
- Primary operation: Evaluating
whenconditions before each stage runs. - How many times: Once per stage, so the number of stages determines how many condition checks happen.
As the number of stages increases, Jenkins checks each stage's when condition once.
| Input Size (n = number of stages) | Approx. Operations (condition checks) |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of condition checks grows directly with the number of stages.
Time Complexity: O(n)
This means the time to evaluate stage conditions grows linearly with the number of stages in the pipeline.
[X] Wrong: "Adding more when conditions won't affect pipeline run time much because they are simple checks."
[OK] Correct: Each when condition is checked once per stage, so more stages mean more checks, increasing total time linearly.
Understanding how conditional checks scale helps you design efficient pipelines and shows you can reason about execution costs in automation tasks.
What if we added nested when conditions inside stages? How would the time complexity change?
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
