14 December 2016

Jenkins Pipeline Shared Groovy Script by Hand

There is currently a new version of the Pipeline Shared Groovy Libraries Plugin (2.5) that adds capabilities to put some Groovy scripts in Git, at least, and to make them available easily to call from Jenkinsfile (the Jenkins Pipeline script, also stored with the application code in Git)

I can't move to that version yet and so I set it up to load the script by hand:

def notify

stage "Load-Scripts"
node() {
    git branch: 'master', changelog: false, credentialsId: 'ID', poll: false, url: 'https://github.company.com/my-organization/my-repository.git'
    notify = load 'src/com/aa/jenkins/pipeline/notify.groovy'

    notify.notifySlack("START")
}
The above code goes in the Jenkinsfile at the top, before any other  stages. You will, of course have to put in your own  credentials and branch and so forth, the Pipeline Snippets tool might be handy in doing that line.

Some things ...

The variable into which we store the class created by loading the one groovy file has to be defined with the 'def' and without a type. This makes it into a global variable that can be used outside the scope in which we do the load.

The path to the groovy file is in the context of the root of the git workspace.

You may need a node name inside the parentheses.