Jenkins Groovy Parallel Variable not working

2019-06-09 23:01发布

I'm running the Jenkins Build Flow Plugin with the following script:

def builds = [:]

[1,2].each { 
  builds[it] = { build("test", parm: ("$it"))  }
}

parallel builds 

However, whilst the hash (builds[it]) gets populated correctly, the parm is always null. I've also tried the following:

builds[it] = { build("test", parm: $it))  }
builds[it] = { build("test", parm: it))  }

But the it is always null.

Can anyone give me any pointers as to how I can use the $it, or any other variable in the build jobs please.

2条回答
狗以群分
2楼-- · 2019-06-09 23:45

Seems like you are running into a bug in Build Flow Plugin (I've seen similar issues with Pipeline DSL). No expert, but it seems to be related to groovy closures and scoping of outer variables that are provided by each or foreach constructs. For example (smilar to your example):

def builds = [:]

[1,2].each { 
  builds[a] = { print "${it}\n"  }
}

parallel builds

prints:

null
null

while:

def builds = [:]

[1,2].each { 
  def a = it;
  builds[a] = { print "${a}\n"  }
}

parallel builds 

will print

1
2

as expected. So, use an local variable to store the iteration value.

查看更多
\"骚年 ilove
3楼-- · 2019-06-09 23:53

As per the build flow documentation I believe the syntax should be:

builds[it] = { build("test", param1: it) }

i.e. the param1 argument name needs to literally read param followed by a sequential integer starting with 1.

查看更多
登录 后发表回答