How to call a groovy function from a Jenkinsfile?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to call a groovy function from a Jenkinsfile?

Idan Adar
(Cross-posting my question from Stack Overflow: http://stackoverflow.com/questions/42790966/how-to-call-a-groovy-function-from-a-jenkinsfile)

I am unable to successfully use a local groovy file in my Jenkinsfile (both are in the same repository).

def deployer = null
   
...
   
...
   
...
    pipeline
{
       agent
{
          label
'cf_slave'
       
}
   
       options
{
          skipDefaultCheckout
()
          disableConcurrentBuilds
()
       
}
   
       stages
{
          stage
("Checkout SCM") {
             steps
{
                checkout scm
             
}
         
}
         
...
         
...
         
...
          stage
("Publish CF app") {
              steps
{
                  script
{
                      STAGE_NAME
= "Publish CF app"
   
                      echo
"before loading groovy file"
                      deployer
= fileLoader.load ('deployer')
                      echo
"after loading groovy file"
                     
                      withCredentials
(...) {  
                         
if (BRANCH_NAME == "develop") {
                             
...
                             
...
                             
...
                         
} else {
                              deployer
.generateManifest()
                         
}
                     
}
                 
}
             
}
         
}
         
...
         
...
     
}


**deployer.groovy**:

   
#!/usr/bin/env groovy

def generateManifest() {
    sh
"..."
    echo
"..."
}


In the console log ([stack](http://pastebin.com/j7dWrFQq)): 

    [Pipeline] stage
    [Pipeline] { (Publish CF app)
    [Pipeline] script
    [Pipeline] {
    [Pipeline] echo
    before loading groovy file
    [Pipeline] echo
    Loading from deployer.groovy
    [Pipeline] load
    [Pipeline] // load
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage

It seems the problem was not with loading the file but rather with the contents of the file, where I execute the following which apparently does not play well:

sh "node $(pwd)/config/mustacher manifest.template.yml config/environments/common.json config/environments/someFile.json"
echo
"..."

Not the sh nor the echo work.  
When only the echo is there, [this is the stack].(http://pastebin.com/G5MXc7YF).

If I will make the same node call in the pipeline (for example in the `withCredentials` `if` statement, it works.


--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/f5ef03b8-424e-4d6f-ae0d-76efda52d1c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to call a groovy function from a Jenkinsfile?

Christopher Orr
On Tue, 14 Mar 2017, at 20:17, Idan Adar wrote:
> (Cross-posting my question from Stack Overflow:
> http://stackoverflow.com/questions/42790966/how-to-call-a-groovy-function-from-a-jenkinsfile)

You *really* need to pare down your question to the minimum required to
reproduce the problem.

Between here and Stack Overflow, you have conflicting information about
what's in deployer.groovy.  On SO, the file apparently continues, yet
here it just ends after the method definition — but neither end with
`return this` as required:
https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#triggering-manual-loading
(linked from the docs of the Pipeline Remote Loader plugin you're using)

You say that the problem was *not* with loading the file (so presumably
most of the Jenkinsfile could have been omitted) — yet your stacktrace
suggests that the file failed to load, supposedly when you commented out
a line … in the file that fails to load?

Such a question is very unwieldy, and makes it harder for people to
answer.  Please reproduce the problem in a bare-minimum Pipeline and
update your question on SO.

Regards,
Chris

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/1489530449.4025867.911445776.2515567E%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to call a groovy function from a Jenkinsfile?

Daniel Beck

> On 14.03.2017, at 23:27, Christopher Orr <[hidden email]> wrote:
>
> Such a question is very unwieldy, and makes it harder for people to
> answer.  Please reproduce the problem in a bare-minimum Pipeline and
> update your question on SO.

On top of all that, it's been posted to the wrong mailing list.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/3CC1D819-CA23-4EA1-B9C6-0C6DAE5A0BE9%40beckweb.net.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to call a groovy function from a Jenkinsfile?

Christopher Orr
On Tue, 14 Mar 2017, at 23:38, Daniel Beck wrote:
>
> > On 14.03.2017, at 23:27, Christopher Orr <[hidden email]> wrote:
> >
> > Such a question is very unwieldy, and makes it harder for people to
> > answer.  Please reproduce the problem in a bare-minimum Pipeline and
> > update your question on SO.
>
> On top of all that, it's been posted to the wrong mailing list.

Gaaaaahh… (╯°□°)╯︵ ┻━┻

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/1489531252.4028576.911465512.725847B1%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.