Jenkins Library

Sunday, May 14, 2017

Jesus fuck I hope this is the last Jenkins blog I do. As part of our switch to Jenkins I wanted to define a bunch of common function that all of our scripts could use - without copy and pasting them.
To do this I defined a global Jenkins library. Everything you read about this makes it seem a million times harder than it is. So here's the easy way.

Create a Github Repository

You know how to do this right?

Create a Global Library

Go to "Manage Jenkins > Configure System > Global Pipeline Libraries". Add one. Mark it as local implicitly (removes a bunch of manual library loading bullshit), then use the source code management and point it to your repository you just made.

Defining Classes

Create a file in your repo called src/fuckhole/jizz.groovy

package fuckhole;
def PrintHello()
{
echo 'hello'
}

So commit that and in any Jenkins scripts you use you'll be able to do

def j = new fuckhole.jizz()
j.PrintHello()

You probably worked out, the file name determines what the class is named. The folder determines the package name.
Here's a real example, our Unity script, which wraps our UnityBatch tool, and is in

Something to be aware of is that you can run into problems when calling these functions from other similarly created functions. After a bit of a struggle I found that it was better to create classes with "expert level" functionality, then create these "easy mode" functions which just wrapped the classes.