Jenkinsfile per branch - Time & Space Complexity
When Jenkins runs pipelines using a Jenkinsfile per branch, it checks each branch separately.
We want to understand how the time to process grows as the number of branches increases.
Analyze the time complexity of the following Jenkins pipeline snippet that checks out code from each branch.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
def branches = ['main', 'dev', 'feature1', 'feature2']
for (branch in branches) {
checkout([$class: 'GitSCM', branches: [[name: "refs/heads/${branch}"]], userRemoteConfigs: [[url: 'https://repo.url']]])
// Run build steps for this branch
}
}
}
}
}
}
This code loops over a list of branches, checks out each branch, and runs build steps.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each branch to checkout and build.
- How many times: Once per branch in the list.
As the number of branches grows, the pipeline runs the checkout and build steps for each branch separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checkouts and builds |
| 100 | 100 checkouts and builds |
| 1000 | 1000 checkouts and builds |
Pattern observation: The total work grows directly with the number of branches.
Time Complexity: O(n)
This means the time to run the pipeline grows in a straight line as the number of branches increases.
[X] Wrong: "Checking out multiple branches happens all at once, so time stays the same."
[OK] Correct: Each branch checkout and build runs one after another, so time adds up with each branch.
Understanding how Jenkins pipelines scale with branches helps you design efficient CI/CD workflows.
What if we ran the branch builds in parallel instead of a loop? How would the time complexity change?