2

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 Answers 2

6

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.

2
  • awesome, didn't realise there was a bug, but your solution works great, thanks.
    – bazza2000
    Feb 4, 2017 at 23:25
  • 1
    Maybe not a bug and it's seems to be general to groovy, found this blog post about the issue blog.freeside.co/2013/03/29/…
    – Jon S
    Feb 5, 2017 at 9:29
0

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.

1
  • I still get no parameter value passed to the job
    – bazza2000
    Feb 4, 2017 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.