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.
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
Generating a Jenkins-shared-library.
- Create a git repository called “global-pipeline-libraries”. Create 3 folders inside it in the format shown in the following image.
- 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
- “vars” directory contains the files/functions which can be directly accessed from the Jenkinsfiles of the pipeline.
- Create a “mavenBuild.groovy” file inside the “vars” folder.
- After adding the piece of code, push the code to the git repository.
- In the above code, we have an args variable, value for these will come from the pipeline.
Configuring Jenkins to use the shared library.
- Navigate to Jenkins-> Manage Jenkins-> Configure System.
- Locate the section called “Global Pipeline Libraries”.
- Configure all the fields as shown in the image.
- Name can be anything, this name will be used by our pipeline. Versions can be based on branchName, tagName, etc.
- 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.
- Click on apply.
Creating a pipeline for maven projects using Jenkins Shared Library.
- Let’s generate a sample spring-boot application using https://editor.swagger.io
- Once you open the above link, it will open a swager specification for a Petstore.
- Now, go to Generate Server-> spring.
- This will generate a zip file, just extract it to a workspace.
- 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.
- Create a file called Jenkinsfile alongside pom.xml.
- Add the following code snippet to it.
- 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..
- 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”.
- Now, pass on the 3 parameters to it as these are used by “Jenkins-shared-library”.
- Push the code.
- Now, inside Jenkins create a “Multibranch Pipeline” project (Jenkins->New Item-> Multibranch pipeline).
- Add Branch source as “Git” and add the project url to “Project Repository” textbox.
- Click Finish.
- Trigger the build, once the build is successful, your code will be deployed to the local .m2 repository.
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.