Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Jenkins Pipeline scripts, is it safe to access a global variable from a parallel step?

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"
like image 822
Nick Sonneveld Avatar asked Apr 29 '16 02:04

Nick Sonneveld


1 Answers

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.

like image 135
Jesse Glick Avatar answered Oct 02 '22 20:10

Jesse Glick