When writing Jenkins Pipeline scripts, is it safe to access variables from parallel steps? The documentation isn't clear on this.
For example, this pipeline code modifies a common counter and queue from parallel branches:
def donecount = 0;
def work = [6,5,4,3,2,1,0]
def branches = [:]
for (int i = 0; i < 3; i++) {
branches["worker-${i}"] = {
while (true) {
def item = null
try {
item = work.remove(0)
} catch (java.lang.IndexOutOfBoundsException e) {
break
}
echo "Working for ${item} seconds"
sleep time:item
donecount += 1
}
}
}
branches.failFast = true
parallel branches
echo "Completed ${donecount} tasks"
In the current implementation, this is probably safe, in that Pipeline execution uses coöperative multitasking (otherwise known as “green threads”). But I am not sure that, for example, +=
is an atomic operation in Groovy at the granularity that matters here. Better to play it safe and use standard Java concurrency utilities: ConcurrentLinkedQueue
, AtomicInteger
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With