How to Create a Jenkins Shared Library Tutorial (Step by Step). If you would like to know about Jenkins servers then this post will help you how to create a Jenkins shared library post, how to load them and how to use them in the Jenkins pipeline. Let’s get started.
Before dive into creating a Jenkins library, we would like to give you some basic knowledge of Jenkins.
Jenkins is an open source and popular automation server that helps developers around the world to automate the software development process such as building, testing, and deploying. Jenkins is written in Java with plugins built for Continuous Integration purposes, which is a very important tool for DevOps. It achieves that with the help of plugins (Git, Maven 2). Jenkins server also provides continuous integration and continuous delivery of the software components. Jenkins main aim is accelerate the software development process through automation. This is a server based system that mainly runs on the servlet containers (the popular one is Apache servlet).
Copying and pasting the same code many times into Jenkins pipeline is really a big issue. To avoid such a thing, we need the Jenkins library.
The Jenkins library helps in storing the reusable codes. So you need to write your code once, then you can share the same code (which you have built ) with different Jenkins pipelines.
Note: Is it possible to reuse the code in Jenkins?
Yes in order to customize the already written code, please follow the steps below:
First, read the configuration File of the Jenkins pipeline.
Then you need to perform a code review of the application (use the tool SonarQube).
Perform some checks on the target environment.
The final step is to perform a deployment.
Then you can add these codes into a shared library, so that other projects can reuse them.
Over a period, you can build a collection of the functions in your Jenkins library.
In this section of how to Create a Jenkins Shared Library, we are going to explain steps that are required to create the Jenkins shared library:
First, you need to create the Groovy scripts -> then add them into your Git repositories.
Then you can add your shared library into Jenkins from the “manage Jenkins screen”.
Finally, you need to pull the shared library into the Jenkins library using the standard annotations which are available on the top of Jenkins(Files).
The shared library in the Jenkins looks like this:
@Library('your-library-name')
What’s inside of the Jenkins library?
Once you created the shared library in the Jenkins, you will get to see two types of common code:
1. Steps
It is a collection of Global variables in the Jenkins terminology. But these are the “customized steps” that will be available to all the Jenkins pipelines.
For example, to deploy an application you might write a standard step then you review the code. To do this follow the navigation, add your customized codes into vars/YourStepName.groovy and then implement one more function that is def call function like this:
This might consist of helper classes or sometimes you want to include the common codes inside the pipelines. You can also use common code to store the static constraints which are used later throughout the pipeline.
The navigation to use the common codes, go to the file src/your/package/name in the directory and then you can use the groovy syntax.
For example, consider a common file xyz/tomd/GlobalVars.groovy (here you can notice the XYZ.tomd at the top).
Each of your steps is completely different inside the “groovy” file as well as in the “Vars/” directory. In Jenkins the variables are known as “Global variables” and located in the directory “Vars/”.
Now you have to create a file for your custom step and fill the below code:
Notice that how Groovy script is implemented in the method “call”. After writing the above code custom codes within the braces {}. Also, you can add parameters for example name, and human.
Now you have a library which is created using custom steps, so need to connect this library with Jenkins. Follow the steps to perform this task:
You have to define a shared library within a Jenkins file or you can configure the library by using the Jenkins web console.
Note: it’s always good to configure the shared library using the Jenkins web console so that you can share the library across all your build jobs.
2. In Jenkins, go to manage Jenkins -> then select the configure system. Under the “Global pipeline libraries”, add the shared library with the following details:
Name : pipeline-library-demo
Default version: need to specify Git reference (for example branch or commit SHA).
To use the Shared library in the Jenkins pipeline add “@Library(‘your-library-name’)” to the aerial definitions or any Jenkins file. Consider the below example:
How to Create a Jenkins Shared Library Tutorial (Step by Step) Conclusion
In this how to create a shared library in Jenkins post, we have explained how to set up the Jenkins server application, how to create the shared library, and how to connect the shared library to the Jenkins.
It’s important to have automation and streamlining the development process in today business. CI/CD is central to devops as it grows, scale the business and takes it to the next level.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.