0
0
Jenkinsdevops~10 mins

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

Choose your learning style9 modes available
Process Flow - Branch-specific pipeline behavior
Start Pipeline Trigger
Detect Branch Name
Match Branch?
NoRun 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.