Jenkins Shared Library Configuration

Veeresh Kumar
5 min readJan 30, 2022

Well, we face situations where written piece of code or functions are being duplicated or same code written again in different uses cases. It poses a question that why can’t we use the already existing piece of code?

You’re right!!! we often ask this question ourselves many times. That’s why we follow technic called as keep code DRY (DON’T REPEAT YOURSELF). It can be achieved by using the Jenkins Shared Library feature.

Before getting into the Jenkins Shared Library configuration, First let’s understand brief about Shared Library.

What is Shared Library?

  • It’s feature provided by Jenkins to centrally to store, access and share the piece of common groovy codes across all the Jenkins jobs.
  • The library can be stored and version controlled with git or any other version control repositories.

Why do we use Shared Library?

  • As Pipeline adoption increases for more and more projects in an organization, common patterns code and parameters are likely to emerge. Oftentimes it is useful to share parts of Pipelines between various projects to reduce redundancies and keep code “DRY”.
  • The Library supports calling custom groovy scripts and accessible across all pipelines.
  • We can store static non-groovy files like; json, txt, etc.. and consume. These are often called as external library resources.

Shared Library Directory Structure:

The directory structure of a Shared Library repository is as follows:

(root)
+- src # Groovy source files
| +- vkn
| +- labs
| +- testClass.groovy # for vkn.labs.test class file
+- vars
| +- globalVars.groovy # for global 'test' variable
|
+- resources # resource files (external libraries only)
| +- vkn
| +- labs
| +- sample.json # static helper non-groovy files
  • src: This directory can be used to store static or helper classes, or common code that would like to share across pipelines. This directory is added to the classpath when executing Pipelines.
  • vars: This directory hosts script files that are exposed as a variable in Pipelines. The name of the file is the name of the variable in the Pipeline. These are also called Global Variables in pipeline.
  • resources: This directory allows the libraryResource step to be used from an external library to load associated non-Groovy files.

Using libraries in Pipeline:

Shared Libraries marked Load implicitly allows Pipelines to immediately use classes or global variables defined by any such libraries.

To access other shared libraries, the Jenkinsfile needs to use the @Library annotation, specifying the library’s name:

/* Using a default configured library branch and version */ @Library('SharedLibraryName')_/* Using a version specifier, such as git tag version */
@Library('SharedLibraryName@1.0')_
/* Using a version specifier, such as git branch */ @Library('SharedLibraryName@branchName')_/* Accessing multiple libraries with one statement */
@Library(['SharedLibraryName', 'SomeOtherLib@branchName'])_

Note: The underscore (_) is not a typo! You need this underscore if the line immediately after the @Library annotation is not an import statement.

Writing Libraries:

At the base level, any valid Groovy Code is okay for use.

Example: I have added this greet function vars/gvars.groovy file into my shared library repo on github.

Let’s see how to consume this greet function on our pipeline.

Before that we need to configure our jenkins with shared library repo configuration:

  1. Go to Manage Jenkins → Configure System → Global Shared Libraries Configuration:
  2. Add the shared library name(it can be any name you can use at your convenience). Here have used it as jenkins-shared-library.
  3. Under Default Version: you mention whichever the branch to use. Here have used Master.
  4. Under Source Code Management, provide the Github repo URL and it’s credentials saved in your jenkins Credentials. Here I have used my repo for Shared Library is : https://github.com/veereshkumarn/jenkins.git
Shared Library Configuration

Save and Apply the Changes.

Github shared Library Repo Structure:

Where under vars directory contains groovy functions:

/vars/ directory contents: groovy files and here you can see gvars.groovy file, which will called in our pipeline.

/vars/gvars.groovy Content: Which has simple Greet() function with Default String Name value as “Veeresh”.

gvars.groovy
  1. created New Pipeline as Shared_library, with below configuration:

@Library(‘jenkins-shared-library’) : Calls the ‘jenkins-shared-library’ name which we had given in our earlier configuration. It’s mandatory to use the same and this configuration to make use of shared library functions or resources.

gvars.Greet() : this function is being called within pipeline.

shared_library pipeline config
shared_library pipeline config image

2. Now run and see the Pipeline output.

The above image shows our configured jenkins shared library is cloned automatically and gvars.groovy function is being called and it’s output is printed as expected.

Now lets modify the pipeline script by passing the string value to Greet function as “Kumar” and Its should print this value instead of Default Value “Veeresh”.

Post Running Build, See the below Console output:

Now you can see the passed new String value is being displayed in the output.

I hope you this will be helpful !!! Thank You !!

--

--

Veeresh Kumar

He is a Sr. DevOps Engineer loves to automate cloud infrastructure and App deployments. #Terraform #Azure #Jenkins #Ansible #Chef #Bash #PowerShell #AWS #GCP