How To Use Shared Libraries In A Jenkins Pipeline?

Reading Time: 3 minutes

Jenkins Pipeline:

 the Jenkins, a pipeline is the smallest unit every Jenkins user interacts with.

Jenkinsfile is just a text file where we write pipeline code to define the Jenkins pipeline. It can be check into source control like Git along with our project source code. In most cases, every application will have its own Jenkinsfile to build, test & deploy.

There are two ways of writing a Jenkinsfile –

  • Scripted Pipeline: Syntax for pipeline is strictly Groovy
  • Declarative Pipeline: A new way of writing Jenkinsfile

Jenkins Shared Library

In today’s Microservices world large monolith applications are broken down into small applications & deploy separately. Interestingly, most of these applications have the same steps to build, test & deploy the application.

A shared library is a collection of independent Groovy scripts which you pull into your Jenkinsfile at runtime. The best thing is that the Library can be store in a Git repository. This means that we can version, tag, and do all the cool stuff we’re use to with Git.

Why Use Jenkins Shared Library?

Jenkins Shared Library avoids this repetition of pipeline code by creating a shared library.

Here are steps to create & use the Jenkins shared library:

  • Create a separate git repo for the Jenkins pipeline library & push the shared library code to that repo.
  • Integrate the shared library repo in Jenkins under the Manage Jenkins section.
  • Create Jenkinsfile in the project. In that Jenkinsfile, Import & use the shared library.

Creating Jenkins Shared Library

Jenkins Shared Libraries are push into separate Git repository & check out by Jenkins job.

Steps to create Jenkins shared library:

Step 1: Create vars folder

Create a separate project directory. Inside the project directory, create a directory called vars, which will host the shared library’s source code (usually in files with extension .groovy

Step 2: Create Groovy file

We create a file called welcome inside var folder.

vars
welcome

Step 3: Create call() function inside Groovy file.

Consider the call() function like the main() method in Java.

def call(String name = 'User') {
 echo "Welcome, ${name}."
}

In the above example, the name is the parameter.

Now commit & push the contents of the vars folder to a remote git repository.

Configure Shared Library in Jenkins

To configure the Shared Library in Jenkins,follow the steps:

Steps to configure Jenkins pipeline library:

1: Open Jenkins in your browser. (if local, http://localhost:8080/)

2: Click on manage jenkins on the left side menu.

3:  click on configure system.

4: we find configure values in the  Library section

  • Name (remember, we will refer to this shared library from Jenkinsfile using this name).
  • Default version (branch name of our Shared Library git repo).
  • Under the Retrieval method, choose Modern SCM.
  • Under Source Code Management, choose Git.
    • Enter your Pipeline Shared Libraries repo URL under Project Repository
    • Configure credentials if your repo requires credentials to checkout code

5: Click on the save button.

Referring Jenkins Shared Library from Pipeline

Step 1: In Jenkins, click on New Item on the left side menu, enter Jenkins job name & choose the pipeline as style & click on the OK button.

Step 2: Inside the job, scroll down to the pipeline section. Here we need our Jenkinsfile, which refers to the Shared Library.

Step 3: Click on Build Now button on the left side menu to trigger the build.

Step 4: Once the build is complete, check the logs by clicking on the Console Output from the left side menu.

Conclusion

Jenkins Pipeline is a set of plugins which support implementing and integrating pipelines into Jenkins.Jenkins Shared library is the concept of having a common pipeline code in the version control system that can be use by any number of pipelines just by referencing it.

Written by 

I am reliable, hard-working with strong attention to detail and eager to learn about new technologies and business issues. I am able to work well both on my own initiative and as part of a team.