Shared Libraries, Global Variables, vars, src, resources, Versioning, Testing, and Pipeline Product Design: Concepts, Architecture, and Mental Model
Shared Libraries turn repeated Pipeline behavior into reusable software. This lesson establishes the trust, source identity, directory layout, version-selection, API and migration boundaries that keep reuse from becoming an invisible controller-wide code path.
Learning objectives
- Explain how a consumer Jenkinsfile selects, loads and executes a Shared Library.
- Define trusted versus sandboxed library boundaries before using them.
-
Distinguish the
vars,srcandresourcesAPI surfaces. - Track library ref/commit, consumer source SHA, dependencies and migration policy as build evidence.
- Inspect library configuration and source identity before changing it.
1. The reuse problem
By Chapter 23 you can onboard many repositories. If every repository copies the same checkout, test, publish and policy logic, drift becomes inevitable. Jenkins Shared Libraries let teams reuse reviewed Pipeline code from an external SCM repository. The benefit is consistency; the risk is that a single library change can alter dozens or hundreds of builds.
Therefore the unit of control is not just “the Jenkinsfile.” A
reproducible build needs a tuple such as
consumer SHA + Jenkinsfile path + library name/version/resolved
commit + Jenkins/plugin baseline + agent/toolchain. If the library is trusted, its repository write controls are part
of Jenkins controller security.
2. Mental model: source to side effect
Read the causal chain before using syntax:
flowchart TD A[Consumer Jenkinsfile + source SHA] --> B[Library name + requested version] B --> C[Jenkins library config / trust policy] C --> D[Modern SCM checkout] D --> E[Resolved library commit] E --> F[Compile/load vars + src + resources] F --> G[Library API call] G --> H[Pipeline steps on agent/controller] H --> I[Reports / artifacts / external side effects] E --> J[Library provenance + compatibility evidence]
The consumer chooses or inherits a version. Jenkins retrieves that version from SCM. Library Groovy is compiled and CPS-transformed for Pipeline use. API calls may orchestrate Pipeline steps, agent work or external systems. Evidence must therefore retain both consumer and library identities.
3. Shared Library repository structure
ch24-lib/
├── vars/
│ ├── buildBanner.groovy
│ └── buildBanner.txt
├── src/
│ └── academy/pipeline/Metadata.groovy
├── resources/
│ └── academy/pipeline/defaults.json
└── README.md
| Area | Purpose | Design rule |
|---|---|---|
vars/*.groovy |
Global variables/custom steps visible to Jenkinsfiles | Keep entry points small and explicit; do not silently shadow built-in steps. |
src/ |
Namespaced Groovy classes | Prefer pure/testable logic; make Pipeline-step dependencies explicit. |
resources/ |
Static resources for libraryResource |
Treat templates/config as versioned behavior, not “just data.” |
| SCM refs | Branches/tags/commits | Production consumers need a controlled, attributable release identity. |
4. Trust is not a convenience checkbox
Global trusted libraries can call APIs that normal sandboxed Pipeline code cannot. Jenkins documentation warns that anyone able to push to a trusted library repository may obtain unlimited access to Jenkins. Configure trusted libraries only when a reviewed use case genuinely needs that power, and protect the SCM accordingly.
Folder-scoped libraries are always untrusted and run in the Groovy sandbox. For most reusable build/test orchestration, that is the safer default. “The sandbox blocked it” is not evidence that the library should become trusted.
5. Version identity and Modern SCM
A configured Shared Library can have a default version and may allow consumers to override it. Git branches, tags and commit hashes can be version selectors. Modern SCM is the preferred retrieval path because Jenkins asks the SCM source for the requested revision directly.
@Library('academy-lib@v1.0.0') _
buildBanner(message: 'compile')
A human-readable tag is useful for release communication; record the resolved commit SHA as immutable evidence. If override is disabled, consumers cannot silently select a different library revision.
6. Read-only state inventory
| Layer | Record before change |
|---|---|
| Controller/library config | Name, trust mode, default version, override setting, retrieval method, credential ID |
| Library SCM | Repository URL, tag/branch, resolved commit, owners/protection |
| Consumer | Job full name, Jenkinsfile path, requested library version, application/source SHA |
| API surface | Public vars functions, src classes, resource paths, documented parameters/returns |
| Compatibility | Supported consumer range, tests, deprecation/migration policy |
| Execution | Agent/node/workspace, build number/cause, produced evidence and external side effects |
7. Product discipline
Treat a library release like an internal package: owner, changelog, release/tag policy, review requirements, automated tests, security boundary and rollback. A Shared Library should reduce duplication without hiding the important decisions that belong in a consumer Jenkinsfile—such as privileged deployment authorization or which production environment is targeted.
Knowledge check
Answer before revealing the explanation.
1. What makes a Shared Library a software product rather than a code-snippet folder?
It has a defined API surface, source repository and immutable version identity, tests, trust model, compatibility promises, owners, release/migration rules and observable consumer evidence.
2. What is the most important security fact about a trusted global library?
Trusted library code can call otherwise unsafe Java, Groovy, Jenkins and plugin APIs outside the normal Pipeline sandbox. Anyone who can modify that library repository effectively gains controller-level code power.
3. What is the role of vars, src and resources?
vars exposes global variables/custom steps, src holds namespaced Groovy classes, and resources carries static files loadable with libraryResource from external Shared Libraries. They are different API surfaces and should be versioned together.
4. Why record the exact library commit as well as the consumer source SHA?
A build can be reproducible only if both the Jenkinsfile/application revision and the library implementation that shaped execution are independently attributable.
5. Why is a default branch such as main a weak production version contract?
It is mutable. Two rebuilds of the same application commit can execute different library behavior unless the library revision is pinned or otherwise release-controlled.
Official references and version notes
-
Jenkins — Extending with Shared Libraries
— trusted/untrusted libraries,
vars/src/resources, version selection, Modern SCM,@Library,libraryand testing guidance. -
Pipeline: Groovy Libraries
— version
805.va_fc79344957d, released 2026-08-27, requiring Jenkins 2.555.1. -
Pipeline: Groovy
— version
4380.v6eb_8378b_9647; Shared Library Groovy is CPS-transformed under the same Pipeline execution model. -
Script Security
— version
1422.v06869826dd9b_; sandbox and approval boundaries remain security-sensitive. -
Git
— version
5.10.1; used by the Modern SCM lab retriever. -
Git Client
— version
6.6.1. - Pipeline Best Practices — avoid overriding built-in Pipeline steps and avoid oversized global-variable files.
- JenkinsPipelineUnit — optional third-party/open-source test framework for Pipeline/library tests; verify the current release and Groovy compatibility before adopting it. The mandatory lab does not require it.
- Pipeline: Deprecated Groovy Libraries — legacy/deprecated plugin; do not use it as the design target for new Shared Libraries.
Version note — 2026-09-17: chapter baseline is
Jenkins 2.568.3 LTS with Java 21. The current Pipeline:
Groovy Libraries release listed above includes security fixes beyond
earlier versions affected by sandbox/file-read/CSRF advisories.
Re-check plugin security advisories and the exact SCM/test-tool
versions before production rollout.
Keep the academy open
Support free, practical DevOps education.
Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0Send only Ethereum/ERC-20 compatible assets to this
address.