What if your pipeline could know exactly what to do for each branch without you lifting a finger?
Why Branch-specific pipeline behavior in Jenkins? - Purpose & Use Cases
Imagine you have a project with many branches like 'main', 'develop', and 'feature'. You want to run different tests or deploy steps depending on which branch you work on. Doing this by hand means changing pipeline settings every time you switch branches.
Manually updating pipelines for each branch is slow and easy to forget. You might run the wrong tests or deploy unfinished code. This causes bugs, delays, and stress for the team.
Branch-specific pipeline behavior lets you write one pipeline that automatically changes what it does based on the branch. This means less work, fewer mistakes, and faster feedback.
if branch == 'main': run_deploy() else: run_tests()
pipeline {
agent any
stages {
stage('Deploy') {
when {
branch 'main'
}
steps {
sh 'deploy'
}
}
}
}You can safely automate different workflows for each branch, making your development faster and more reliable.
On the 'develop' branch, you run full tests but no deploy. On 'main', you run tests and deploy to production automatically. This keeps your releases smooth and your team confident.
Manual pipeline changes for branches are slow and risky.
Branch-specific behavior automates workflows based on branch names.
This leads to faster, safer, and more efficient development.
Practice
Which Jenkins pipeline syntax runs a stage only on the main branch?
Solution
Step 1: Understand Jenkins branch condition syntax
Jenkins useswhen { branch 'branchName' }to run stages on specific branches.Step 2: Match syntax to run on
The correct syntax ismainbranchwhen { branch 'main' }to target the main branch.Final Answer:
when { branch 'main' } -> Option DQuick Check:
Branch condition = when { branch 'main' } [OK]
- Using if statements instead of 'when' block
- Incorrect syntax like 'onlyIf' or inline conditions
- Confusing branch name quotes or missing quotes
Which snippet correctly uses Jenkins declarative pipeline syntax to run a stage only on branches dev or feature/*?
stage('Test') {
when {
???
}
steps {
echo 'Running tests'
}
}Solution
Step 1: Identify correct syntax for multiple branch conditions
Jenkins usesanyOf { branch 'name'; branch 'pattern' }to run if any branch matches.Step 2: Choose syntax matching either 'dev' or 'feature/*'
UsinganyOf { branch 'dev'; branch 'feature/*' }runs the stage on either branch.Final Answer:
anyOf { branch 'dev'; branch 'feature/*' } -> Option AQuick Check:
Multiple branches = anyOf with branch conditions [OK]
- Using logical operators like || or && directly inside when
- Using allOf which requires all conditions true
- Incorrect syntax without braces or semicolons
Given this Jenkins pipeline snippet, what will be the output if the branch is feature/login?
pipeline {
agent any
stages {
stage('Build') {
when { branch 'main' }
steps { echo 'Building main branch' }
}
stage('Test') {
when { branch 'feature/*' }
steps { echo 'Testing feature branch' }
}
}
}Solution
Step 1: Check branch conditions for each stage
'Build' runs only on 'main' branch; 'Test' runs on branches matching 'feature/*'.Step 2: Match current branch 'feature/login'
'feature/login' matches 'feature/*', so only 'Test' stage runs and prints 'Testing feature branch'.Final Answer:
Testing feature branch -> Option AQuick Check:
Branch 'feature/login' matches 'feature/*' only [OK]
- Assuming 'main' stage runs on all branches
- Ignoring wildcard pattern matching
- Expecting both stages to run always
Identify the error in this Jenkins pipeline snippet that tries to run a stage only on the release branch:
stage('Deploy') {
when {
branch release
}
steps {
echo 'Deploying release branch'
}
}Solution
Step 1: Review syntax for branch condition in when block
The branch name must be a string inside quotes:branch 'release'.Step 2: Identify missing quotes causing syntax error
Here,branch releaselacks quotes, causing a syntax error.Final Answer:
Missing quotes around branch name in when condition -> Option CQuick Check:
Branch names need quotes in when { branch 'name' } [OK]
- Omitting quotes around branch names
- Placing steps outside stage block
- Misplacing branch condition inside steps
You want a Jenkins pipeline stage to run on all branches except main and develop. Which when condition correctly implements this?
Solution
Step 1: Understand negation of multiple branches
To exclude branches, wrap the branches inanyOfand negate withnot.Step 2: Apply correct syntax for excluding 'main' and 'develop'
not { anyOf { branch 'main'; branch 'develop' } }runs on all branches except these two.Final Answer:
not { anyOf { branch 'main'; branch 'develop' } } -> Option BQuick Check:
Exclude branches = not { anyOf { ... } } [OK]
- Using allOf with negations incorrectly
- Using logical operators like && directly
- Misplacing not inside anyOf instead of outside
Ready to prove it?
Pass the test and this pattern moves unknown
