variables in parallel tasks in pipeline plugin.

32 views
Skip to first unread message

John Thompson

unread,
Mar 17, 2016, 10:57:01 AM3/17/16
to Jenkins Users

I have code like the following: (obviously more complicated, but this shows the problem)

for (int i = 0; i < list.size(); i++) {
    thing = list.get(i)
    print thing
    branches[thing] = {
        node {
            print thing
    } 
}
parallel branches

As it's building the branches, it prints out each thing in the list in order as I would expect.  When it actually executes the branches, it prints the last thing in the list for as many items there are in the list.  From what I understand, that's because it's executing the value of the external variable at the time the job is run, rather than what it was at the time the branch was built.  Which I believe is the correct behavior, so I'm probably approaching this the wrong way.

What would be the correct way to run several parallel tasks on different items in a list?  Is there a way to built the code block for each branch so it has the *value* of the variable when I'm building it rather than the *variable itself*?

John Thompson

unread,
Mar 17, 2016, 11:26:07 AM3/17/16
to Jenkins Users
So, just in case anyone else has this problem.  Variable scope is a thing.  Adding a 'def' fixes everything.

for (int i = 0; i < list.size(); i++) {
    def thing = list.get(i)
    print thing
    branches[thing] = {
        node {
            print thing
    } 
}
parallel branches
Reply all
Reply to author
Forward
0 new messages