8

I am using Jenkins 2.6 on Red Hat Linux. I want to use the following in my Jenkinsfile, but when I try, Jenkins complains bitterly. (It seems only to dislike the syntax on left-hand side of the = operator.):

def (a, b) = [6, 7]

It doesn't like Multiple Assignments it seems, yet Groovy 1.6 and later apparently support them, as per this post:

http://mrhaki.blogspot.co.uk/2009/09/groovy-goodness-multiple-assignments.html

I want to do this so that when I call a method that returns [6, 7] I can call it like this:

def (a, b) = mymethod()

def mymethod()
{
    return [6, 7]
}

Can anyone tell me whether this should work in Jenkins and if so in which version of Jenkins? Or is it an unsupported feature? Or a bug?

Thanks

Will
  • 1,509
  • 1
  • 13
  • 16

2 Answers2

4

As explained here, Pipeline "scripts" are not simple Groovy scripts, they are heavily transformed before running, some parts on master, some parts on slaves, with their state (variable values) serialized and passed to the next step. As such, every Groovy feature is not supported.

I wish it was made more clear in Jenkins docs & presentations (that's why I find myself repeating the paragraph above... there is much confusion about this). Pipeline is so simple it seems magic... well, it kinda is ;)

It seems multiple assignments are not supported indeed. I only found this reference to confirm it: this example of a commit where the author changes his code from multiple assignments to simple ones because of that limitation.

Probably a good idea to ask for it on the mailing list as suggested by @rjohnston.

Community
  • 1
  • 1
Hugues M.
  • 19,846
  • 6
  • 37
  • 65
  • Hi think this restriction still a little bit confusing in the [Scripted Pipeline](https://jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline) docs because it says that "Most functionality provided by the Groovy language is made available to users ...". Would be more helpful to define somewhere which is the real scope of Groovy in it. – snaphuman Aug 16 '17 at 15:46
0

It should be available (and indeed it works from the Script Console) however it looks like whatever is necessary to make it work from a pipeline script hasn't been done.

The dev mailing list (https://groups.google.com/forum/#!forum/jenkinsci-dev) may be able to tell you if this is a bug or just an unsupported feature!

rjohnston
  • 7,153
  • 8
  • 30
  • 37