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()

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

package facepunch;

def unityVersion = '5.6.0f3'

def Setup( v )
unityVersion = v

// UnityBatch wraps Unity in a way that outputs the log file to jenkins
def Batch( String projectFolder, String methodName, String otherCommandLine = "" )
bat "Facepunch.UnityBatch.exe ${otherCommandLine} -unityVersion \"${unityVersion}\" -projectPath \"${projectFolder}\" -executeMethod \"${methodName}\""

// UnityBatch with a BuildTo
def BatchTo( String projectFolder, String methodName, String outFolder, String otherCommandLine = "" )
bat "Facepunch.UnityBatch.exe ${otherCommandLine} -unityVersion \"${unityVersion}\" -projectPath \"${projectFolder}\" -executeMethod \"${methodName}\" -buildTo \"${projectFolder}/${outFolder}\""

Global Functions

You probably just want to add a global function and not fuck around with classes and shit, right? So here functions are defined per file. So create

def call( String name ) 
echo "fuck you " + name

Pretty simple eh. The function name needs to always be called "call", but it can take arguments and return shit. Now in your scripts you'll be able to call..

PrintFuckYou( 'Dave' )

And a real world example in

def call( String pattern ) 
def batFile = "";

def files = findFiles( glob: pattern )
def c = 0;

for ( int i =0; i 32 )
batFile += "\n"
c = 0;

bat batFile

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.

More Rubbish

Your Portfolio Gave Me Diarrhea

Friday, October 1, 2021


Friday, September 24, 2021

We got married

Thursday, September 16, 2021

Unity Quiz

Sunday, September 6, 2020

Everything We Watched In Lockdown

Thursday, September 3, 2020


Saturday, May 6, 2017


Saturday, April 29, 2017

Unity 2017 Changelist

Tuesday, January 3, 2017

Binding Steamworks SDK

Tuesday, November 1, 2016


Monday, October 3, 2016