Advanced Jenkins

Shared Libraries in Jenkins

Configure Shared Library in Jenkins

Centralize reusable Groovy scripts by registering a Jenkins Shared Library. This guide covers:

  • Defining library functions (e.g., Slack notifications)
  • Adding a Shared Library in Jenkins
  • Referencing and importing library code in pipelines
  • Fetching third-party dependencies with @Grab
  • Bundling and retrieving resource files

The image shows a Jenkins documentation page about using shared libraries in pipelines, with a form for configuring a global trusted pipeline library.

1. Example: Slack Notification Script

Place this Groovy function under vars/notify.groovy in your Shared Library to send build status updates to Slack:

def call(String buildStatus = 'STARTED') {
    buildStatus = buildStatus ?: 'SUCCESS'
    def color = (buildStatus == 'SUCCESS') ? '#47ec05' :
                (buildStatus == 'UNSTABLE') ? '#d5ee0d' : '#ec2805'
    def msg = "${buildStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}\n${env.BUILD_URL}"
    slackSend(color: color, message: msg)
}
Build StatusColor Code
SUCCESS#47ec05
UNSTABLE#d5ee0d
FAILED/OTHER#ec2805

2. Register the Shared Library

  1. Go to Manage JenkinsConfigure System.
  2. Scroll to Global Pipeline Libraries.

Note

By default, libraries run in Jenkins’ Groovy sandbox.
Disabling the sandbox allows static calls, @Grab, and advanced APIs but bypasses script-security checks.

FieldDescriptionExample
NameUnique identifier for the libraryDasherSharedLib
Default versionGit branch or tag used when unqualifiedmain
Load implicitlyAuto-load library without @LibraryOff
Allow version overridePermit @Library('name@branch') to select alternate branchesEnabled
Include in job recent changesTrigger pipelines when the library code changesOpt-in
Retrieval methodSCM plugin to fetch the library (Git, SVN, etc.)Git
Project repositoryRepository URLhttps://github.com/...
  1. Click Add under Global Pipeline Libraries.
  2. Fill out the form using the table above.
  3. Click Apply and Save.

The image shows a Jenkins configuration screen for managing shared libraries, with options for setting the default version, loading implicitly, and retrieval methods using SCM.

The image shows a Jenkins system configuration page with options for managing shared libraries, including settings for library changes, retrieval methods, and source code management using Git.

3. Reference the Library in Your Pipeline

Add the library at the top of your Jenkinsfile:

@Library('DasherSharedLib@main') _
import com.mycorp.pipeline.slack.notify

pipeline {
    agent any
    stages {
        stage('Notify Start') {
            steps {
                notify('STARTED')
            }
        }
    }
}

3.1 Fetching Third-Party Dependencies

Trusted Shared Libraries can include @Grab annotations to pull Maven artifacts:

@Grab('org.apache.commons:commons-math3:3.4.1')
import org.apache.commons.math3.primes.Primes

void checkPrime(int count) {
    if (!Primes.isPrime(count)) {
        error "${count} is not prime"
    }
}

Retrieve packed resources (e.g., JSON templates) with libraryResource:

def template = libraryResource 'com/mycorp/pipeline/somelib/request.json'

Warning

@Grab and libraryResource require the library to run outside the sandbox.
Sandboxed mode will block these calls with security exceptions.

4. Next Steps

  • Create a pipeline to invoke your Shared Library methods.
  • Explore Pipeline Syntax for advanced usage.

Watch Video

Watch video content

Previous
Create a Shared Library for Slack Notification