Jenkins Shared Library

Sanjeetkumar Mehta
Tech Prescient
Published in
5 min readNov 23, 2021

This blogpost is for any DevOps engineer whose day to day job is to build Jenkins pipelines for automating builds/deployment.

All the major programming languages provide us a way which we can use to write a code, and share it across multiple projects. These shared code are called libraries. Keeping the same concept in mind, Jenkins provides us a way which we can use to share the common Jenkins scripts across various projects. This helps us to reduce the code duplication and makes the management of scripts easier.

Some basic terminologies-

Jenkins: As described officially, Jenkins is the leading open-source automation server, Jenkins provides hundreds of plugins to support building, deploying, and automating any project.

Jenkins Pipeline: It is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. A continuous delivery pipeline is an automated expression of your process for getting software from version control right through to your users and customers.

Jenkinsfile: The definition of a Jenkins Pipeline is typically written into a text file which in turn is checked into a project’s source control repository.

For more information please visit https://www.jenkins.io.

“Jenkins shared library” in detail:

Jenkins Shared Library: As the pipeline is adopted for more and more projects in an organisation, common patterns are likely to emerge. Often it is useful to share parts of Pipelines between various projects to reduce redundancies and keep the code DRY (Do not repeat yourself).

First, I will describe how we leverage “Jenkins Shared Library” in our project and then I will do a short demo on how you can create your own shared libraries and use it.

In our project, we have multiple micro-services developed in java spring-boot, and it is then deployed using Kubernetes.

Now, as you can imagine, since all these services are developed using spring-boot, almost all the build and deploy steps remain the same. So, we have extracted the common functionalities to a separate project i.e. “Jenkins Shared Library” and then we just import it into our project’s Jenkinsfile. This helps us to keep our Jenkinsfile clean and easy to manage.

The following steps are typically required in our projects for building and deploying the code.

  1. Clean workspace before the build starts.
  2. Checkout project into Jenkins’s workspace.
  3. Validating license for the maven packages used in the project.
  4. Running test cases.
  5. Deploying the code to SonarQube for test case coverage and quality.
  6. Building a JAR and deploying it into Artifactory.
  7. Creating a docker image for the project.
  8. Pushing the created image to the private docker registry.
  9. Using the Helm charts to deploy the code to Kubernetes.
  10. Finally, cleaning the workspace.

As you can see, all the steps are generic so we have moved each of these to a shared library, and from our project, we just invoke the library and then pass on the parameters which are specific to the project. This helps us to reduce the size of Jenkinsfile which resides inside the project, and if we want to add/remove any step we can easily do so by modifying the shared library and it would be reflected across all the different services.

Implementation:

Generating a Jenkins-shared-library.

  1. Create a git repository called “global-pipeline-libraries”. Create 3 folders inside it in the format shown in the following image.
Folder structure of shared-library

2. For now we are only going to use the “vars” folder, if you want to read more about the significance of all the folders please visit https://www.jenkins.io/doc/book/pipeline/shared-libraries/.

3. “vars” directory contains the files/functions which can be directly accessed from the Jenkinsfiles of the pipeline.

4. Create a “mavenBuild.groovy” file inside the “vars” folder.

5. After adding the piece of code, push the code to the git repository.

6. In the above code, we have an args variable, value for these will come from the pipeline.

Configuring Jenkins to use the shared library.

  1. Navigate to Jenkins-> Manage Jenkins-> Configure System.

2. Locate the section called “Global Pipeline Libraries”.

3. Configure all the fields as shown in the image.

4. Name can be anything, this name will be used by our pipeline. Versions can be based on branchName, tagName, etc.

5. Project repository should be the URL of the git repository which we created for the shared library in the “Generating a Jenkins-shared-library” section.

6. Click on apply.

Creating a pipeline for maven projects using Jenkins Shared Library.

  1. Let’s generate a sample spring-boot application using https://editor.swagger.io.
  2. Once you open the above link, it will open a swagger specification for a Petstore.
  3. Now, go to Generate Server-> spring.
  4. This will generate a zip file, just extract it to a workspace.
  5. Navigate to the root of the project where the pom.xml file is located, and then do a mvn clean install, remove any error which you face.
  6. Create a file called Jenkinsfile alongside pom.xml.
  7. Add the following code snippet to it.

8. Now, if you carefully observe the above code, it has a tag called @Library, add the name of library and the version to it. In our case we created a library called “global-pipeline-libraries” and its version was “master” in section “Configuring Jenkins to use the shared library”, there is one “_” at the end, it should be there as we don’t have any import statements..

9. On the next line we have “mavenBuild”, this is the name of the groovy file which we added to the “vars” folder in section “Generating a Jenkins-shared-library”.

10. Now, pass on the 3 parameters to it as these are used by “Jenkins-shared-library”.

11. Push the code.

12. Now, inside Jenkins create a “Multibranch Pipeline” project (Jenkins->New Item-> Multibranch pipeline).

13. Add Branch source as “Git” and add the project url to “Project Repository” textbox.

14. Click Finish.

15. Trigger the build, once the build is successful, your code will be deployed to the local .m2 repository.

Summary:

So to conclude, “Jenkins shared library” is just like any other library which has a specific purpose and can be shared across multiple projects.

This helps us to reduce the duplication of scripts across different projects and helps us to manage the scripts easily.

Github repos:
Shared library: https://github.com/SanjeetMehta/global-pipeline-libraries
Project: https://github.com/SanjeetMehta/swagger-pet-store

Happy Coding!

We are Hiring!!

At Tech Prescient, we are always looking for good and passionate folks, who love challenges and like to own problems. If you think you are a change-maker who is hungry to create, develop, and influence; Don’t Wait!

Apply Now: https://cutshort.io/company/tech-prescient-Sdo5bhiP

--

--