to use groovy in dleclarative you have to use the `script` step to warp 
your code
 
pipeline {
  stages {
    stage('Stage1') {
      steps {
        script { // YOU NEED TO USE THE SCRIPT BLOCK
          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
        }
      }
    }
  }
}

or define a function then call it, I personally like it much is cleaner

pipeline {
  stages {
    stage('Stage1') {
      steps {
        parallelTasks()
      }
    }
  }
}

def parallelTasks() {
  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
}

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/c0e081d0-668a-4eb3-b538-be3ada207078%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to