When writing scripts for Jenkins pipelines for our different development teams using our OpenShift platform we found that maintaining individual Jenkinsfiles for each project quickly gets messy. Surprise!

#Simple lib file for jenkins functions - and how to load it

Our current solutions is to create a general purpose Groovy file with the functions and let individual pipelines put together their stages by piecing functions together. This can be done with something like this:

The jenkins functions file:

// pipeline.groovy 
// contains all the generic, reusable functions used in the pipelines. 
def preparation() { 
def checkout(gitUrl, credentialsId){ 
	checkout([$class: 'GitSCM', branches: [[name: '*/master']], 
				doGenerateSubmoduleConfigurations: false, 
				extensions: [[$class: 'CloneOption', shallow: true]], 
				submoduleCfg: [], 
				userRemoteConfigs: [[credentialsId: "${credentialsId}", 
				url: "${gitUrl}"]]])
return this; 

…and the pipeline file:

// myPipeline.groovy 
// Pipeline script for the individual build / deploy pipeline 
// structured as stages for Openshift pipelines gui 

node {
	def pipefuncs 
	def rootDir = pwd()
	def gitUrl = "ssh://git@..."
	def gitCredentialsId = '...'


	pipefuncs = load "${rootDir}/../workspace@script/scm/functions/pipeline.groovy"

	stage('Prepare and checkout') {
		pipefuncs.checkout(gitUrl, gitCredentialsId)
	stage('Build') {

The benefits of this is that we can make the scripts for the individual pipelines clean and easy to overview. Also, we can let a certain group of developers (DevOps) have git commit / pull request review for the main function file and let the individual teams manage their individual jenkins pipeline file. We provide each new pipeline with a starting template that should work for general purpose but they can extend/modify to their needs.

Further down the line we pick the best modifications and incorporate them into the general purpose lib.

None of this should come as a surprise to an experienced devops person, but I wanted to put the return this / load(…) commands out there to inspire.

I will get back to you when I figure out how to force both developers to maintain good hygiene in their scripts. Probably need to find out if there is any good static code analysis tools out there for build and deploy scripts?