If you’re a Jenkins user adopting Jenkins pipelines, it is likely that you have run in to the scenario where you want to break out common functionality from your pipeline scripts, instead of having to duplicate it in multiple pipeline definitions. To cater for this scenario, Jenkins provides the Pipeline Shared Groovy Libraries plugin.
You can configure your shared global libraries through the Jenkins UI (Manage Jenkins > Configure System), which is what most resource online seems to suggest. But if you are like me and looking to automate things, you do not want configure your shared global libraries through the UI but rather doing it programmatically. This post aims to describe how to automate the configuration of your shared global libraries.
Jenkins stores plugin configuration in its home directory as XML files. The expected configuration file name for the Shared Groovy Libraries plugin (version 2.8) is:
Here’s an example configuration to add an implicitly loaded global library, checked out from a particular Git repository:
<?xml version=’1.0′ encoding=’UTF-8’?>
<scm class=”jenkins.plugins.git.GitSCMSource” plugin=”firstname.lastname@example.org″>
You can also find a copy/paste friendly version of the above configuration on GitHub: https://gist.github.com/tommysdk/e752486bf7e0eeec0ed3dd32e56f61a4
If your version control system requires authentication, e.g. ssh credentials, create those credentials and use the associated id’s in your
Store the XML configuration file in your Jenkins home directory (echo “$JENKINS_HOME”) and start your Jenkins master for it to load the configuration.
If you are running Jenkins with Docker, adding the configuration is just a copy directive in your Dockerfile:
COPY org.jenkinsci.plugins.workflow.libs.GlobalLibraries.xml $JENKINS_HOME/org.jenkinsci.plugins.workflow.libs.GlobalLibraries.xml
Jenkins environment used: 2.60.1, Pipeline Shared Groovy Libraries plugin 2.8