Skip to content

Instantly share code, notes, and snippets.

@hartsock
Created May 15, 2019 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartsock/f76f2bf150644c16d2a7831b0688933a to your computer and use it in GitHub Desktop.
Save hartsock/f76f2bf150644c16d2a7831b0688933a to your computer and use it in GitHub Desktop.
A Jenkins Declarative Pipeline with parallel execution and coordination based on actions in each parallel node.
/**
* Problem:
* I have some action occurring on several parallel nodes. The action may have a diffrent result on each node non-deterministically.
* I need to act from the master depending on the result at each node.
*
* For a contrived example, we record the name of each node used to act on the parallel actions specified in this pipeline. The
* names are collected in a global string and a global `Map<String,String>` object for use by the master.
*
*/
value = ':'
record = [:]
def parallelNodes() {
List<String> nodes = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Map<String, Object> pipelines = [:]
nodes.each { String name ->
pipelines[name] = {
node('my-label') {
stage(name) {
script {
echo "NODE_NAME = ${env.NODE_NAME}"
echo value
echo "from " + name
value += ' [' + env.NODE_NAME + '] '
record[name] = env.NODE_NAME
echo value
}
}
}
}
}
return pipelines
}
pipeline {
agent { label 'my-label' }
stages {
stage('Init') {
steps {
script {
value += " init "
echo value
}
}
}
stage('Parallel') {
steps {
script {
parallel(parallelNodes())
}
}
}
stage('Done') {
steps {
script {
echo value
value += " Done "
echo value
echo record.toString()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment