Multibranch Pipelines, Pull Request Builds, Folders and Organization Scanning With GitHub

CI CD Pipelines with Jenkins 35 min min read Updated: Nov 17, 2026 Intermediate
Multibranch Pipelines, Pull Request Builds, Folders and Organization Scanning With GitHub
Intermediate Topic 5 of 5

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.

text Repository app.git ├─ main ─► job "main" (builds, deploys to staging) ├─ feature/login ─► job "feature%2Flogin" ├─ PR-42 ─► job "PR-42" (builds the merge result, reports status to GitHub) └─ tag v1.4.0 ─► job "v1.4.0" (builds a release, if tags are discovered)

Create One

  1. Install GitHub Branch Source (or the GitLab or Bitbucket branch source plugin)
  2. New Item, name shop-app, type Multibranch Pipeline
  3. Branch Sources, *Add source*, GitHub: choose credentials (a GitHub App or token), owner and repository
  4. Behaviours: Discover branches, Discover pull requests from origin and from forks (with a trust policy), optional Discover tags
  5. Build Configuration: *by Jenkinsfile*, Script Path Jenkinsfile
  6. Scan triggers: *Periodically if not otherwise run* (for example 1 day) as a safety net; webhooks make it instant
  7. Save. Jenkins runs Scan Repository and creates the jobs
Webhooks: With the GitHub plugin and *Manage hooks*, Jenkins can register the webhook for you, or you add the repository webhook JENKINS_URL/github-webhook/ yourself. Events needed: push and pull request.

One Jenkinsfile for All Branches

groovy pipeline { agent any options { timeout(time: 30, unit: 'MINUTES'); disableConcurrentBuilds(abortPrevious: true) } stages { stage('Build and test') { steps { sh 'mvn -B verify' } post { always { junit '**/target/surefire-reports/*.xml' } } } stage('Publish snapshot') { when { branch 'develop' } steps { sh './publish-snapshot.sh' } } stage('Deploy staging') { when { branch 'main' } steps { sh './deploy.sh staging' } } stage('Release') { when { buildingTag() } steps { sh "./release.sh ${env.TAG_NAME}" } } stage('PR extras') { when { changeRequest target: 'main' } steps { echo "Pull request ${env.CHANGE_ID} from ${env.CHANGE_BRANCH} to ${env.CHANGE_TARGET}" } } } }
Variable or conditionMeaning
env.BRANCH_NAMEmain, feature/login, PR-42 or the tag name
env.CHANGE_IDPull request number (empty for normal branches)
env.CHANGE_BRANCH, env.CHANGE_TARGETSource and target branch of the PR
env.CHANGE_AUTHOR, env.CHANGE_URLAuthor and link
env.TAG_NAMESet 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
Pull requests from strangers can run code: A malicious PR can change the Jenkinsfile and try to steal credentials. Never expose production secrets to PR builds from forks. Use separate agents with no credentials for untrusted code and a Jenkinsfile loaded from the trusted target branch when needed.

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.

ItemBehaviour
ScanRuns on a schedule and on organization webhook events
FiltersInclude or exclude repositories by name pattern
CredentialsA GitHub App is recommended: higher rate limits and no personal account
Orphaned itemsRemove 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/main in build job: steps

Housekeeping and Troubleshooting

ProblemFix
Branch has a Jenkinsfile but no job appearsCheck the *Scan Repository Log*; wrong script path or no discovery behaviour
Builds do not start on pushWebhook missing or failing; check GitHub delivery log; use scan trigger as a fallback
Could not find credentials entryWrong ID or credential is not visible from the folder
GitHub API rate limit exceededUse a GitHub App or token with higher limits; reduce scan frequency
Many old branch jobsSet orphaned item strategy; delete merged branches
PR build uses an old stateThe PR strategy merges with target: re-run after the target updates

Practice Tasks

  1. Create a multibranch pipeline for a repository with two branches and open a pull request
  2. Make a stage that runs only on main and one only for PRs
  3. Make the PR check required in GitHub branch protection
  4. Test a fork PR policy and write down what the pipeline may and may not do
  5. 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.

Quick Summary: multibranch discovery; one Jenkinsfile many branches; BRANCH_NAME/CHANGE_*; PR status + branch protection; fork trust; org folders (GitHub App); folders; troubleshooting.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators