Topic 3.4
Multibranch Pipelines & Shared Libraries
In one line
A Multibranch Pipeline automatically discovers and builds every branch (and pull request) in a repository — and a Shared Library extends the reusable-workflow idea from Phase 2.3 to Jenkins' own Groovy-based pipelines.
Key ideas
- 01
A plain Jenkins Pipeline job is configured to build ONE specific branch — a MULTIBRANCH PIPELINE job instead automatically SCANS a repository and creates a separate, independent pipeline for EVERY branch (and, with the right configuration, every pull request) that contains a Jenkinsfile, with zero manual per-branch configuration required.
- 02
This directly solves a genuinely common real need: automatically running CI on every feature branch a team creates, and automatically REMOVING that branch's pipeline once the branch itself is deleted (after a pull request merges, say) — Jenkins keeps its own configuration in sync with the repository's actual branch structure, rather than requiring manual setup and teardown for every single branch.
- 03
A SHARED LIBRARY is Jenkins' own answer to the exact problem Phase 2.3's reusable workflows and composite actions solve for GitHub Actions — a genuinely common pipeline pattern (a standard build-test-notify sequence, say) gets defined ONCE, as actual Groovy code in a separate, dedicated Git repository, and any Jenkinsfile across the organization can then call it directly.
- 04
@Library('my-shared-library') _at the top of a Jenkinsfile imports a configured shared library, making its custom functions/steps available for use in that pipeline — a shared library can define an entire custom pipeline STEP (myOrgBuildAndTest()) that internally wraps the same multi-stage logic every team would otherwise have to duplicate individually in their own Jenkinsfiles. - 05
Shared libraries are versioned exactly like any other Git repository — a Jenkinsfile can pin to a SPECIFIC library version (
@Library('my-shared-library@v2.1.0')) for stability, or track a branch for the latest changes automatically — the exact same version-pinning trade-off Phase 2.1 already covered for GitHub Actions (a tag versus a specific commit SHA), just applied to Jenkins' own equivalent mechanism. - 06
Together, multibranch pipelines and shared libraries let a genuinely large organization scale Jenkins usage across hundreds of repositories and thousands of branches without hundreds of independently-maintained, slowly-diverging Jenkinsfiles — new branches get CI automatically, and a shared, centrally-maintained library keeps the ACTUAL pipeline logic consistent and easy to update everywhere at once.
Code & diagrams
One Multibranch Pipeline job automatically tracks every branch with a Jenkinsfile — no manual per-branch setup.
A whole standard pipeline, defined once in a shared library, used with one line in any repository.
// In any repository's Jenkinsfile:
@Library('my-shared-library@v2.1.0') _
myOrgBuildAndTest(
javaVersion: '21',
runIntegrationTests: true
)
// everything this actually does — checkout, build, test, archive, notify —
// lives in the shared library's own repository, maintained centrally,
// used identically (and consistently) across every calling repositoryExplain it without notes
What genuine, ongoing manual work does a Multibranch Pipeline job remove, compared to a plain single-branch Pipeline job?
How does a Jenkins Shared Library solve the same underlying problem as GitHub Actions' reusable workflows, from Phase 2.3?
Practice
If you have access to a Jenkins instance, set up a Multibranch Pipeline job pointing at a real repository with a Jenkinsfile, and confirm it automatically discovers and builds more than one branch.
Sketch, in plain English, what a shared library function for a hypothetical organization's standard build-test-notify pattern should accept as parameters, and what it should do internally.
Trade-offs
- ↔
Multibranch pipelines and shared libraries add genuine power and consistency at scale, but they also mean an individual Jenkinsfile becomes less self-contained and readable in isolation — understanding what
myOrgBuildAndTest()actually does now requires looking at a separate shared library repository, exactly the same readability-versus-reuse trade-off Phase 2.3 already flagged for GitHub Actions' reusable workflows and composite actions.
Done when you can
I understand what a Multibranch Pipeline job automatically discovers and manages, compared to a plain Pipeline job.
I can explain how a Shared Library solves the same duplication problem as GitHub Actions' reusable workflows.
I understand shared libraries can be version-pinned, with the same trade-offs as pinning any other reusable dependency.