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?