Recently I came into a situation where I need to have several jobs, say 10, with the exact same settings/implementation – meaning, basically having the same pipeline several times. In a “brute-force” approach, I will simply create one “instance” of this job and then copy it “9 more times” (I need total of 10). Obviously, this will become cumbersome when modifications will have to take place in the “original” instance – which off course means that these changes will need to be applied to all other 9 copies as well. In order to solve this issue with a more elegant and proper way, I will use the Jenkins shared libraries concept.
1.1) My Jenkins server is with version 2.176.1
2.Jenkins shared library repository preparations
As described in the official Jenkins shared libraries tutorial, the “code” of the pipeline/steps that will be “shared” need to be kept in a repository of its own (or at least, it is best to keep it in such).In my case it is hosted on a git repository on a BitBucket server.
The structure of this repository needs to be as follows:
2.1) At the moment the only folder that I will use is the vars folder. Its name MUST be exactly as it is.
2.2) Within this folder the .groovy file with the “shared pipeline” code will reside in. In this example it is called sampleSharedLibrary.groovy
2.3) At the moment I will not use (“add”) files to either one of the other folders.
3. Shared library implementation
In this post I will demonstrate how to share an “entire” pipeline. Note, however, that more “specific entities” of pipelines, such as steps/stages can also be “shared” (implemented within a shared library).
In order to keep this example as simple and minimal as possible, the implementation of the “shared” pipeline will be as follows:
3.1) The entire pipeline is written within a Groovy function with the following signature:
The name MUST be call and the single argument of this function is a Map object (Groovy object) that will contain the arguments that will be sent to this pipeline (will be discussed later on).
3.2) This pipeline also has a “static” parameter of string type called input.
3.3) Other that that, the rest of the implementation of the pipeline is as every “normal” pipeline will be.
4. Configuring the shared library in Jenkins
The next thing that needs to be set is within the “global Jenkins” settings. Here I will indicate Jenkins that it “can” use a shared library. In order to do so, the following needs to be done under Manage Jenkins –> Configure System –> Global Pipeline Libraries:
5.1) In this example the name of the shared library is my-sample-shared-library noted in point 2.
5.2) It is hosted within a git repository that is noted in point 4, with its credentials noted in point 5.
5.3) Also note that in the Default version tab, for now, add master, meaning, the shared library will be fetched from its master branch (it is NOT depicted in the figure).
6. Create a pipeline that will utilize the shared library
All that is left to do, is to create a simple and minimal pipeline that will “utilize” the shared library. Keeping things simple and minimal as before in this post, a very naive implementation of such pipeline within a file called, for example, simplePipelineThatUtilizesSharedLibrary, can be like so:
6.1) The ‘_’ at the end of the first line is NOT a typo and it must be present.
6.2) The value of the SetupNumber argument passed to the shared library from THIS particular pipeline is ‘2’ and MUST be indicated as seen above (i.e. – <ArgsName>: ‘<ArgValue>’). In case more then one argument is required by the shared library, one simply adds it as the second argument, and so on…
6.3) What left to do is to create a pipeline in the Jenkins that will use this above simplePipelineThatUtilizesSharedLibrary Jenkinsfile.
a) official Jenkins shared libraries tutorial
b) Good StackOverflow answer regarding how to create a “template” pipeline using the shared libraries concept
c) Nice tutorial about Jenkins shared libraries
e) shared library within the same repository
The picture: Colca canyon, near the city of Arequipa, Peru.