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?
Practice
Jenkinsfile in each branch?Solution
Step 1: Understand Jenkinsfile per branch concept
Each branch can have its own Jenkinsfile, allowing different pipeline steps or settings per branch.Step 2: Compare options
It allows customizing the pipeline for each branch independently. correctly states the benefit. Options A, B, and D describe incorrect behaviors.Final Answer:
It allows customizing the pipeline for each branch independently. -> Option AQuick Check:
Separate Jenkinsfiles = branch-specific pipelines [OK]
- Thinking one Jenkinsfile applies to all branches
- Assuming Jenkinsfile absence disables pipeline
- Believing pipelines merge automatically
Solution
Step 1: Identify correct syntax for branch checkout
Using thegitstep withbranchandurlis the simplest correct way to checkout a specific branch.Step 2: Analyze options
The snippet usinggit branch: 'main', url: 'https://repo.git'is correct. The snippet usingcheckout([$class: 'GitSCM', branches: ['refs/heads/feature'], userRemoteConfigs: [[url: 'https://repo.git']]])has invalid syntax forbranches(expects list of maps like[[name: 'refs/heads/feature']]). The snippet usingscript { checkout scm }checks out the current SCM without specifying a branch. The snippet usingload 'Jenkinsfile'loads a Groovy script but does not checkout a branch.Final Answer:
pipeline { agent any; stages { stage('Build') { steps { git branch: 'main', url: 'https://repo.git' } } } } -> Option BQuick Check:
Use git step with branch and url [OK]
- Using load to get Jenkinsfile instead of checkout
- Not specifying branch in checkout
- Confusing checkout scm with git step
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
if (env.BRANCH_NAME == 'main') {
echo 'Deploying main branch'
} else {
echo 'Deploying feature branch'
}
}
}
}
}
}What will Jenkins print if the branch is named
feature/login?Solution
Step 1: Understand branch name condition
The script checks ifenv.BRANCH_NAMEequals 'main'. If yes, it prints 'Deploying main branch', else 'Deploying feature branch'.Step 2: Apply branch name 'feature/login'
Since the branch is 'feature/login', it does not equal 'main', so the else branch runs printing 'Deploying feature branch'.Final Answer:
Deploying feature branch -> Option CQuick Check:
Branch name check leads to else output [OK]
- Assuming partial match triggers main branch output
- Confusing env.BRANCH_NAME with other variables
- Expecting no output if branch is not main
feature branch but the pipeline fails with error: groovy.lang.MissingPropertyException: No such property: env for class. What is the likely cause?Solution
Step 1: Understand env variable usage
In declarative pipelines,envvariables must be accessed inside ascriptblock to use Groovy expressions.Step 2: Analyze error cause
The error indicatesenvis not recognized, likely because it is used outside ascriptblock where Groovy code runs.Final Answer:
The env variable is used outside a script block. -> Option AQuick Check:
Use env only inside script blocks [OK]
- Using env directly in steps without script
- Assuming missing pipeline block causes env error
- Not committing Jenkinsfile to branch
main branch but customize pipeline steps for feature/* branches without creating Jenkinsfiles in each feature branch. Which approach is best?Solution
Step 1: Understand single Jenkinsfile with branch conditions
You can usewhen { branch 'pattern' }in declarative pipelines to run stages only on matching branches.Step 2: Evaluate options
Usewhen { branch 'feature/*' }conditions inside the Jenkinsfile to run different stages. uses branch conditions inside one Jenkinsfile, which fits the requirement. Create symbolic links to the main Jenkinsfile in each feature branch. is not practical or supported. Use separate Jenkinsfiles in each feature branch and merge them manually. contradicts the goal of single Jenkinsfile. Configure Jenkins to ignore feature branches. ignores feature branches, not customizing them.Final Answer:
Use when { branch 'feature/*' } conditions inside the Jenkinsfile to run different stages. -> Option DQuick Check:
Branch conditions enable single Jenkinsfile customization [OK]
- Trying to link Jenkinsfiles across branches
- Creating multiple Jenkinsfiles despite single file goal
- Ignoring feature branches instead of customizing
Ready to prove it?
Pass the test and this pattern moves unknown
