Jenkins Shared Library Guidance

Mahdi Mallaki
ITNEXT
Published in
4 min readNov 22, 2023

--

Encapsulate functions across Jenkins pipelines using the Jenkins shared library

Introduction

Jenkins library has emerged as a powerful tool in the ecosystem, enabling developers to encapsulate and reuse functions within a library, effortlessly reducing code duplication. Its versatility and effectiveness make it a valuable asset for agile development teams seeking increased efficiency. Imagine the scenario where you need to create a new pipeline for a microservice; traditionally, this requires duplicating and customizing an existing Jenkinsfile. However, with Jenkins library, you can bid farewell to repetitive copy-pasting and opt for a more streamlined approach by simply invoking the necessary functions tailored to your new pipeline.

What’s Jenkins library?

A Jenkins library can contain several functions that you can encapsulate into a library and include in a Jenkinsfile pipeline to reduce duplicate code. you can learn more about it at this link.

Why Use a Jenkins Library?

A Jenkins library is useful for reducing duplicate code and increasing your agility. Let’s suppose you want to create a new pipeline for one of your new microservices. In this case, you would need to copy the content of one of your previous Jenkinsfile and customize it for the new pipeline. However, if you use a Jenkins library, you can avoid copying and pasting all the time. You just need to call the necessary functions for your new pipeline.

How to Create a Jenkins Library?

Creating a Jenkins library is quite easy. You just need to create a Git repository like this:

.
├── .git
├── README.md
├── src
│ └── AddSidebarLinkAction.groovy
└── vars
└── devops.groovy

As you can see in the above directory structure, I created a devops.groovy file inside the vars directory to contain my functions. Jenkins libraries should be written in Groovy scripts. Below is the content of the /vars/devops.groovy file:

def verifyK8sManifests() {

sh """
set -xe;
cd /tmp;
curl -Lk https://github.com/stackrox/kube-linter/releases/download/v0.6.5/kube-linter-linux.tar.gz -O;
tar -xvzf kube-linter-linux.tar.gz;

cd -;
for manifest_file in \$(find . -name '*.yaml'); do
/tmp/kube-linter lint \${manifest_file}
done

echo 'verified'
"""
}

In the aforementioned function, I’ve created an sh block to use Kube-Linter, which is a simple tool for verifying security concerns in my manifest files. I can call this function in the middle of my pipeline to check if there are any security problems with my manifests.

You can find this Jenkins library in the following GitHub repository:

Using Jenkins library

There are two ways to include and use a Jenkins library inside a Jenkinsfile. In the following sections, I’ll describe how you can use a Jenkins library:

1. Using the Library Directive

The first method involves using the library directive. You can include a Jenkins library like this:

library identifier: 'jenkinslib@master', retriever: modernSCM([
$class: 'GitSCMSource',
credentialsId: 'devops-creds',
remote: 'https://github.com/mlkmhd/jenkins-shared-library.git'
])

pipeline {

agent any

stages {

stage('test1') {

steps {

script {

dir("${buildOutputDir}") {
devops.verifyK8sManifests();
}
}
}
}
}
}

As you can see in the above simple pipeline, I included the Jenkins library using the library directive. This requires specifying your Git repository address and credentials to clone it before your pipeline execution. I called my function using devops.verifyK8sManifests(). Here, devops refers to the name of your script file (in my case, it's devops.groovy), and within that file, I have a function called verifyK8sManifests().

2. Using the Jenkins Admin Dashboard

There’s another way to include a function in your Jenkinsfile without the need for explicit inclusion. You need to navigate to Dashboard -> Manage Jenkins -> Configure System -> Global Pipeline Libraries and add the library like this:

As shown in the image above, I’ve added my Jenkins library to the Jenkins admin dashboard, enabling its inclusion within the Jenkinsfile. An important option in the above image is Load implicitly. When this option is selected, there is no need to explicitly include the Jenkins library in your Jenkinsfile. Jenkins will automatically load the library in all your pipelines without the need for manual inclusion. Consequently, you can modify the Jenkinsfile as follows (without needing to include library implicitly):

pipeline {

agent any

stages {

stage('test1') {

steps {

script {

dir("${buildOutputDir}") {
devops.verifyK8sManifests();
}
}
}
}
}
}

As you can see in the content above, I did not include any library directives because the library will be included automatically when the pipeline executes.

Conclusion

Harnessing the power of Jenkins library fuels productivity gains by minimizing code repetition and enhancing development agility. It facilitates the creation of streamlined and maintainable pipelines, ultimately enabling teams to focus on delivering quality software. By adopting either the explicit library directive or leveraging the implicit loading capability, developers can seamlessly integrate Jenkins library into their workflows and enjoy the benefits it brings to their CI/CD processes.

Support My Blog ☕️

If you enjoy my technical blog posts and find them valuable, please consider buying me a coffee at here. Your support goes a long way in helping me produce more quality articles and content. If you have any feedback or suggestions for improving my code, please leave a comment on this post or send me a message on my LinkedIn.

--

--