Shared libraries and the sandbox
One reviewed pipeline vocabulary, pinned by tag, and what trust it carries.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/sharedlibs.tar.gz && tar -xzf sharedlibs.tar.gz, which creates ~/lab/sharedlibs/. SHA-256: 28e44ac0ee1a7a0fb5915b7621915575c09a2beaa7af772aa53282d929c4f2c6Forty repos each have a copy of the same deploy stage, and each has drifted a little. A security fix means forty pull requests, and some will sit for weeks. Shared libraries solve this: common pipeline code lives in one repo, and every Jenkinsfile calls it. But they also come with a catch you need to know. A global library is trusted code, with more power than the Jenkinsfiles that call it, so whoever can change it holds the keys to your Jenkins.
A library is a Git repo
A file in vars/ becomes a step, named after the file:
// A pipeline step every repo can call: deployTo('staging')def call(String target) {echo "lib v1: deploying to ${target}"}
The library is registered with Jenkins through JCasC, like the rest of the configuration:
unclassified:globalLibraries:libraries:- name: labdefaultVersion: v1allowVersionOverride: trueimplicit: falseretriever:modernSCM:scm:git:remote: git://git/lab-lib.git
defaultVersion: v1 is the key line. Jobs that don't ask for a version get tag v1, so a push to the library's main branch can't change every pipeline at once. implicit: false means a Jenkinsfile must load the library explicitly. A Jenkinsfile then calls it like any other step:
@Library('lab') _pipeline {agent { label 'linux' }stages {stage('Deploy') { steps { deployTo 'staging' } }}}
The _ after the annotation is required syntax; the annotation has to attach to something. Publish two tagged versions, then run one job on each:
Loading library lab@v1 confirms the pin. Version 2 is already on the Git server, and this job didn't change behaviour, because nothing asked for it. A team can move at its own pace with one line in its Jenkinsfile:
@Library('lab@v2') _pipeline {agent { label 'linux' }stages {stage('Deploy') { steps { deployTo 'staging' } }}}
The sandbox, and what's outside it
Version 2 called hudson.model.User.getAll(), which reaches into the controller. Try the identical call directly from a Jenkinsfile:
pipeline {agent { label 'linux' }stages {stage('Reach for the controller') {steps {script { echo "Jenkins has ${hudson.model.User.getAll().size()} user(s)" }}}}}
A pipeline from Git runs in the Groovy sandbox, and it's stopped with Scripts not permitted to use staticMethod hudson.model.User getAll. The same call worked in the library, because global libraries are trusted and run outside the sandbox. Notice that the job asked for lab@v2 and it just worked, with no approval from anyone.
So treat the library repository like the controller itself. Require reviews and protected tags on it, since a change there runs code on the controller for every job that loads it. And allowVersionOverride: true, which this lab enabled so you could try @v2, also lets any Jenkinsfile load any branch in that repo with trusted privileges. Consider turning it off, or keeping experiments in a folder library.
Jenkins, User, files or credentials; move that logic behind a reviewed library step instead.Hands-on
In a fresh lab, publish the library with tags v1 and v2 (./seed.sh lab-lib lib/, then git -C lib tag and git -C lib push origin v1 v2), and create the pinned and latest jobs. Set allowVersionOverride: false in casc.d/10-library.yaml and apply it with ./jk.sh /configuration-as-code/reload -X POST, which re-reads the YAML without a restart (the JCasC lesson covers it). Run latest, read the error, then run pinned. Finish with ./down.sh.
What you should see: latest fails at once with Version override not permitted for library lab, before any stage runs. pinned still loads lab@v1 and succeeds. With the override off, the library's maintainers decide which version runs, not each Jenkinsfile.
Remember
Pin libraries to tags, so a push can't change every pipeline at once. getAll() was blocked in the Jenkinsfile and allowed in the library, so review a global library like controller code.
defaultVersion: main. Someone merges a mistake to main on a Friday evening. What happens?Scripts not permitted to use staticMethod .... A developer asks you to approve it in Script Approval so their build can pass. What should you weigh?