0
0
Jenkinsdevops~10 mins

Jenkinsfile per branch - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Jenkinsfile per branch
Start Jenkins Pipeline
Detect current branch
Load Jenkinsfile for branch
Execute branch-specific pipeline
Pipeline completes or fails
Jenkins detects the current branch, loads the matching Jenkinsfile, then runs the pipeline defined for that branch.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo "Building branch ${env.BRANCH_NAME}"
      }
    }
  }
}
A simple Jenkins pipeline that prints the current branch name during the build stage.
Process Table
StepActionBranch DetectedJenkinsfile LoadedPipeline Output
1Start pipelinefeature/loginJenkinsfile-featureN/A
2Load Jenkinsfilefeature/loginJenkinsfile-featureN/A
3Execute Build stagefeature/loginJenkinsfile-featureBuilding branch feature/login
4Pipeline completesfeature/loginJenkinsfile-featureSuccess
💡 Pipeline ends after executing branch-specific Jenkinsfile for feature/login branch.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
env.BRANCH_NAMEundefinedfeature/loginfeature/loginfeature/loginfeature/login
Jenkinsfile LoadednonenoneJenkinsfile-featureJenkinsfile-featureJenkinsfile-feature
Pipeline OutputnonenonenoneBuilding branch feature/loginSuccess
Key Moments - 2 Insights
How does Jenkins know which Jenkinsfile to use for each branch?
Jenkins detects the current branch name (see Step 1 in execution_table) and loads the Jenkinsfile matching that branch (Step 2). This allows branch-specific pipelines.
What happens if the Jenkinsfile for a branch is missing?
The pipeline will fail to load the Jenkinsfile at Step 2, causing the build to fail early. This is why each branch should have its own Jenkinsfile or fallback.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the branch detected at Step 1?
Afeature/login
Bmain
Cdevelop
Drelease
💡 Hint
Check the 'Branch Detected' column at Step 1 in the execution_table.
At which step does Jenkins load the branch-specific Jenkinsfile?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Jenkinsfile Loaded' column in the execution_table.
If the branch was 'main', how would the Jenkinsfile Loaded value change in the execution_table?
AJenkinsfile-feature
BJenkinsfile-main
CJenkinsfile-develop
DNo Jenkinsfile loaded
💡 Hint
The Jenkinsfile loaded matches the branch name as shown in the 'Jenkinsfile Loaded' column.
Concept Snapshot
Jenkinsfile per branch:
- Jenkins detects current branch via env.BRANCH_NAME
- Loads Jenkinsfile matching branch name
- Runs branch-specific pipeline steps
- Enables different workflows per branch
- Fails if Jenkinsfile missing for branch
Full Transcript
This visual execution shows how Jenkins runs a pipeline using a Jenkinsfile specific to the current branch. First, Jenkins detects the branch name, for example 'feature/login'. Then it loads the Jenkinsfile named for that branch, such as 'Jenkinsfile-feature'. Next, it executes the pipeline steps defined in that Jenkinsfile, here printing the branch name during the build stage. Finally, the pipeline completes successfully. If the Jenkinsfile for the branch is missing, the pipeline will fail early. This approach allows teams to customize CI/CD workflows per branch easily.