Jenkins Library

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 /src/facepunch/Unity.groovy.

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 vars/PrintFuckYou.groovy.

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 /vars/SignTool.groovy

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.

2 thoughts on “Jenkins Library

  1. Oh, fuck my brains out; I’ll settle with my manual one – which isn’t even accessed on the repository.
    Defining paths to unity batch, defining paths of checked out repositories, defining paths of builds + temporary folder where the bundles are, at the end it sends the builds to a official folder and cleans up the temp folder (bat ‘rd /s /q ‘ + tempFolder).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s