Jenkins Shared Library

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. This kind of shared code is referred as 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.

Sanjeet Mehta
Sanjeet Kumar

November 23, 2021 | 5 minutes read

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

Demo:

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.
  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 swager 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.

Happy Coding!