Branch-specific pipeline behavior in Jenkins - Time & Space Complexity
We want to understand how the time to run a Jenkins pipeline changes when it behaves differently for each branch.
How does the pipeline's work grow as the number of branches or steps changes?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
if (env.BRANCH_NAME == 'main') {
echo 'Run full build'
} else {
echo 'Run quick build'
}
}
}
}
}
}
This pipeline runs a full build on the main branch and a quick build on other branches.
Look for loops or repeated steps that affect time.
- Primary operation: The build steps inside the conditional run once per pipeline execution.
- How many times: Once per pipeline run, no loops or recursion here.
The pipeline runs one set of steps depending on the branch name.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 branch | Runs either full or quick build once |
| 10 branches | Runs 10 pipelines, each with one build step set |
| 100 branches | Runs 100 pipelines, each with one build step set |
Pattern observation: Each pipeline run does a fixed amount of work depending on branch, so time per run stays the same. Total work grows linearly with number of branches if all run.
Time Complexity: O(1)
This means each pipeline run takes a fixed amount of time regardless of branch, because it chooses one path to run.
[X] Wrong: "The pipeline runs both full and quick builds every time, so time doubles."
[OK] Correct: The pipeline uses a condition to run only one build per run, so it does not do both builds each time.
Understanding how conditional steps affect pipeline time helps you explain efficient build setups and manage resources well.
What if the pipeline ran both full and quick builds for every branch? How would the time complexity change?
Practice
Which Jenkins pipeline syntax runs a stage only on the main branch?
Solution
Step 1: Understand Jenkins branch condition syntax
Jenkins useswhen { branch 'branchName' }to run stages on specific branches.Step 2: Match syntax to run on
The correct syntax ismainbranchwhen { branch 'main' }to target the main branch.Final Answer:
when { branch 'main' } -> Option DQuick Check:
Branch condition = when { branch 'main' } [OK]
- Using if statements instead of 'when' block
- Incorrect syntax like 'onlyIf' or inline conditions
- Confusing branch name quotes or missing quotes
Which snippet correctly uses Jenkins declarative pipeline syntax to run a stage only on branches dev or feature/*?
stage('Test') {
when {
???
}
steps {
echo 'Running tests'
}
}Solution
Step 1: Identify correct syntax for multiple branch conditions
Jenkins usesanyOf { branch 'name'; branch 'pattern' }to run if any branch matches.Step 2: Choose syntax matching either 'dev' or 'feature/*'
UsinganyOf { branch 'dev'; branch 'feature/*' }runs the stage on either branch.Final Answer:
anyOf { branch 'dev'; branch 'feature/*' } -> Option AQuick Check:
Multiple branches = anyOf with branch conditions [OK]
- Using logical operators like || or && directly inside when
- Using allOf which requires all conditions true
- Incorrect syntax without braces or semicolons
Given this Jenkins pipeline snippet, what will be the output if the branch is feature/login?
pipeline {
agent any
stages {
stage('Build') {
when { branch 'main' }
steps { echo 'Building main branch' }
}
stage('Test') {
when { branch 'feature/*' }
steps { echo 'Testing feature branch' }
}
}
}Solution
Step 1: Check branch conditions for each stage
'Build' runs only on 'main' branch; 'Test' runs on branches matching 'feature/*'.Step 2: Match current branch 'feature/login'
'feature/login' matches 'feature/*', so only 'Test' stage runs and prints 'Testing feature branch'.Final Answer:
Testing feature branch -> Option AQuick Check:
Branch 'feature/login' matches 'feature/*' only [OK]
- Assuming 'main' stage runs on all branches
- Ignoring wildcard pattern matching
- Expecting both stages to run always
Identify the error in this Jenkins pipeline snippet that tries to run a stage only on the release branch:
stage('Deploy') {
when {
branch release
}
steps {
echo 'Deploying release branch'
}
}Solution
Step 1: Review syntax for branch condition in when block
The branch name must be a string inside quotes:branch 'release'.Step 2: Identify missing quotes causing syntax error
Here,branch releaselacks quotes, causing a syntax error.Final Answer:
Missing quotes around branch name in when condition -> Option CQuick Check:
Branch names need quotes in when { branch 'name' } [OK]
- Omitting quotes around branch names
- Placing steps outside stage block
- Misplacing branch condition inside steps
You want a Jenkins pipeline stage to run on all branches except main and develop. Which when condition correctly implements this?
Solution
Step 1: Understand negation of multiple branches
To exclude branches, wrap the branches inanyOfand negate withnot.Step 2: Apply correct syntax for excluding 'main' and 'develop'
not { anyOf { branch 'main'; branch 'develop' } }runs on all branches except these two.Final Answer:
not { anyOf { branch 'main'; branch 'develop' } } -> Option BQuick Check:
Exclude branches = not { anyOf { ... } } [OK]
- Using allOf with negations incorrectly
- Using logical operators like && directly
- Misplacing not inside anyOf instead of outside
Ready to prove it?
Pass the test and this pattern moves unknown
