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

2017-09-18 Thread Michael Pailloncy
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


2017-09-15 11:02 GMT+02:00 ishan jain :

> 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 ?
>
> --
> 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+unsubscr...@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.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/CAPO77c3T%3DtouGwy85XJGQJowRKPJPEF2b-UVHtDveiR%3DTbdZkw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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

2017-09-15 Thread ishan jain
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 ?

-- 
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+unsubscr...@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.