Jenkins Shared Libraries

Varun Tomar
3 min readApr 10, 2020

Overview:

There are a lot of good articles for Jenkins. A rollout in our case consists of two parts: Application code and Infrastructure. I am not going to talk about Jenkins. I am going to focus on how we are designing rollout using Jenkins Shared Library.

Depending on your requirement you can roll out a project in multiple ways. I am highlighting two options.

  1. Create a single repo that consists of all the components related to a project: app and infra.
  • app: application code
  • infra: consists of Terraform, Datadog, WAF, anything not related to the application

The idea behind this is anything related to a project resides in one repo. It makes it easier to navigate and less maintenance. However, Developers will not like DevOps making changes to the repo owned by Developers. It results in friction over time.

2. Create two git repos for every project (also called sister repo method):

  • Repo1: application code, docker image and it triggers Repo2 when application code is ready for rollout.
  • Repo2: configures the infrastructure (Terraform, Datadog, WAF, anything not related to the application).

In this case, we are splitting responsibilities between Developers and DevOps and each team takes full responsibilities of their own components. However, it results in a project being split into two different locations.

Whatever, method of deployment that you decide. Jenkins will play an important part. In era of microservices lot of small components need to work together to make an application work. As the number of deployments grows so does the complexity of managing them. One thing that will come really handy in creating the pipelines is a shared pool of scripts (groovy/bash/python) which collectively form the Global Pipeline Libraries. In our case, the Shared library consists of groovy and shell scripts.

Configure Shared Library:

Jenkins → Manage Jenkins → Configure System

If you choose Load Implicitly it means you don’t need to use the @Library tag in your pipelines to access your library, but it also ensures the shared library is loaded for all pipelines without exception.

You can clone this repo to get the basic structure of the Shared library that we are using. This repo contains a list of files that we have been using to build and deploy our CICD.

GitHub repo: https://github.com/tomarv2/jenkins-pipeline-library

Repo structure:

  • sample-jenkinsfile (artifact-repo: two use cases: Gradle and Maven projects, config-repo: deploy Infrastructure (k8s, Terraform)).
  • vars: List of all sh and groovy scripts
Screenshot of our Pipeline-library repo

How to use:

We created different types of Jenkins slaves from different applications. We currently run Jenkins slaves on k8s as pods (we have slaves for Mobile, Java, NiFi and Spark, and Infrastructure).

Any project that wants to use the shared library can use it by importing @Library('pipeline-library) and call the function from the library as needed in this case we are calling sendStartBuildNotification(env.channel) function which takes slack channelas input to which notifications can be sent.

Final thoughts:

In case, you want to use the repo in full or parts feel free to use them. I cannot take all the credit for the files in here, I got help from my teammates. Jenkins documentation for using Shared Libraries can be found: here

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Varun Tomar
Varun Tomar

No responses yet

Write a response