Shared Libraries in Jenkins Pipeline: A Comprehensive Guide with Examples

Anshul
3 min readOct 10, 2023

--

Jenkins is a powerful and widely used open-source automation server that enables teams to automate various aspects of the software development and deployment process. Jenkins Pipelines, a key feature of Jenkins, allows you to define and manage your build, test, and deployment processes as code. One of the most valuable tools at your disposal when working with Jenkins Pipelines is the use of shared libraries. In this article, we’ll explore the concept of shared libraries in Jenkins Pipelines and provide real-world examples to demonstrate their practical use.

What Are Shared Libraries?

Shared libraries in Jenkins Pipelines are reusable pieces of code that can be organized into functions and classes. These libraries allow you to encapsulate common logic, making it easier to maintain and share across multiple pipelines and projects. By utilizing shared libraries, you can enhance the modularity, reusability, and maintainability of your Jenkins Pipeline scripts.

Advantages of Using Shared Libraries:

1. Code Reusability: Shared libraries enable you to define common functions and classes that can be used across multiple Jenkins Pipelines, reducing duplication of code and making maintenance more manageable.

2. Standardization: You can establish coding standards and best practices within your organization by centralizing shared functionality in libraries. This ensures consistency in your CI/CD processes.

3. Improved Collaboration: Shared libraries make it easier for teams to collaborate on Jenkins Pipelines, as they provide a common set of building blocks that everyone can leverage.

4. Ease of Maintenance: When a change or improvement is needed, you can update the shared library in one place, and all pipelines using that library will automatically inherit the changes.

DevOps CI/CD with Jenkins Pipeline and SonarQube Integration

DevOps CI/CD with Jenkins Pipeline and SonarQube Integration

Creating a Shared Library:

To create a shared library in Jenkins, follow these steps:

1. Create a Git Repository: Start by creating a Git repository to host your shared library code. This repository should have a specific structure that Jenkins understands.

2. Create a ‘vars’ Directory: Inside your repository, create a ‘vars’ directory. This directory will contain your reusable functions.

3. Define Functions: In the ‘vars’ directory, define your functions as Groovy files. Each file should contain a single function, with the filename serving as the function name. For example, if you have a function called `buildDockerImage`, create a file named `buildDockerImage.groovy` containing the function.

4. Commit and Push: Commit your changes to the Git repository and push them to a remote repository.

Using a Shared Library in a Jenkins Pipeline:

Now that you have created a shared library, you can use it in your Jenkins Pipeline scripts. Here’s an example:

@Library('my-shared-library') // Load the shared library
import com.example.pipeline.* // Import the functions and classes

pipeline {
agent any

stages {
stage('Build and Test') {
steps {
script {
// Use the shared library function
buildDockerImage()
}
}
}
stage('Deploy') {
steps {
script {
// Use another function from the shared library
deployToProduction()
}
}
}
}
}

In the example above, we load the shared library with `@Library` and import the functions and classes we want to use. Then, we can call these functions in our pipeline script.

Real-World Example: Using a Shared Library for Kubernetes Deployment

Suppose you have a shared library that simplifies Kubernetes deployment. Here’s how you might use it in a Jenkins Pipeline:

@Library('k8s-deploy-library') // Load the Kubernetes deployment library
import com.example.kubernetes.*

pipeline {
agent any

environment {
DOCKER_IMAGE = 'myapp:latest'
}

stages {
stage('Build and Push Docker Image') {
steps {
script {
buildAndPushDockerImage(image: DOCKER_IMAGE)
}
}
}
stage('Deploy to Kubernetes') {
steps {
script {
// Deploy to Kubernetes using the shared library function
deployToKubernetes(deploymentName: 'myapp', image: DOCKER_IMAGE)
}
}
}
}
}

In this example, we use the shared library to build and push a Docker image and then deploy it to a Kubernetes cluster. The shared library abstracts the complexities of these tasks, making the pipeline script more readable and maintainable.

Conclusion

Shared libraries in Jenkins Pipelines are a powerful tool for enhancing the modularity, reusability, and maintainability of your CI/CD processes. By centralizing common functionality in shared libraries, you can streamline your pipeline scripts, promote collaboration, and ensure consistency across projects. Start creating and using shared libraries in your Jenkins Pipelines to unlock their full potential and simplify your automation workflows.

--

--

Anshul

DevRel 🥑 DevOps / Cloud Engineer | Terraform, Ansible, Docker & Kubernetes Enthusiast 🐳 GCP | Jenkins | Terraform Certified