Bird
Raised Fist0
Jenkinsdevops~10 mins

Branch-specific pipeline behavior in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Branch-specific pipeline behavior
Start Pipeline Trigger
↓
Detect Branch Name
↓
Match Branch?
No→Run Default Steps
Yes↓
Run Branch-Specific Steps
↓
Complete Pipeline
The pipeline starts, detects the branch, checks if it matches a specific branch, then runs either branch-specific or default steps before finishing.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      when { branch 'main' }
      steps { echo 'Building main branch' }
    }
    stage('Test') {
      steps { echo 'Testing all branches' }
    }
  }
}
This Jenkins pipeline runs the 'Build' stage only on the 'main' branch and always runs the 'Test' stage.
Process Table
StepBranch DetectedConditionBranch Matches 'main'?Action TakenOutput
1mainbranch == 'main'YesRun Build stageBuilding main branch
2mainAlways trueN/ARun Test stageTesting all branches
3feature-xyzbranch == 'main'NoSkip Build stageNo output
4feature-xyzAlways trueN/ARun Test stageTesting all branches
5developbranch == 'main'NoSkip Build stageNo output
6developAlways trueN/ARun Test stageTesting all branches
7Any branchPipeline completeN/AEndPipeline finished
💡 Pipeline ends after running all applicable stages based on branch conditions.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
branchundefinedmainfeature-xyzdevelopN/A
Build stage runfalsetruefalsefalsefalse
Test stage runfalsetruetruetruetrue
Key Moments - 3 Insights
Why does the Build stage run only on the 'main' branch?
Because the 'when { branch 'main' }' condition in the pipeline restricts the Build stage to run only when the branch name is exactly 'main', as shown in execution_table rows 1 and 3.
Does the Test stage run on all branches?
Yes, the Test stage has no branch condition, so it runs on every branch, confirmed by execution_table rows 2, 4, and 6.
What happens if the branch name is not 'main'?
The Build stage is skipped, but the Test stage still runs, as seen in execution_table rows 3 and 4 for 'feature-xyz' branch.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at Step 1 when the branch is 'main'?
ATesting all branches
BBuilding main branch
CNo output
DPipeline finished
💡 Hint
Check the 'Output' column for Step 1 in the execution_table.
At which step does the pipeline skip the Build stage for the 'feature-xyz' branch?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'Skip Build stage' action with branch 'feature-xyz' in execution_table.
If the branch was 'main', which stages run according to the variable_tracker?
ABoth Build and Test stages
BOnly Build stage
COnly Test stage
DNeither stage
💡 Hint
Check 'Build stage run' and 'Test stage run' values after Step 1 in variable_tracker.
Concept Snapshot
Jenkins pipelines can run different steps based on the branch.
Use 'when { branch "branch-name" }' to run stages only on specific branches.
Stages without branch conditions run on all branches.
This helps customize CI/CD for different development workflows.
Full Transcript
This visual execution shows how a Jenkins pipeline behaves differently depending on the branch name. The pipeline starts by detecting the branch. If the branch is 'main', it runs the Build stage; otherwise, it skips it. The Test stage runs on all branches. The execution table traces each step with branch detection, condition checks, actions, and outputs. The variable tracker shows how the branch and stage run flags change. Key moments clarify why stages run or skip. The quiz tests understanding of branch conditions and stage execution. This helps beginners see how to control pipeline flow based on branches.

Practice

(1/5)
1.

Which Jenkins pipeline syntax runs a stage only on the main branch?

easy
A. branch == 'main' { runStage() }
B. if (branch == 'main')
C. onlyIf branch == 'main'
D. when { branch 'main' }

Solution

  1. Step 1: Understand Jenkins branch condition syntax

    Jenkins uses when { branch 'branchName' } to run stages on specific branches.
  2. Step 2: Match syntax to run on main branch

    The correct syntax is when { branch 'main' } to target the main branch.
  3. Final Answer:

    when { branch 'main' } -> Option D
  4. Quick Check:

    Branch condition = when { branch 'main' } [OK]
Hint: Use 'when { branch "branchName" }' for branch-specific stages [OK]
Common Mistakes:
  • Using if statements instead of 'when' block
  • Incorrect syntax like 'onlyIf' or inline conditions
  • Confusing branch name quotes or missing quotes
2.

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'
  }
}
easy
A. anyOf { branch 'dev'; branch 'feature/*' }
B. branch 'dev' && branch 'feature/*'
C. allOf { branch 'dev'; branch 'feature/*' }
D. branch 'dev' || branch 'feature/*'

Solution

  1. Step 1: Identify correct syntax for multiple branch conditions

    Jenkins uses anyOf { branch 'name'; branch 'pattern' } to run if any branch matches.
  2. Step 2: Choose syntax matching either 'dev' or 'feature/*'

    Using anyOf { branch 'dev'; branch 'feature/*' } runs the stage on either branch.
  3. Final Answer:

    anyOf { branch 'dev'; branch 'feature/*' } -> Option A
  4. Quick Check:

    Multiple branches = anyOf with branch conditions [OK]
Hint: Use anyOf { branch 'x'; branch 'y' } for multiple branches [OK]
Common Mistakes:
  • Using logical operators like || or && directly inside when
  • Using allOf which requires all conditions true
  • Incorrect syntax without braces or semicolons
3.

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' }
    }
  }
}
medium
A. Testing feature branch
B. Building main branch
C. Building main branch\nTesting feature branch
D. No output

Solution

  1. Step 1: Check branch conditions for each stage

    'Build' runs only on 'main' branch; 'Test' runs on branches matching 'feature/*'.
  2. Step 2: Match current branch 'feature/login'

    'feature/login' matches 'feature/*', so only 'Test' stage runs and prints 'Testing feature branch'.
  3. Final Answer:

    Testing feature branch -> Option A
  4. Quick Check:

    Branch 'feature/login' matches 'feature/*' only [OK]
Hint: Match branch patterns carefully to predict stage execution [OK]
Common Mistakes:
  • Assuming 'main' stage runs on all branches
  • Ignoring wildcard pattern matching
  • Expecting both stages to run always
4.

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'
  }
}
medium
A. Steps block should be outside the stage
B. Incorrect stage name 'Deploy'
C. Missing quotes around branch name in when condition
D. Branch condition should be inside steps

Solution

  1. Step 1: Review syntax for branch condition in when block

    The branch name must be a string inside quotes: branch 'release'.
  2. Step 2: Identify missing quotes causing syntax error

    Here, branch release lacks quotes, causing a syntax error.
  3. Final Answer:

    Missing quotes around branch name in when condition -> Option C
  4. Quick Check:

    Branch names need quotes in when { branch 'name' } [OK]
Hint: Always quote branch names in when { branch 'name' } [OK]
Common Mistakes:
  • Omitting quotes around branch names
  • Placing steps outside stage block
  • Misplacing branch condition inside steps
5.

You want a Jenkins pipeline stage to run on all branches except main and develop. Which when condition correctly implements this?

hard
A. allOf { not branch 'main'; not branch 'develop' }
B. not { anyOf { branch 'main'; branch 'develop' } }
C. anyOf { not branch 'main'; not branch 'develop' }
D. branch != 'main' && branch != 'develop'

Solution

  1. Step 1: Understand negation of multiple branches

    To exclude branches, wrap the branches in anyOf and negate with not.
  2. Step 2: Apply correct syntax for excluding 'main' and 'develop'

    not { anyOf { branch 'main'; branch 'develop' } } runs on all branches except these two.
  3. Final Answer:

    not { anyOf { branch 'main'; branch 'develop' } } -> Option B
  4. Quick Check:

    Exclude branches = not { anyOf { ... } } [OK]
Hint: Use not { anyOf { branch 'x'; branch 'y' } } to exclude branches [OK]
Common Mistakes:
  • Using allOf with negations incorrectly
  • Using logical operators like && directly
  • Misplacing not inside anyOf instead of outside
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard