0
0
Jenkinsdevops~10 mins

Branch selection and branch specifier in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Branch selection and branch specifier
Start Jenkins Pipeline
Read Branch Specifier
Check Branch Exists?
NoFail or Wait
Yes
Checkout Branch
Run Pipeline Steps on Selected Branch
End Pipeline
The Jenkins pipeline reads the branch specifier, checks if the branch exists, then checks out that branch to run the pipeline steps.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: 'refs/heads/main']]])
      }
    }
  }
}
This pipeline checks out the 'main' branch from Git before running further steps.
Process Table
StepActionBranch SpecifierBranch Exists?Result
1Start Pipelinerefs/heads/mainN/APipeline started
2Read Branch Specifierrefs/heads/mainN/ABranch specifier read
3Check Branch Existsrefs/heads/mainYesBranch found
4Checkout Branchrefs/heads/mainYesBranch 'main' checked out
5Run Pipeline Stepsrefs/heads/mainYesPipeline runs on 'main' branch
6End Pipelinerefs/heads/mainYesPipeline completed
💡 Pipeline ends after running steps on the specified branch.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
branchSpecifierundefinedrefs/heads/mainrefs/heads/mainrefs/heads/mainrefs/heads/main
branchExistsundefinedundefinedtruetruetrue
checkoutStatusundefinedundefinedundefinedsuccesssuccess
Key Moments - 2 Insights
Why does the pipeline fail if the branch specifier is incorrect?
Because in the execution_table at Step 3, if 'branchExists' is 'No', Jenkins cannot checkout the branch, causing failure.
Can the branch specifier be changed dynamically during the pipeline?
No, the branch specifier is read at the start (Step 2) and used for checkout; changing it later won't affect the already checked out code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the branchExists value at Step 3?
ANo
BYes
CUndefined
DError
💡 Hint
Check the 'branchExists' column in row for Step 3 in execution_table.
At which step does Jenkins perform the actual branch checkout?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Checkout Branch' action in execution_table.
If the branch specifier was 'refs/heads/dev', how would the variable 'branchSpecifier' change after Step 2?
AIt would be 'refs/heads/dev'
BIt would be 'refs/heads/main'
CIt would be undefined
DIt would be empty string
💡 Hint
Refer to variable_tracker 'branchSpecifier' value after Step 2.
Concept Snapshot
Jenkins branch specifier defines which Git branch to checkout.
It uses syntax like 'refs/heads/main' to specify branches.
Pipeline reads this specifier early, checks if branch exists.
If branch exists, Jenkins checks it out and runs pipeline steps.
Incorrect specifier causes checkout failure and pipeline error.
Full Transcript
In Jenkins pipelines, the branch specifier tells Jenkins which Git branch to use. The pipeline starts by reading this specifier, for example 'refs/heads/main'. Jenkins then checks if this branch exists in the repository. If it does, Jenkins checks out the branch so the pipeline can run on that code. If the branch does not exist, the pipeline fails because it cannot checkout the code. The branch specifier is fixed at the start of the pipeline and cannot be changed dynamically during execution. This process ensures the pipeline runs on the correct branch code.