I am considering to use Jenkins pipeline script recently, one question is that I don't figure out a smart to way to create internal reusable utils code, imagine, I have a common function helloworld
which will be used by lots of pipeline jobs, so I hope to create a utils.jar
can injected it into the job classpath.
I notice Jenkins have a similar concept with the global library, but my concern regarding this plugin:
Since it is a plugin, so we need to install/upgrade it through jenkins plugin manager, then it may require reboot to apply the change, this is not what I want to see since utils may change, add always, we hope it could be available immediately.
Secondly, it is official jenkins shared lib, I dont want to (Or they will not apply us) put private code into jenkins repo.
Any good idea?
Here is the solution that we are currently using in order to re-use Jenkinsfile code:
I might be a bit ugly but it works realiably and in addition to that it also allows us to insert some repository specific code before or after the shared code.
The Shared Libraries (docs) allows you to make your code accessible to all your pipeline scripts. You don't have to build a plugin for that and you don't have to restart Jenkins.
E.g. this is my library and this a Jenkinsfile that calls this common function.
EDIT (Feb 2017):
The library can be accessed through Jenkins' internal Git server, or deployed through other means (e.g. via Chef) to the(still possible, but very unhandy).workflow-lib/
directory within the jenkins user's home directory.The global library can be configured through the following means:
@Library('github.com/...')
annotation in theJenkinsfile
pointing to the URL of the shared library repo.A mix of the first and last method would be a not explicitly loaded shared library that is then requested only using its name in the
Jenkinsfile
:@Library('mysharedlib')
.Depending on how often you plan on reusing your code, you could also load a function (or a set of functions) as part of another pipeline.
This solution might seems a little bit cumbersome compared to StephenKing's one but what I like about this solution is that my global functions are all commited to Git and anybody can easily modify them without (almost) any knowledge of Jenkins, just basics of Groovy.
In the Groovy script your are
load
ing, make sure you addreturn this
at the very end. This will allow you to make calls later. Otherwise when you setpipeline = load global-functions.groovy
, the variable will be set tonull
.