How to call a function defined within my Jenkinsfile

7,992 views
Skip to first unread message

ishan jain

unread,
Mar 21, 2017, 1:02:34 PM3/21/17
to Jenkins Users
Probably i am being silly, but seriously i am not able to call a function which i have defined in my Jenkinsfile from within a step. Here is a sample:

def meta = "something"

stage (build) {

      buildProject(meta)
}

def buildProject(meta) {
return {
      node {
          //whatever i need to do
      }
  }
}

This simply does not do anything. I tried various combos to try n call it, but nothing. How exactly should i call a function ?

def meta = "something"

stage (build) {
      node {
      buildProject(meta)
  }
}

def buildProject(meta) {
return {
      
          //whatever i need to do
      
  }
}



def meta = "something"

stage (build) {
      step {
      buildProject(meta)
     }
}

def buildProject(meta) {
return {
      node {
          //whatever i need to do
      }
  }
}



def meta = "something"

stage (build) {

      def output =  buildProject(meta)
}

def buildProject(meta) {
return {
      node {
          //whatever i need to do
      }
  }
}

David Karr

unread,
Mar 21, 2017, 3:34:12 PM3/21/17
to Jenkins Users
On Tue, Mar 21, 2017 at 10:02 AM, ishan jain <ishan...@gmail.com> wrote:
> Probably i am being silly, but seriously i am not able to call a function
> which i have defined in my Jenkinsfile from within a step. Here is a sample:
>
> def meta = "something"
>
> stage (build) {
>
> buildProject(meta)
> }
>
> def buildProject(meta) {
> return {
> node {
> //whatever i need to do
> }
> }
> }

I might be wrong, but I don't believe you can use the "node" or
"stage" DSL anywhere but in the main body of the script.

> This simply does not do anything. I tried various combos to try n call it,
> but nothing. How exactly should i call a function ?

Calling a function is simple. Did you try having the function just do
a "println" or "echo" to verify that it got there? If you see that,
then move on to more complex functionality.

> def meta = "something"
>
> stage (build) {
> node {
> buildProject(meta)
> }
> }
>
> def buildProject(meta) {
> return {
>
> //whatever i need to do
>
> }
> }
>
>
>
> def meta = "something"
>
> stage (build) {
> step {
> buildProject(meta)
> }
> }
>
> def buildProject(meta) {
> return {
> node {
> //whatever i need to do
> }
> }
> }
>
>
>
> def meta = "something"
>
> stage (build) {
>
> def output = buildProject(meta)
> }
>
> def buildProject(meta) {
> return {
> node {
> //whatever i need to do
> }
> }
> }
>
> --
> 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/1cf8438c-7bc4-44f6-9e1d-3f42ee863de2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Cuong Tran

unread,
Mar 21, 2017, 10:44:22 PM3/21/17
to Jenkins Users
You can call any function the normal way:

stage (build) {
      buildProject(meta)
}

def buildProject(meta) {

ishan jain

unread,
Mar 22, 2017, 5:18:58 AM3/22/17
to Jenkins Users
@David can you please elaborate what do you mean by main body of the script ?

I did try adding print statements to see if it is getting there. All the code samples i have pasted in my post would result in successful execution but no output, even when i have some in the function. Not sure what is wrong.

David Karr

unread,
Mar 22, 2017, 12:18:04 PM3/22/17
to Jenkins Users
On Wed, Mar 22, 2017 at 2:18 AM, ishan jain <ishan...@gmail.com> wrote:
> @David can you please elaborate what do you mean by main body of the script
> ?

I guess you can ignore that point, as Cuong Tran indicates that is not
the case. I did say that I wasn't sure about that.

> I did try adding print statements to see if it is getting there. All the
> code samples i have pasted in my post would result in successful execution
> but no output, even when i have some in the function. Not sure what is
> wrong.

No idea. You haven't really shown us anything we can diagnose.
> https://groups.google.com/d/msgid/jenkinsci-users/a11f106d-2db1-4d57-94ba-6213e9430342%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages