Sling Academy
Home/DevOps/Jenkins: How to load external libraries into Pipeline

Jenkins: How to load external libraries into Pipeline

Last updated: February 03, 2024

Introduction

Jenkins is an open-source automation server that allows developers to build, test, and deploy their projects with ease. One of Jenkins’ most powerful features is its Pipeline, a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. A common requirement in these pipelines is the ability to use external libraries to reduce code duplication and foster reuse. This tutorial will guide you through the process of loading external libraries into your Jenkins Pipeline.

Prerequisites

  • Basic understanding of Jenkins and its Pipeline feature.
  • Jenkins instance with Pipeline plugin installed.
  • Access to a source control system (e.g., GitHub, Bitbucket) to host your external library.

Creating a Shared Library

Before we dive into loading external libraries, it’s important to understand the structure of a shared library in Jenkins. A shared library is simply a collection of reusable Groovy scripts that you can import into your Jenkins Pipeline scripts.

vars/
    myHelper.groovy
src/
    org/
        myorg/
            MyUtilityClass.groovy
resources/
    myresource.file

This structure includes:

  • vars: Directory for global variables or scripts that can be called directly from a Pipeline script.
  • src: Directory for more complex Groovy code.
  • resources: Directory for non-Groovy resources, e.g., property files.

Loading the Shared Library

There are two primary methods for loading a shared library into a Jenkins Pipeline: statically or dynamically.

Static Loading

Static loading involves configuring Jenkins to automatically load the shared library. This is done in Jenkins’ system configuration:

Jenkins -> Manage Jenkins -> Configure System -> Global Pipeline Libraries

Add your library by specifying its name, source control location, and version. Once configured here, you can reference this library in any Pipeline script without further scripted loading steps.

Dynamic Loading

For more flexibility, especially in version handling, you can dynamically load a shared library into a specific Pipeline script:

@Library('[email protected]') _

This script line loads version 1.0 of the shared library named ‘my-shared-library’. The underscore (_) denotes that you’re importing the global variables defined in the vars directory.

Using Library Classes and Resources

Once loaded, you can use the classes and resources from your shared library within your Pipeline:

def utility = new org.myorg.MyUtilityClass()
echo utility.doSomethingUseful()

This example creates an instance of MyUtilityClass defined under the src directory of the shared library and calls one of its methods.

Advanced Usage

As you become more comfortable with using external libraries in Jenkins, you might want to explore advanced features like passing parameters to library methods, or defining global variables that can take parameters themselves.

// Passing parameters to a library method
def utility = new org.myorg.MyUtilityClass()
echo utility.doSomethingUseful('param1', 42)

// Defining a global variable that takes parameters
// vars/myComplexFunction.groovy

def call(String name, int age) {
    echo "Hello, ${name}, age ${age}"
}

In these examples, we demonstrate how to pass parameters to a class method within the library and how to define a global script (under the vars directory) that can accept parameters.

Conclusion

Integrating external libraries into your Jenkins Pipeline empowers you to reuse code, maintain cleaner scripts, and leverage the full potential of Jenkins’ automation capabilities. By following the steps outlined in this tutorial, you will be well-equipped to implement shared libraries in your projects, improving maintainability and code reuse in your continuous delivery processes.

Next Article: How to manage credentials and secrets in Jenkins

Previous Article: Jenkins Pipeline Syntax: The Complete Cheat Sheet

Series: Jenkins Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide