The Idea
With a normal pipeline job you build one branch. Real teams use feature branches and pull requests. A Multibranch Pipeline looks into the repository, finds every branch that has a Jenkinsfile, and creates a pipeline job for each one automatically. When a branch is deleted, its job is removed. Each pull request also gets built before it is merged.
Create One
- Install GitHub Branch Source (or the GitLab or Bitbucket branch source plugin)
- New Item, name
shop-app, type Multibranch Pipeline - Branch Sources, *Add source*, GitHub: choose credentials (a GitHub App or token), owner and repository
- Behaviours: Discover branches, Discover pull requests from origin and from forks (with a trust policy), optional Discover tags
- Build Configuration: *by Jenkinsfile*, Script Path
Jenkinsfile - Scan triggers: *Periodically if not otherwise run* (for example 1 day) as a safety net; webhooks make it instant
- Save. Jenkins runs Scan Repository and creates the jobs
JENKINS_URL/github-webhook/ yourself. Events needed: push and pull request.
One Jenkinsfile for All Branches
| Variable or condition | Meaning |
|---|---|
env.BRANCH_NAME | main, feature/login, PR-42 or the tag name |
env.CHANGE_ID | Pull request number (empty for normal branches) |
env.CHANGE_BRANCH, env.CHANGE_TARGET | Source and target branch of the PR |
env.CHANGE_AUTHOR, env.CHANGE_URL | Author and link |
env.TAG_NAME | Set when building a tag |
when { branch 'main' } | Only on that branch |
when { changeRequest() } | Only for PRs |
when { buildingTag() } | Only for tags |
disableConcurrentBuilds(abortPrevious: true)cancels an older build of the same branch when a new commit arrives, saving agents- A pull request is built with the result of merging into the target branch by default, so you test what would really be merged (strategy setting)
Pull Requests and Status Checks
- Jenkins reports commit status to GitHub: the PR shows a yellow dot while building and a green tick or red cross at the end
- In GitHub branch protection, mark the Jenkins check (for example
continuous-integration/jenkins/pr-merge) as required, so nothing is merged unless the pipeline passes - For forks, choose a trust policy: *Contributors* or *Nobody*, because a fork's Jenkinsfile can run any code on your agents. Use *Trust: from users with Admin or Write permission* where possible
Organization Folders
A GitHub Organization (or Bitbucket Team, GitLab Group) folder scans all repositories of an owner, and creates a multibranch pipeline for each repository that contains a Jenkinsfile. New repositories are picked up automatically.
| Item | Behaviour |
|---|---|
| Scan | Runs on a schedule and on organization webhook events |
| Filters | Include or exclude repositories by name pattern |
| Credentials | A GitHub App is recommended: higher rate limits and no personal account |
| Orphaned items | Remove old branch jobs after a number of days or builds (set *Orphaned Item Strategy*) |
Folders for Organisation and Security
- Use Folders (a plugin) to group jobs by team or product:
payments/,platform/ - Each folder can have its own credentials, permissions, environment variables and shared libraries
- Names can be addressed as
payments/api/maininbuild job:steps
Housekeeping and Troubleshooting
| Problem | Fix |
|---|---|
| Branch has a Jenkinsfile but no job appears | Check the *Scan Repository Log*; wrong script path or no discovery behaviour |
| Builds do not start on push | Webhook missing or failing; check GitHub delivery log; use scan trigger as a fallback |
Could not find credentials entry | Wrong ID or credential is not visible from the folder |
| GitHub API rate limit exceeded | Use a GitHub App or token with higher limits; reduce scan frequency |
| Many old branch jobs | Set orphaned item strategy; delete merged branches |
| PR build uses an old state | The PR strategy merges with target: re-run after the target updates |
Practice Tasks
- Create a multibranch pipeline for a repository with two branches and open a pull request
- Make a stage that runs only on
mainand one only for PRs - Make the PR check required in GitHub branch protection
- Test a fork PR policy and write down what the pipeline may and may not do
- Set orphaned item cleanup and delete a branch to see the job disappear
Conclusion
A multibranch pipeline turns each branch, pull request and tag with a Jenkinsfile into its own job, driven by webhooks and scans. Use BRANCH_NAME and CHANGE_ID in when conditions, report status to Git and require it in branch protection, and treat pull requests from forks as untrusted code.

