I'm new to using pipelines.
I'm trying to execute the same commands against a number of nodes in parallel
Ideally I will pass a list of machines to run the same code against. Below I am
hardcoding the machine names (machine1 and machine2) for the purposes of this example.
I found the following in Jenkins Pipeline examples:
----------------------------
def labels = ['precise', 'trusty'] // labels for Jenkins node types we will build on
def builders = [:]
for (x in labels) {
def label = x // Need to bind the label variable before the closure - can't do 'for (label in labels)'
// Create a map to pass in to the 'parallel' step so we can fire all the builds at once
builders[label] = {
node(label) {
// build steps that should happen on all nodes go here
}
}
}
parallel builders
----------------------------
So I adapted it to be the following:
----------------------------
pipeline {
stages {
stage('Stage1') {
steps {
def labels = ['machine1','machine2'] // labels for Jenkins node types we will build on
def builders = [:]
for (x in labels) {
def label = x // Need to bind the label variable before the closure - can't do 'for (label in labels)'
// Create a map to pass in to the 'parallel' step so we can fire all the builds at once
builders[label] = {
node(label) {
sh '''
echo "hostname:`hostname`"
echo "whoami:`whoami`"
'''
}
}
}
parallel builders
}
}
}
}
----------------------------
But when I use it I get the following error:
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Expected a step @ line 5, column 17.
def labels = ['machine1','machine2'] // labels for Jenkins node types we will build on
^
WorkflowScript: 6: Expected a step @ line 6, column 17.
def builders = [:]
^
WorkflowScript: 7: Expected a step @ line 7, column 17.
for (x in labels) {
^
General error during semantic analysis: There's no @DataBoundConstructor on any constructor of class org.jenkinsci.plugins.workflow.cps.steps.ParallelStep
org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class org.jenkinsci.plugins.workflow.cps.steps.ParallelStep
----------------------------
I can't figure a way around this.
So I was googling around and found this code that does work
pipeline {
agent none
stages {
stage('stage1') {
failFast true
parallel {
stage('S1') {
agent {
label "machine1"
}
steps {
sh '''
echo "hostname: `hostname`"
echo "whoami:`whoami`"
'''
}
}
stage('S2') {
agent {
label "machine2"
}
steps {
sh '''
echo "hostname: `hostname`"
echo "whoami:`whoami`"
'''
}
}
}
}
}
}
But it's not dynamic, I need to have a stage per machine. Does anyone have a suggestion
about how I can achieve what I have in my original code?
I want to dynamically decide what nodes to run the code on.