Coder Social home page Coder Social logo

jenkins-sharedlibrary's Introduction

JENKINS SHARED LIBRARY

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

Let’s say we are supporting five micro services in the project typically, all five microservices need their own Jenkinsfile, but the content of the Jenkinsfiles is going to be mostly the same except for some inputs. Jenkins Shared Library avoids this repetition of pipeline code by creating a shared library.

Steps to create Jenkins shared library:

Step 1: Create vars folder

Create a Git repository and create a directory called vars, which will host the shared library’s source code (file extension .groovy)

Step 2: Create Groovy file

Create a file <name.groovy> inside the vars folder (camel casing is mandatory for file names). The filename will be used later by Jenkinsfile to access this Jenkins pipeline library.

Step 3: Create call() function inside Groovy file

When a shared library is referred from the Jenkins job, Jenkins, by default, will invoke the call() function within our Groovy file. Consider the call() function like the main() method in Java. We can also specify parameters for the call() function if we want to.

Steps to configure Jenkins pipeline library:

In manage jenkins, click on configure system and search for Global Pipeline Libraries section.

Under the Library section, configure values as below:

  • Name (remember, we will refer to this shared library name from Jenkinsfile).
  • 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 shared library is stored in private repo

Referring Jenkins Shared Library from Pipeline

Call the shared library under the pipeline section

@Library('testlibrary')_
kubernetesShell('dockerhub', 'artisantek/useraccount', \
'ver', 'https://github.com/artisantek/nodejs-useraccount.git', 'main', 'github', \
'webapp-deployment', 'nodejs')

@Library will import the shared library configured under manage jenkins to our Jenkins job.

_ (underscore) is a must after the @Library annotation.

kubernetesShell will invoke the call() function in kubernetesShell.groovy created under vars folder.

The ‘dockerhub’ string and others will be sent as a parameter to the call() function


This repo consists of groovy scripts, used to continously integrate and continously deliver the projects in both docker and kubernetes/eks environmenrts.

Shell --> Uses only shell commands with piplenine declarative synatx

Script --> Uses script block to perform various operations

jenkins-sharedlibrary's People

Contributors

artisantek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.