Jenkins / Evidence, reuse, and authority / Lesson 12
Shared Library interfaces and trust
Reuse orchestration through narrow versioned contracts without hiding controller-level authority.
On this page
Lesson goals
- Distinguish vars, src, and resources roles.
- Review trusted library authority, callers, and revision selection.
A library is a maintained dependency
Jenkins Shared Libraries can organize reusable Pipeline behavior through documented layouts such as vars for global variables or callable steps, src for classes, and resources for supported non-code material. Parcelโs fixture selector can be a small shared interface rather than repeated Groovy across many jobs. Reuse introduces a dependency whose updates affect every caller. Define inputs, outputs, result behavior, required plugins, and execution context. Removing duplicate text is not enough if callers can no longer tell where a node is allocated or which credential a helper receives.
Trusted library code has exceptional authority
A globally configured trusted library can execute outside the Groovy sandbox under the documented trust model. Control of its source repository therefore carries powerful Jenkins authority. Untrusted library configuration has different restrictions, and folder or installation settings matter. Review who can publish library revisions, change default versions, override refs, and approve script signatures. Do not mark a library trusted merely to silence a rejected method. Script sandboxing is a Pipeline language control, not an operating-system sandbox for commands executed on an agent.
Select a reviewed revision deliberately
The fragment below uses a synthetic configured library name and a teaching ref label. It does not invent a commit hash or claim that a library exists. A real shared dependency should resolve to a reviewed revision under a controlled update policy. A moving default can change many jobs without a visible Jenkinsfile diff in each caller. Record the resolved library identity with candidate evidence, including nested executable dependencies where relevant. A precise pin improves traceability but still needs implementation and authority review.
@Library("parcel-library@lab-reviewed-ref") _
// A configured library could expose a narrow callable interface.
// The library and its implementation are not supplied here.
The name and ref are synthetic teaching aliases for explicitly declared context, not a real repository or verified pin. No library is loaded, fetched, installed, or trusted by this course.
Keep helpers narrow and context-aware
A helper accepting a fixture mode can validate plain or unicode and produce a small result record. A helper accepting arbitrary shell code, Jenkins object paths, or credential identifiers can become a broad privilege gateway. State whether callers must already hold an agent context and whether the helper invokes Pipeline steps. Avoid persistent global mutable state as an informal cross-run database. Library methods still obey Pipeline persistence and CPS constraints. Design changes to outputs or result handling as interface changes requiring caller review, not private refactoring.
Test a pure domain helper outside Jenkins
The complete local program below validates a format mode and returns a plain serializable record. It models the contract a shared helper might enforce; it is not Groovy, Shared Library loading, or a Jenkins unit-test harness. Pure business transformations can often be tested outside orchestration while platform integration remains separately verified. The rejection case matters as much as the successful case. Preserve a small supported input language rather than turning library convenience into unrestricted execution under trusted controller or agent authority.
def fixture_record(mode):
if mode not in {"plain", "unicode"}:
raise ValueError("unsupported mode")
return {"mode": mode, "component": "label"}
print(fixture_record("plain"))
try:
fixture_record("arbitrary-command")
except ValueError as error:
print(error)
{'mode': 'plain', 'component': 'label'}
unsupported mode
The program executes only Python over declared strings. It does not validate Groovy CPS, plugin steps, credentials, or library trust configuration.
Common errors and fixes
A library is marked trusted to bypass a method rejection.
- Why it happens
- A language restriction is removed without reviewing the resulting authority.
- How to fix it
- Use supported APIs or a narrowly reviewed trusted implementation where genuinely required.
- Prevent it next time
- Treat trusted library source control as a high-authority boundary.
A shared output changes and downstream jobs silently read empty data.
- Why it happens
- The reusable interface was treated as private implementation.
- How to fix it
- Inventory callers and migrate or version the contract.
- Prevent it next time
- Test missing outputs, result changes, and compatibility before rollout.
A library helper accepts arbitrary caller shell code with credentials.
- Why it happens
- Reuse exposes an unrestricted execution gateway.
- How to fix it
- Offer narrow validated operations and separate privileged authority.
- Prevent it next time
- Review every input as data, code, identity, or capability.
Check your understanding
1. Does a commit pin make a trusted library safe without reviewing its source?
Show worked solution
No. It identifies content but does not constrain its authority or prove harmlessness. Review implementation, repository writers, nested dependencies, and permitted execution. Maintain an update process so the pin does not become permanent neglect.
2. A helper is pure string normalization. Must it be tested only on a Jenkins controller?
Show worked solution
No. Test the pure domain logic with an appropriate local language tool where available. Separately verify the actual Groovy and Pipeline integration in an authorized environment. Do not label a Python model as a successful Jenkins library test.
Key takeaways
- Shared code carries interface and authority contracts.
- Separate pure logic evidence from actual Pipeline integration.