Skip to content

Latest commit

 

History

History
94 lines (78 loc) · 2.77 KB

File metadata and controls

94 lines (78 loc) · 2.77 KB

JENKINS SHARED LIBRARY

image

REF LINK : https://github.com/pavankumar0077/Jenkins-Zero-To-Hero/tree/main/shared-libraries REF LINK : https://github.com/praveen1994dec/jenkins_shared_lib/tree/main/vars

Problem

  1. We hava 500 + microservices then that are individually developed and deployed, For every microservices we have to write jenkins file for the CI CD part.
  2. Now we you want to change the configuration like some commands in pipeline, Then going to 500 microservices and changing the config is very big task, Even you have wrote jenkins file for once and rest copies for 500 microservices.

Solution -- Jenkins Shared Library

  1. It is nothing but a commone or repetative or reusable code Ex: MAVEN BUILD IS same for all the applicaitons.
  2. Jenkins provide a concept called SHARED LIBRARIES, You need to put the reusable code as a library in GIT REPO anf reference the LIBRARY in our stage or pipeline or jenkins file
  3. Now we will take reference the library and use it in the jenkins file
  4. @Library("my-shared-library) _ This should to used to import the library and add the library details in the System or Tool settings in jenkins
  5. Change in one place and it will changed in all the jenkins files.
    • vars
    • src
    • resources

Normal pipeline

pipeline {
    agent any

    stages {
        stage('Greetings') {
            steps {
                echo 'Hi Techie'
            }
        }
    }
}

Step 1: Create a Git Repo for JENKINS SHARED LIBRARY Step2 : Create vars folder --> Create the scripty --> maven-test.groovy (Ex) (Camalcase - helloWorld.groovy) Step3: Congigure the shared library in jenkins --> Managme jenkins --> systems --> Global piple image Step4: In the jenkins file we have to import the shraed lib --> @Library("my-shared-library") _ Here _ is represents everything in the library

Shared lib pipeline

@Library("my-shared-library") _
pipeline {
    agent any

    stages {
        stage('Greetings') {
            steps {
                helloWorld()
            }
        }
    }
}

Here we are using helloWorld() --> IT IS FILE NAME NOT THE FUNCTION NAME image

def call() {
  sh 'mvn clean install'
}
@Library("my-shared-library") _
pipeline {
    agent any

    stages {
        stage('Greetings') {
            steps {
                helloWorld()
            }
        }
        
        stage('Maven Build') {
            steps {
               mvnBuild()
            }
        }
    }
}