๐Ÿ“ฆ about ๐Ÿงป posts

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.

question_answer

Add a Comment

An error has occurred. This application may no longer respond until reloaded. Reload ๐Ÿ—™