How can i call global function defined in Jenkinsfile from inside the external groovy script loaded with 'load' step ?

2,912 views
Skip to first unread message

ishan jain

unread,
Sep 15, 2017, 5:02:41 AM9/15/17
to Jenkins Users
Hi,

My Jenkinsfile is getting rather large and i want to break it down into different groovy scripts for easy maintenance. Here is a sample code structure.


externalScripts = ''

pipeline {
    agent master
stages {
        
stage('first') {
            
agent { label 'master' }
steps {
script {
loadScripts()
externalScripts.functionA()
}
}
}
}
stage('second') {
            
agent { label 'some_slave_node' }
steps {
script {
externalScripts.functionB()
}
}
}
}
}

def someGlobalFunction() {
// some common code
}

def loadScripts() {

externalScripts = load 'script_next_to_jenkinsfile.groovy'
}


This works fine if i have some independent code defined in external groovy file. But this breaks if i call a function defined in main Jenkinsfile. 

// contents of script_next_to_jenkinsfile.groovy

def 
functionA() {
// Some regular code will work just fine
}

def 
functionB() {

   // this throws an error - 
java.lang.NoSuchMethodError: No such DSL method 'someGlobalFunction' found among steps....

    someGlobalFunction()
}
return this



I thought the groovy files are loaded in the whole code. How can i make it work like this setup ?

Michael Pailloncy

unread,
Sep 18, 2017, 6:25:11 PM9/18/17
to jenkins...@googlegroups.com

IIC, this is a scope problem.

To strictly answer to your question, you could convert the global function into a Closure and then pass it as parameter of functionB

I think something like that should work :

externalScripts = ''

pipeline {
    agent master

    stages {

        stage('first') {

            agent { label 'master'  }

            steps {
                script {
                    loadScripts()

                    externalScripts.functionA()
                }
            }
        }

        stage('second') {

            agent { label 'some_slave_node' }

            steps {
                script {
                    externalScripts.functionB(this.&someGlobalFunction)
                }
            }
        }
    }

}

def someGlobalFunction() {
    // some common code
}

def loadScripts() {

    externalScripts = load 'script_next_to_jenkinsfile.groovy'
}

// contents of script_next_to_jenkinsfile.groovy

def functionA() {
// Some regular code will work just fine
}

def functionB(customFunction) {
    customFunction()
}

return this

However, I'm personally not very comfortable to use this kind of syntax inside Pipeline scripts and IMHO, you should try to find another way to structure it (to avoid complexity).

BTW, I'm wondering if others have already used this kind of syntax/tricks inside their pipeline and if it's something that it's not recommended (if so, why ?).

Hopefully it helps.

Cheers
Michaël


--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f959f630-2892-452b-a6da-2f56e3b8d329%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages