Jenkins Shared Libraries: Reusable, Scalable, and Efficient Pipelines

Jenkins Shared Libraries are reusable code snippets that help you centralize and modularize common Jenkins pipeline logic. They allow you to write custom functions, steps, or entire pipeline logic in Groovy and reuse them across multiple Jenkinsfiles.


πŸ”Ή Why Use Shared Libraries?

  1. Code Reusability – Avoid duplicating pipeline code across multiple projects.

  2. Maintainability – Easier to update pipeline logic in a single place.

  3. Modularity – Separate business logic from pipeline code.

  4. Security – Reduce security risks by defining approved scripts.


πŸ”Ή Setting Up a Jenkins Shared Library

1️⃣ Create a Repository for the Library

Shared libraries are typically stored in a Git repository.

2️⃣ Define the Library Structure

A Shared Library follows a specific structure:

vbnetCopyEditπŸ“‚ shared-library/
 β”œβ”€β”€ πŸ“‚ src/                 # Groovy classes (optional)
 β”‚   β”œβ”€β”€ com/example/MyClass.groovy
 β”‚
 β”œβ”€β”€ πŸ“‚ vars/                # Reusable pipeline functions (global variables)
 β”‚   β”œβ”€β”€ myCustomStep.groovy
 β”‚   β”œβ”€β”€ deployApp.groovy
 β”‚
 β”œβ”€β”€ πŸ“‚ resources/           # Static resources (config files, templates)
 β”‚
 β”œβ”€β”€ πŸ“œ README.md            # Documentation
 β”œβ”€β”€ πŸ“œ build.gradle         # Optional (if using Gradle)
 β”œβ”€β”€ πŸ“œ pom.xml              # Optional (if using Maven)

3️⃣ Writing a Simple Shared Library Function

Global Function (Inside vars/)

Create a Groovy script inside the vars/ directory.

πŸ“„ vars/helloWorld.groovy

groovyCopyEditdef call(String name = 'User') {
    echo "Hello, ${name}! Welcome to Jenkins Shared Libraries!"
}

βœ… This function can be used in any pipeline by calling helloWorld().


4️⃣ Configuring the Shared Library in Jenkins

  1. Go to: Jenkins Dashboard β†’ Manage Jenkins β†’ Configure System.

  2. Scroll to: "Global Pipeline Libraries."

  3. Add a new library:

    • Name: my-shared-library

    • Default version: main (or specify a branch/tag)

    • Source Code Management: Select Git and provide the repo URL.


5️⃣ Using the Shared Library in a Pipeline

You can use the shared library in a Jenkinsfile in two ways:

If the library is configured in Jenkins, use:

groovyCopyEdit@Library('my-shared-library') _
pipeline {
    agent any
    stages {
        stage('Test Shared Library') {
            steps {
                helloWorld('DevOps Engineer')
            }
        }
    }
}

b) Load Dynamically (Without Pre-configuring in Jenkins)

If you don't configure it globally, load it directly:

groovyCopyEdit@Library('github.com/my-org/shared-library@main') _
import helloWorld

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                helloWorld('John')
            }
        }
    }
}

πŸ”Ή Advanced Example with a Class

If you need structured Groovy classes, define them inside src/:

πŸ“„ src/com/example/Utils.groovy

groovyCopyEditpackage com.example

class Utils {
    static String greet(String name) {
        return "Hello, ${name}!"
    }
}

πŸ“„ vars/greetUser.groovy

groovyCopyEditimport com.example.Utils

def call(String name) {
    echo Utils.greet(name)
}

πŸ“„ Jenkinsfile

groovyCopyEdit@Library('my-shared-library') _
pipeline {
    agent any
    stages {
        stage('Greeting') {
            steps {
                greetUser('Alice')
            }
        }
    }
}

πŸ”Ή Best Practices

βœ… Use Versioning – Tag releases or use branches (@v1.0, @develop).
βœ… Avoid Hardcoding Credentials – Use Jenkins Credentials Plugin.
βœ… Write Tests – Validate shared libraries with Unit tests.
βœ… Document Functions – Keep a README.md for usage instructions.
βœ… Use Static Code Analysis – Tools like SonarQube for quality checks.

0
Subscribe to my newsletter

Read articles from Ishika kesarwani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ishika kesarwani
Ishika kesarwani