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'}// 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 thisIIC, 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
--
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.