I work with Jenkins Pipeline for three years now and one pain point is proper isolation of shared functionality between pipelines but even steps. In our repository we defined multiple pipelines and some are that large that we share functionality within it. Jenkins offers the possibility to create shared libraries for that purpose. But unfortunately it’s not possible to load it from the same repository. Since many of the changes in the pipeline are related to a change of the shared library it was tedious to match the branches and versions to be backward compatible. What we actually wanted is having the shared library in our main repository so the states of the pipeline is pinned to the state of the main repository. And we finally figured out how to do (hack) that.
Currently we have the limitation that we can’t define a subfolder from the main repository to use as a shared library (There’s a PR open for 2.5 years). So after searching the internet for a while I stumbled upon this creative idea (hack). The idea is simple. You initialize a new git repository in the subfolder where your shared library lies and give the path to the library step that loads it.
After setting up the repository we can use the
library step to load it from the repository we just created.
And this is essentially it. In the last step we combine both and execute it on the
You can only load a shared library on the master node of your Jenkins instance.
And this code - that loads the local library - can be a shared library itself so we don’t need to repeat ourselves for different projects or pipelines.
I implemented a simple example to demonstrate how to use the loader. The project just has a Jenkinsfile which loads the shared library in the
And this is the Jenkinsfile that loads and uses the shared library:
To make this work you need to configure the
subfolder-library shared library under
Manage Jenkins > Configure System > Global Pipeline Libraries
Depending on your job type you can also configure the
jenkins-library-subfolder-loader directly in the job configuration. This has the neat effect that it runs in the sandbox and can be modified when replaying a run.