Pipeline: Keep the main logic outside of Jenkinsfile

20,667 views
Skip to first unread message

Sverre Moe

unread,
May 24, 2016, 10:00:46 AM5/24/16
to Jenkins Users
What is the best design approach to keeping the Jenkinsfile small (little build logic as possible)?

Having close to a hundred projects and several branches on each, then duplicating the pipeline logic within each Jenkinsfile would be a maintenance nightmare.

1)
I could put the Pipeline script within Jenkins Scriptler
def packageInformation = load '../../../scriptler/scripts/package-information.groovy'
packageInformation.init()
However I'm only able to access this on the master node

2)
I could have all the pipeline scripts within a jenkins-ci-scripts project in Git.
stash includes: '**/*.groovy', name: 'scripts'

stage 'Checkout'
checkout scm
unstash scripts
def packageInformation = load 'src/main/groovy/com/company/pipeline/package-information.groovy'
packageInformation.init()
I could then load the pipeline scripts from this stash. However I do not like having to duplicate even this much in all my different Jenkinsfile.

3)
We have a inhouse build tool installed on all build machines. This contains various build scripts, shell, python, ruby.


Example Jenkinsfile with little logic as possible
def branch = env.BUILD_BRANCH
def pipeline = load '/path/to/our/installed/build-pipeline.groovy'
parallel pipeline.nodes(branch)



My current Jenkins instance I comprised of Multi-configuration jobs/projects (a jenkins job/project for each release branch). Having aprox 8 release branches for each of the 60 projects. I am moving over to Pipeline because of a complex workflow and because Multi-configuration does not support a Prebuild-step that I need.

Baptiste Mathus

unread,
May 25, 2016, 4:54:29 PM5/25/16
to jenkins...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/126d204a-f002-4838-a67e-96d02913f7df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sverre Moe

unread,
May 26, 2016, 3:36:58 AM5/26/16
to Jenkins Users, m...@batmat.net
Yes, I came across it, but there is so little information about how to use this and how it works. 
which I did not understand much of.

// The call(body) method in any file in workflowLibs.git/vars is exposed as a method with the same name as the file.
In any file in "workflowLibs.git/vars": Where is this location?

Jenkinsfile
standardBuild {
    environment = 'golang:1.5.0'
    mainScript = '''
        go version
        go build -v hello-world.go
        '''
    postScript = '''
        ls -l
        ./hello-world
        '''
}

standardBuild.groovy
def call(body) {
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()
    stage 'checkout'
    node {
        checkout scm
        stage 'main'
        docker.image(config.environment).inside {
            sh config.mainScript
        }
        stage 'post'
        sh config.postScript
    }
}

What is body?
What does this do?
body.resolveStrategy = Closure.DELEGATE_FIRST

From what I could understand it would seem standardBuild in Jenkinsfile runs the code within call(body) and variables defined in this function in the Jenkinsfile is accessible through body.delegate = config somewhat, but why that way around and not config = body.delegate

Why is there no def in front of variables in standardBuild?
def mainScript

Where do I place the file standardBuild.groovy?

Kohsuke Kawaguchi

unread,
May 27, 2016, 8:11:53 PM5/27/16
to Jenkins Users, m...@batmat.net

Sverre Moe

unread,
May 30, 2016, 2:03:20 AM5/30/16
to Jenkins Users, m...@batmat.net
Yes, that explained a lot. Thanks.

Sverre Moe

unread,
May 30, 2016, 4:39:41 AM5/30/16
to Jenkins Users, m...@batmat.net
I followed the instructions to get the workflowLibs.git repository from Jenkins:

SInce the repository was empty I got nonexistent ref as expected and mentioned in the instructions.
sverre@mintaka:~/workspace> git clone https://build.company.com:8443/workflowLibs.git
Cloning into 'workflowLibs'...
Checking connectivity... done.
warning: remote HEAD refers to nonexistent ref, unable to checkout.

sverre@mintaka:~/workspace3> cd workflowLibs/
sverre@mintaka:~/workspace/workflowLibs> git checkout -b master
Switched to a new branch 'master'

I added one file
src/com/company/Build.groovy

When I tried to push it didn't work
sverre@mintaka:~/workspace/workflowLibs> git push --set-upstream origin master
fatal: unable to access 'https://build.company.com:8443/workflowLibs.git/': The requested URL returned error: 403
Reply all
Reply to author
Forward
0 new messages