Branch discovery configuration in Jenkins - Time & Space Complexity
When Jenkins scans branches in a repository, it runs operations to find all branches to build.
We want to understand how the time it takes grows as the number of branches increases.
Analyze the time complexity of the branch discovery step in Jenkins pipeline configuration.
properties([
pipelineTriggers([])
])
checkout scm
This Jenkinsfile is used in a multibranch pipeline configured with BranchDiscoveryTrait (strategyId: 1) before checking out code.
Look for repeated actions that depend on the number of branches.
- Primary operation: Scanning each branch in the repository to check if it matches the discovery criteria.
- How many times: Once for every branch present in the repository.
As the number of branches grows, Jenkins must check each one during discovery.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 branch checks |
| 100 | 100 branch checks |
| 1000 | 1000 branch checks |
Pattern observation: The work grows directly with the number of branches.
Time Complexity: O(n)
This means the time to discover branches increases linearly as the number of branches grows.
[X] Wrong: "Branch discovery time stays the same no matter how many branches exist."
[OK] Correct: Jenkins must check each branch, so more branches mean more work and longer time.
Understanding how Jenkins handles branch discovery helps you explain build times and scaling in real projects.
"What if Jenkins cached branch information instead of scanning every time? How would the time complexity change?"
Practice
What is the main purpose of branch discovery in Jenkins?
Solution
Step 1: Understand branch discovery concept
Branch discovery is designed to find all branches automatically in a source code repository.Step 2: Identify the purpose in Jenkins
Jenkins uses branch discovery to keep builds updated without manual intervention.Final Answer:
To automatically find and build all branches in a repository -> Option CQuick Check:
Branch discovery = automatic branch finding [OK]
- Thinking branch discovery requires manual branch selection
- Confusing branch discovery with branch deletion
- Assuming branch discovery merges branches
Which strategyId value in Jenkins branch discovery configuration finds all branches including PRs?
Solution
Step 1: Recall strategyId meanings
In Jenkins, strategyId 3 means discover all branches including pull requests.Step 2: Match strategyId to description
StrategyId 1 is for only branches, 2 is for branches and PRs from origin, 3 includes all branches and PRs, 4 is for PRs only.Final Answer:
3 -> Option DQuick Check:
strategyId 3 = all branches + PRs [OK]
- Confusing strategyId 1 with including PRs
- Using strategyId 4 which only finds PRs
- Assuming strategyId 2 includes PRs
Given this Jenkins branch discovery snippet:
{"strategyId": 1}Which branches will Jenkins discover?
Solution
Step 1: Understand strategyId 1 meaning
StrategyId 1 means discover only branches, excluding pull requests.Step 2: Apply to the snippet
The snippet uses strategyId 1, so Jenkins finds only normal branches, not PRs.Final Answer:
Only branches that are not pull requests -> Option AQuick Check:
strategyId 1 = branches only [OK]
- Assuming strategyId 1 includes PRs
- Thinking strategyId 1 finds no branches
- Confusing strategyId 1 with strategyId 3
You configured Jenkins with {"strategyId": 5} for branch discovery. What is the likely result?
Solution
Step 1: Check valid strategyId values
Valid strategyId values are 1, 2, 3, and 4. 5 is invalid.Step 2: Predict Jenkins behavior on invalid strategyId
Jenkins will likely throw an error or ignore the invalid value, causing no discovery.Final Answer:
Jenkins throws a configuration error or ignores the setting -> Option BQuick Check:
Invalid strategyId = error or ignore [OK]
- Assuming strategyId 5 works like 3 or 4
- Expecting Jenkins to discover branches anyway
- Ignoring configuration errors
You want Jenkins to build only branches that start with feature/ and ignore others. Which branch discovery configuration should you use?
Solution
Step 1: Choose strategyId for branches only
StrategyId 1 discovers only branches (excluding PRs), suitable for filtering branches.Step 2: Apply branch filter for prefix
Adding a filter for branches starting with 'feature/' limits builds to those branches only.Final Answer:
Use {"strategyId": 1} with an additional branch filter for feature/* -> Option AQuick Check:
strategyId 1 + filter = feature branches only [OK]
- Using strategyId 3 without filters builds all branches and PRs
- Disabling filters causes all branches to build
- Using strategyId 4 only finds PRs, not branches
Ready to prove it?
Pass the test and this pattern moves unknown
