Pipeline groovy.. very confused by the way the same code works differently in libraries

18 views
Skip to first unread message

Jonathan Hodgson

unread,
Nov 10, 2016, 1:00:11 PM11/10/16
to Jenkins Users
Hi,

I have a global library class, HelperFunction, which includes a getKeySet function.

package mylib

@NonCPS def getKeySet(m)
{
    def key_set = m.keySet()
    key_set.toArray(new String[key_set.size()])
}

return this



Now I use this function in two places.

One is in a script which is loaded by my JenkinsFile, using a load step

It (in cut down form) looks like this

#!groovy

HelperFunction = new mylib.HelperFunction()

@NonCPS def includeTable(build_types, include_string, exclude_string)
{
    def platforms = HelperFunction.getKeySet(build_types.generators)
    // Rest of function
}

return this

this works, as does this alternative

#!groovy

@NonCPS def includeTable(build_types, include_string, exclude_string)
{
    def HelperFunction = new mylib.HelperFunction()
    def platforms = HelperFunction.getKeySet(build_types.generators)
    // Rest of function
}

return this

It is also used in a Folder library  file which contains a class instantiated as

myBuildClasses = new mylib.MyBuildClasses()

The following works fine, much as in the above example

package mylib

@NonCPS def buildTableHtml(caption, build_types, include_table)
{
    def HelperFunction = new mylib.HelperFunction()
    def platforms = HelperFunction.getKeySet(build_types.generators)
    // Rest of function
}

return this


but this
package mylib

HelperFunction = new mylib.HelperFunction()

@NonCPS def buildTableHtml(caption, build_types, include_table)
{
    def platforms = HelperFunction.getKeySet(build_types.generators)
    // Rest of function
}

return this


results in

you tried to assign a value to the class 'myLib.HelperFunction'. Do you have a script with this name?

The good news is I can work around this issue to get what I need to work working, the bad news is, I don't understand what is going on. Pretty much the same code is acting very differently.

Could somebody please clarify it to me?

Reply all
Reply to author
Forward
0 new messages