Branch discovery configuration in Jenkins - Mini Project: Build & Apply
scm block using git and set the url to 'https://github.com/example/repo.git'.Use checkout with GitSCM and set the repository URL exactly as given.
traits list inside checkout configuration with a BranchDiscoveryTrait to enable branch discovery.Add traits: [[$class: 'BranchDiscoveryTrait']] to enable branch discovery.
script step inside the Setup stage that prints the current branch name using env.BRANCH_NAME.Use script { echo("Current branch is: ${env.BRANCH_NAME}") } to print the branch name.
print equivalent in Jenkins pipeline by using echo to display the message "Current branch is: " followed by the branch name stored in env.BRANCH_NAME.Use echo("Current branch is: ${env.BRANCH_NAME}") to print the branch name.
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
