September 25, 2023

Github Business

Business Printing

How to use Jenkins shared libraries in your pipelines

https://www.youtube.com/enjoy?v=4HTIBxufn-E

Jenkins shared libraries

You in no way want to pollute your Jenkins pipelines with superfluous Groovy code.

In truth, one of the explanations there is been a drive absent from scripted toward declarative pipelines is to cease builders from coding intricate Groovy routines exactly where they shouldn’t be. A little little bit of logic in a pipeline is all right. Elaborate programming? That must go in either a plugin or a Jenkins shared library.

For this Jenkins shared library instance, I have established a simple script that checks to see if the UAT workforce has placed a file named authorized.txt in a local folder of the Jenkins server. If that file is there, the Groovy logic returns real. If it isn’t, the logic returns wrong. When we make this Groovy script element of a Jenkins shared library, a declarative or scripted pipeline can use this logic and behave appropriately:

/* 
* jenkins-shared-library/src/com/mcnz/uatInput.groovy
* https://github.com/understand-devops-speedy/jenkins-shared-library.git
*/
offer com.mcnz

  public course uatInput 
    def buildIsUatApproved() 
    def file = new File("C:/_tools/accepted.txt")
    if (file.exists())
      return genuine
    
    else 
      println "Approval file does not exist."
     
    return fake 
  

With the Jenkins pipeline’s Groovy script saved to GitHub, the Jenkins shared library demands to be configured. This is performed under the Global Pipeline Libraries section underneath the Handle Jenkins web site of the admin console. The adhering to attributes need to be set for the shared pipeline library:

Title: shared-library
Default version (branch): grasp
GitHub URL: https://github.com/understand-devops-rapidly/jenkins-shared-library.git

Note that if you use a new GitHub repository for this Jenkins shared library instance, your default department may perhaps be named main, not grasp.

Jenkins shared library example

Jenkins shared libraries are configured as world wide pipeline libraries in the admin instrument.

Test the Jenkins shared library

To exam the Jenkins shared library from GitHub, produce a new pipeline position and incorporate a very simple scripted pipeline that references the shared library, imports the class, produces an cases and calls one particular of the procedures.

Be certain to uncheck the Use Groovy Sandbox alternative, or you may perhaps get an error such as the following, as sandbox protection forbids sure Groovy operations:

MissingPropertyException: No these kinds of house: groovy.lang.Binding

Jenkins sahred library test

Take a look at your shared Jenkins library with a uncomplicated scripted pipeline.

Declarative pipelines and shared libraries

This Jenkins shared library in GitHub is meant to be made use of in a conditional statement inside a declarative pipeline. In essence, if the approval file exists, a make is permitted to continue on. If not, the make stops.

This is just a simple illustration, but to put into action this kind of a procedure, a conditional declarative pipeline would glance as follows:

@Library('shared-library')
import com.mcnz.uatInput
def uatInput = new uatInput()

pipeline {
    agent any
    stages 
        phase ('Operate only if acceptance exists') 
            when 
                expression  uatInput.buildIsUatApproved() 
            
            ways 
                echo "The develop has been approved!!!"
            
        
    
}

When this declarative pipeline operates, the create will be conditional on no matter whether the Groovy code from the Jenkins shared library suggests the acceptance file exists or not.

Jenkins shared pipeline illustration overview

In summary, the techniques to use a produce a GitHub based, Jenkins shared library for pipelines are:

  1. Create GitHub repository with the Groovy scripts you wish to share
  2. Set up a International Pipeline Library in the Configure System webpage of the Deal with Jenkins tab
  3. Declare the shared library and import the lessons at the commence of your Jenkins pipeline scripts
  4. Disable the Groovy sandbox
  5. Run your CI/CD pipelines that use your shared Jenkins library

Never pollute your build pipelines with extreme amounts of logic and code. When you need to insert some sophisticated logic to your builds, a shared Jenkins library is the ideal way to make intricate logic out there to your scripted and declarative pipelines.