Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile: Define and assign values to several variables at once seems not to be implemented

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

like image 355
Will Avatar asked Jul 26 '16 15:07

Will


People also ask

How do you declare a variable in Jenkinsfile?

Environment variables can be defined using NAME = VALUE syntax. To access the variable value you can use these three methods $env.NAME , $NAME or ${NAME} There are no differences between these methods.

How does Jenkinsfile define environment variables?

Setting Stage Level Environment Variable It is by using the env variable directly in the script block. We can define, let us say, USER_GROUP and display it. You will see that the underlying shell also has access to this environment variable. You can also set an environment variable using withEnv block.

How does Jenkins Pipeline define global variable?

At the address bar of chrome, type ${YOUR_JENKINS_HOST}/env-vars. html . The ${YOUR_JENKINS_HOST} itself is an environment variable defining the Jenkins host address/Jenkins URL(that would be http://localhost:8080/ ). And env-vars.

What is the difference between declarative and scripted pipeline?

Basically, declarative and scripted pipelines differ in terms of the programmatic approach. One uses a declarative programming model and the second uses an imperative programming mode. Declarative pipelines break down stages into multiple steps, while in scripted pipelines there is no need for this.


1 Answers

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.

like image 51
Hugues M. Avatar answered Oct 04 '22 21:10

Hugues M.