0
0
Jenkinsdevops~10 mins

Multi-branch pipeline job creation in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Multi-branch pipeline job creation
Start Jenkins
Create Multi-branch Pipeline Job
Configure SCM Repository
Scan Branches
For each branch found
Create Pipeline Job for Branch
Run Pipeline Job
Collect Results
End
This flow shows how Jenkins creates a multi-branch pipeline job, scans branches in the repository, creates jobs for each branch, and runs them.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo "Building branch ${env.BRANCH_NAME}" }
    }
  }
}
A simple Jenkins pipeline script that prints the branch name during the build stage.
Process Table
StepActionDetailsResult
1Start JenkinsJenkins server startsReady to create jobs
2Create Multi-branch Pipeline JobJob named 'MyMultiBranchJob' createdJob created with SCM config
3Configure SCM RepositorySet Git URL to https://github.com/example/repo.gitSCM linked
4Scan BranchesJenkins scans repo branchesBranches found: main, dev, feature1
5Create Pipeline Job for Branch 'main'Job created for 'main'Job ready to run
6Create Pipeline Job for Branch 'dev'Job created for 'dev'Job ready to run
7Create Pipeline Job for Branch 'feature1'Job created for 'feature1'Job ready to run
8Run Pipeline Job for 'main'Pipeline script runsOutput: Building branch main
9Run Pipeline Job for 'dev'Pipeline script runsOutput: Building branch dev
10Run Pipeline Job for 'feature1'Pipeline script runsOutput: Building branch feature1
11Collect ResultsJobs completedBuild results collected
12EndAll branches processedMulti-branch pipeline complete
💡 All branches scanned and pipeline jobs executed successfully
Status Tracker
VariableStartAfter Step 4After Step 7Final
Branches Found[][main, dev, feature1][main, dev, feature1][main, dev, feature1]
Jobs Created0033
Build Output"""""""Building branch main\nBuilding branch dev\nBuilding branch feature1"
Key Moments - 3 Insights
Why does Jenkins create separate jobs for each branch?
Because the multi-branch pipeline scans the repository and creates a job per branch to run pipelines independently, as shown in steps 5-7.
What happens if a new branch is added after the initial scan?
Jenkins will detect it on the next scan and create a new job for that branch, similar to the initial scan process in step 4.
Why do pipeline jobs print the branch name during the build?
The pipeline uses the environment variable BRANCH_NAME to identify the current branch, demonstrated in the build output in steps 8-10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Jenkins find the branches in the repository?
AStep 8
BStep 2
CStep 4
DStep 10
💡 Hint
Check the 'Action' column for scanning branches in the execution table.
According to the variable tracker, how many jobs are created after step 7?
A3
B1
C0
D5
💡 Hint
Look at the 'Jobs Created' row under 'After Step 7' in the variable tracker.
If a new branch 'hotfix' is added after step 4, what will happen next?
AJenkins ignores it
BJenkins creates a new job for 'hotfix' on next scan
CJenkins deletes all jobs
DJenkins merges 'hotfix' with 'main'
💡 Hint
Refer to key moment about new branches being detected on scans.
Concept Snapshot
Multi-branch pipeline job creation in Jenkins:
- Create a multi-branch pipeline job
- Configure SCM repository URL
- Jenkins scans branches automatically
- Creates pipeline jobs per branch
- Runs pipelines independently per branch
- Uses BRANCH_NAME env variable in scripts
Full Transcript
This visual execution shows how Jenkins creates a multi-branch pipeline job. First, Jenkins starts and the user creates a multi-branch pipeline job. Then, the SCM repository URL is configured. Jenkins scans the repository branches and finds multiple branches like main, dev, and feature1. For each branch, Jenkins creates a separate pipeline job. Each job runs the pipeline script, which prints the branch name using the BRANCH_NAME environment variable. After all jobs run, Jenkins collects the results and completes the multi-branch pipeline process. Variables like branches found and jobs created update step-by-step. Key moments clarify why Jenkins creates jobs per branch and how new branches are handled. The quiz tests understanding of scanning steps, job creation count, and branch addition behavior.