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 commo
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.
{
// ...your pipeline code...
git 'http://urlToYourGit/projectContainingYourScript'
pipeline = load 'global-functions.groovy'
pipeline.helloworld() // Call one of your defined function
// ...some other pipeline code...
}
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 add return this
at the very end. This will allow you to make calls later. Otherwise when you set pipeline = load global-functions.groovy
, the variable will be set to null
.