Skip to content

Jenkins Shared Libraries, Versioning and Organization-Wide Standards ​

Engineering scenario: Twenty teams copy 300 lines of Pipeline logic. Propose a Shared Library that standardizes test, build and publish behavior without becoming a hidden cluster-admin backdoor.

Why use libraries? ​

A Shared Library centralizes reusable Pipeline steps and policies but must retain discoverability and version control. Jenkins recognizes a typical library structure:

text
(platform-ci)
  vars/
    buildService.groovy
    buildService.txt
  src/
    org/example/ci/Versioning.groovy
  resources/
    org/example/ci/config.json
  test/
    ...

vars/ exposes global vars/steps, src/ holds Groovy/Java-like class files and resources/ contains optional bundled data accessed through libraryResource. Exact testing structure is a project convention, not a built-in requirement.

Small and reviewable example ​

groovy
// vars/runChecks.groovy
def call(Map opts = [:]) {
    String command = opts.command ?: './scripts/test.sh'
    sh command
}
groovy
// Jenkinsfile, with a preconfigured global library
@Library('platform-ci@v1') _
pipeline {
  agent { label 'linux-small' }
  stages {
    stage('Test') {
      steps {
        checkout scm
        runChecks(command: './scripts/test.sh')
      }
    }
  }
}

Security note: A library API that accepts arbitrary shell commands can be misused. A stronger golden path constrains allowed operations and validates options. The example demonstrates syntax, not a hardened release library.

Trusted versus untrusted libraries ​

Trusted global libraries can use Jenkins core APIs and escape the Groovy sandbox, so commit access to that library may imply effectively unlimited controller privileges. Restrict repository write access, approval and default-version override. Folder libraries are untrusted, useful when different teams need scoped behavior. Do not put private keys or high-impact controller operations in code maintained by untrusted developers.

Version pinning limits surprise regressions. If every service loads floating main, a library push can break all pipelines at once. Prefer controlled versions (tags or commit SHAs) and phased promotion: library test, canary consumers, then broad rollout. A library version is not a container/image identity; record both.

Declarative limits and CPS ​

Shared libraries can define full Declarative Pipelines through supported vars/*.groovy patterns, but avoid nesting a new Declarative pipeline {} inside another. Pure Groovy utility methods must obey CPS/serialization rules if called from Pipeline. Keep substantial computation and tests in normal executables on agents.

Test strategy ​

  1. Test pure utility methods with a normal Groovy unit test framework where possible.
  2. Mock Jenkins steps using an approved Pipeline testing approach to verify arguments and branching.
  3. Run a sandbox Jenkins instance with the actual plugin set for syntax/step compatibility.
  4. Canary library updates with a small set of real consumers.
  5. Pin or rollback library versions if metrics show breakage.
  6. Inspect permission changes when adding new withCredentials, shell or controller API access.

Incident example ​

A change to the default global shared library replaces a try/finally around artifact cleanup. Hundreds of jobs start leaving temporary credentials/files. Rolling back a single application Jenkinsfile won't fix it because every job uses the same floating library revision. Re-pin the library to a known-good revision, invalidate vulnerable credentials/agents if needed, and add a regression test and canary policy.

Spoken explanation ​

“Shared libraries are useful for a stable high-level pipeline API: test, build, publish and report. I'd keep business-specific commands in repositories, version the library and test it like production code. Trusted libraries are effectively an administrative capability, so their SCM permissions need especially strong controls. I'd roll out changes gradually and keep Pipeline Groovy focused on orchestration, not arbitrary controller computation.”

References: Shared Libraries · CPS behavior

Practical engineering knowledge, openly shared.