bazza2000
bazza2000

Reputation: 141

Jenkins Groovy Parallel Variable not working

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.

Upvotes: 2

Views: 2983

Answers (2)

Jon S
Jon S

Reputation: 16346

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.

Upvotes: 6

Matias Bjarland
Matias Bjarland

Reputation: 4482

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.

Upvotes: 0

Related Questions