Skip to content

Jenkins Pipeline Library (collection of stages to manage jenkins)

License

Notifications You must be signed in to change notification settings

tomarv2/jenkins-pipeline-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jenkins Pipeline Library

Medium post: link

What is a Shared Library

A shared library is a collection of independent Groovy scripts which you pull into your Jenkinsfile at runtime.

The best part is, the Shared Library can be stored in a Git repository. It can be versioned, tagged, etc.

👋 Collection of jenkins stages which can be glued together to setup complex pipelines.

👋 Most of the groovy scripts are written for Kubernetes running on AWS. This Project offers sample pipelines to easily implement CI/CD processes. The corresponding "Shared Library" (vars) provides a set of "steps" to build your own scenarios beyond defaults.

What’s inside a Shared Library

Inside a Shared Library you’ll probably have two types of code:

👋 Steps: These are called Global Variables in Jenkins terminology, but these are the custom steps that you want to be available to all your Jenkins pipelines.

For example, you can write a standard step to deploy an application, or perform a code review.

👋 Other common code: This might include helper classes, or common code that you might want to include inside pipeline steps.

Prerequisites

Jenkins 2.277+

Pipeline Shared Libraries plugin

Other plugins may be required for specific library calls (i.e. AWS, Docker)

Getting started with Shared Library

This library consists of groovy and shell scripts.

Structure of the repo:

  • examples directory:

    • Build -> artifact-repo (covering two use cases: gradle and maven projects)
    • Deploy -> config-repo (manage k8s, manage Terraform)
  • vars directory(Shared Library):

    • List of sh and groovy scripts. Scripts are to build:

      • Manage docker images
      • Manage Kubernetes (Statefulset, Deployments, PVCs)
      • Basic Terraform validation
      • Run Sonarqube
      • Manage Git(commits, approvals)
      • Small components for OPA(Open Policy Agent), Spinnaker
      • Manage NiFi
      • Artifact management

Example usage:

Use library in a pipeline

To use shared library in a pipeline, you add @Library('your-library-name') to the top of your pipeline definition, or Jenkinsfile

@Library('pipeline-library-demo')_

stage('Demo') {
 echo 'Hello world'
 sayHello 'Dave'
}

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

Stage to send notification using Slack of build start:

stage('Start') {
   steps {
       sendStartBuildNotification(env.channel)
   }

Stage to apply Kubernetes definition based on branch name:

stage('Apply Kubernetes Definitions') {
   when {
       anyOf{
           branch "master"
           branch "production"
       }  
   }
   steps {
       applyKubernetesChangesContext()
   }
}

Note:

ℹ️ There is a plan to split Build and Deploy and use Jenkins for building and Spinnaker for deploying.

References: