Global Pipeline library guide
Shared libraries in Jenkins allow you to centralize and reuse pipeline code across multiple projects. By storing these libraries in Perforce and configuring them in Jenkins, teams can standardize build logic, simplify Jenkinsfiles, and reduce duplication.
workflow-cps-global-lib plugin installed on Jenkins. Follow the steps below in Prerequisites if you need to install this plugin. Prerequisites
You must install the plugin Pipeline: Shared Groovy Libraries plugin in Jenkins. This plugin is also known as workflow-cps-global-lib. This plugin enables the Global Trusted Pipeline Libraries section in Jenkins’ Configure System page.
Global Pipeline Libraries (for older versions of Jenkins)
Global Trusted Pipeline Libraries (for newer versions of Jenkins).
Set up Global Pipeline Library
To use your Perforce libraries in Jenkins pipelines, first set them up as a Global Trusted Pipeline Library in the Jenkins system configuration.
-
Go to Manage Jenkins.
-
In the System Configuration section, click on System.
-
Scroll down to Global Trusted Pipeline Libraries.
This section appears only after installing the Shared Groovy Libraries plugin and is quite far down the page, usually after the Default notification URL section). -
To set up your Global Pipeline library, click Add.
This enables fields for configuring your Global Pipeline Library. For detailed instructions, see Configure Global Pipeline Library.
Configure Global Pipeline Library
Configure the following fields to set up a Global Pipeline library.
-
Enter a Name, for example
my-p4-library. This is how you reference the library in a Jenkinsfile. -
(Optional) Select Load implicitly to make the library available to all pipelines without explicitly importing it.
-
In Retrieval method, select Legacy SCM.
-
In Source Code Management, select Perforce Software. This will enable several more fields such as Perforce Credentials, Workspace behaviour and Populate options. For information on how to populate these fields, see Pipeline script from SCM setup.
-
In Library Path, enter a path to the library in the Perforce depot. This is covered in more detail in the following section, Library Path
Library Path
The Library Path defines the location of your library inside the Perforce depot. Jenkins uses this path to check out the library code into the pipeline workspace.
Consider using a create a src folder in your depot
For example, the following Utils.groovy file has this depot path: //depot/myLibrary/src/org/example/Utils.groovy
When you have added in the library path, click Save to apply the configuration.
Library Path Tips and Troubleshooting
-
You may need to include
${library.<libName>.version}in depot paths to resolve versions correctly. -
Verify that your Perforce workspace view maps only the library depot path. Mismatched views are a common source of errors.
-
Keep libraries small and modular, for example, use the
vars/directory for global functions, andsrc/for packaged classes.
Use the Library in a Pipeline
Once you have a Global Trusted Pipeline Library configured, here’s how you use it in a pipeline.
-
Open or create a Jenkins Pipeline job
-
Go to your Jenkins dashboard.
-
Click New Item, give your job a name, select Pipeline and click OK.
-
Scroll to the Pipeline section where you can enter your script.
-
-
Import the library. This is done at the very top of your pipeline script, where you to tell Jenkins which library to use.
This case, it is
@Library('my-p4-library') _If you checked Load implicitly when configuring the library, you can skip this step as the library is automatically available.
-
Import a specific class or function from the library. For example, if your library has a Groovy class called
Utilsin the packageorg.exampleimport org.example.UtilsNow you can use it in your pipeline.
-
Call a function from the library. This is done inside your pipeline’s script block and you can create an instance of the class and call its functions. For example:
pipeline { agent any stages { stage('Demo') { steps { script { def u = new Utils() // create the class u.sayHello("Jenkins") // call a function } } } } } -
To run the pipeline, click Save and then Build Now. Jenkins will check out the library from Perforce and run your pipeline using the shared code.