Branch-specific pipeline behavior in Jenkins - Commands & Configuration
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "Building on branch ${env.BRANCH_NAME}"
}
}
stage('Test') {
when {
anyOf {
branch 'feature/*'
branch 'develop'
}
}
steps {
echo 'Running tests on feature or develop branches'
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
echo 'Deploying from main branch'
}
}
}
}This Jenkinsfile defines a pipeline with three stages: Build, Test, and Deploy.
The when condition controls which branches run each stage:
- Test runs only on branches named like
feature/*ordevelop. - Deploy runs only on the
mainbranch.
This lets you customize pipeline behavior based on the branch name.
git checkout -b feature/new-uigit push origin feature/new-uijenkins build example-pipeline -p BRANCH_NAME=feature/new-ui
-p BRANCH_NAME=feature/new-ui - Sets the branch name environment variable for the pipelinejenkins build example-pipeline -p BRANCH_NAME=main
-p BRANCH_NAME=main - Sets the branch name environment variable for the pipelineIf you remember nothing else from this pattern, remember: use the 'when' directive in Jenkins pipelines to run different steps based on the branch name.
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
