Operations 5 min read

Configuring and Using Jenkins Shared Libraries for Flexible Pipelines

This tutorial explains how to configure Jenkins shared libraries, create Groovy utility classes, and integrate them into pipelines, covering library structure, code examples, required plugins, and usage within Jenkinsfiles to enhance DevOps workflows.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Configuring and Using Jenkins Shared Libraries for Flexible Pipelines

The module introduces Jenkins shared library configuration, enabling the creation of more flexible and powerful pipelines, and is suitable for anyone interested in DevOps.

A shared library is similar to a Python module: each Groovy file in the library is a class containing one or more methods, and each method holds Groovy code blocks.

1. Shared Library Content

The demo repository shows the typical structure:

── docs
│   └── README.md
├── src
│   └── org
│       └── devops
│           └── tools.groovy
└── vars

The src directory stores Groovy classes that are added to the classpath during pipeline execution. The vars directory holds script files that become globally available variables. The resources directory can contain non‑Groovy files accessed via the libraryResource step.

2. Creating the Shared Library

Create a file src/org/devops/tools.groovy and define a utility class with an example method PrintMes that prints a message in a specified color.

Note: Install the AnsiColor plugin to use the ansiColor() method for colored console output.

package org.devops

// Format output
def PrintMes(value, color) {
    colors = [
        'red'   : "\033[40;31m >>>>>>>>>>>>>${value}<<<<<<<<<< \033[0m",
        'blue'  : "\033[47;34m ${value} \033[0m",
        'green' : "[1;32m>>>>>>>>>>${value}<<<<<<<<<<[m",
        'green1': "\033[40;32m >>>>>>>>>>>>>${value}<<<<<<<<<< \033[0m"
    ]
    ansiColor('xterm') {
        println(colors[color])
    }
}

3. Using the Shared Library

In Jenkins, go to System Configuration → Global Pipeline Libraries , add a new library, set the name (e.g., jenkinslib ), default version (e.g., master ), and the repository URL (GitHub, GitLab, etc.). If the repository is private, provide credentials.

In a Jenkinsfile , load the library and use the class:

@Library('jenkinslib') _

def tools = new org.devops.tools()

pipeline {
    agent { node { label "master" } }
    stages {
        // Get code stage
        stage("GetCode") {
            steps {
                timeout(time: 5, unit: "MINUTES") {
                    script {
                        tools.PrintMes("获取代码", 'green')
                    }
                }
            }
        }
    }
}

Run the pipeline on Jenkins to verify that colored messages appear in the console output.

CI/CDDevOpsPipelineGroovyShared LibraryJenkins
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.