Skip to content
Latchkey

How to Build a Multibranch Pipeline in Jenkins

A Multibranch Pipeline scans the repo and creates a job for every branch that contains a Jenkinsfile.

Point a Multibranch Pipeline at your repo; Jenkins discovers branches and PRs, runs their Jenkinsfile, and removes jobs when branches are deleted.

Branch-aware Jenkinsfile

The same Jenkinsfile runs per branch; use env.BRANCH_NAME to vary behavior such as deploying only from main.

Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { sh 'make build' }
    }
    stage('Deploy') {
      when { branch 'main' }
      steps { sh './deploy.sh' }
    }
  }
}

Notes

  • Configure branch sources and discovery (branches, PRs from origin/fork) in the Multibranch Pipeline job config.
  • env.BRANCH_NAME and the when { branch } condition let one Jenkinsfile behave differently per branch.
  • Orphaned branch jobs are pruned automatically on the next scan.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →