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.
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.
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.