Learn › Jenkins

Jenkins Shared Libraries and Approval Gates for Production Changes

Reuse pipeline code with Jenkins shared libraries, and make production changes wait for a human with input steps, timeouts and locks.

Advanced 12 min read
By Gopinath Karthikeyan

When you have twenty pipelines, the same validation, notification and reporting code appears in all of them – with twenty slightly different bugs. A shared library puts that code in one place. And for jobs that change production, an approval gate makes sure a person checks the plan first.

Shared library layout

A shared library is a separate Git repo:

jenkins-shared-lib/
├── vars/
│   ├── validateTarget.groovy     # callable as validateTarget(...)
│   └── notifyTeam.groovy
└── src/org/winops/
    └── Utils.groovy              # classes, if you need them

vars/validateTarget.groovy:

def call(String target) {
    if (!(target ==~ /^[A-Za-z0-9._-]+$/)) {
        error("Invalid target '${target}'")
    }
    if (target in ['all', 'prod']) {
        error("Target '${target}' is too broad – choose a specific group")
    }
    echo "Target '${target}' validated"
}

Register it

Manage Jenkins → System → Global Trusted Pipeline Libraries (or Global Pipeline Libraries): name winops-lib, default version main, retrieval method Git with your repo URL and a read-only credential.

Use it

@Library('winops-lib@main') _

pipeline {
    agent { label 'linux && ansible' }
    parameters { string(name: 'TARGET', defaultValue: 'lab') }
    stages {
        stage('Validate') { steps { validateTarget(params.TARGET) } }
    }
}

Pin to a tag (@Library('winops-lib@v1.4')) for critical pipelines so a library change can’t break them unexpectedly. Changes to the library go through pull requests like everything else.

Approval gates with input

The pattern for production changes: plan → approve → apply.

stage('Plan') {
    steps {
        sh 'cd ansible && ansible-playbook patch.yml -l "$TARGET" --check --diff | tee plan.txt'
        archiveArtifacts artifacts: 'ansible/plan.txt'
    }
}
stage('Approve') {
    steps {
        timeout(time: 4, unit: 'HOURS') {
            input message: "Apply changes to ${params.TARGET}?",
                  ok: 'Apply',
                  submitter: 'windows-leads'     // user or group allowed to approve
        }
    }
}
stage('Apply') {
    steps { sh 'cd ansible && ansible-playbook patch.yml -l "$TARGET"' }
}
  • The approver reads the archived plan before clicking Apply.
  • submitter limits who may approve – ideally not the person who started the build.
  • timeout aborts the build if nobody approves, instead of leaving it hanging for days.
  • If the pipeline uses agent none at the top and declares an agent on each stage, the approval stage holds no executor while it waits.

Don’t run two changes at once

The Lockable Resources plugin prevents two builds touching the same environment simultaneously:

stage('Apply') {
    steps {
        lock(resource: "env-${params.TARGET}") {
            sh 'cd ansible && ansible-playbook patch.yml -l "$TARGET"'
        }
    }
}

Add options { disableConcurrentBuilds() } for jobs that must never overlap.

Audit trail

Jenkins records who started each build, who approved the input step, and the full console log. Keep builds long enough for your audit needs (buildDiscarder), and archive plans and reports as artifacts – see Jenkins reports and artifacts.

FAQ

Shared library code runs with extra rights – is that a risk? Global trusted libraries run outside the Groovy sandbox, so treat that repo as highly privileged: restrict who can merge to it and review every change.

What’s next

Next: HTML reports and artifacts in Jenkins.

Practise this topic

Test yourself with free questions on this topic – instant answers and explanations.

Start practice questions

JenkinsShared LibraryApprovalsChange ManagementAdvanced

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in Jenkins