João Amaro
João Amaro

Reputation: 496

How to run parallel jobs from map inside groovy function?

I have a jenkinsfile that calls a function from groovy:

jenkinsfile:

pipeline {
  agent none
  environment {
  HOME = '.'
  }
  stages {
    stage("initiating"){
      agent {        
        docker {
          image 'docker-image'
        }
      }
      stages {
        stage('scanning') {
          steps {
            script {
              workloadPipeline = load("Pipeline.groovy")
              workloadPipeline.loopImages1(Images)
            }
          }     
        }
      }
    }
  }
}

groovy functions:

def loopImages1(Images){
  Images.each { entry ->
    parallel {
      stage('test-' + entry.key) {
        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {

          script {
            sh """
              docker run -d $entry.value
            """
          }
        }
      }
    }
  }
}

Images returns a map, something like this:

image-1 : 123.dkr.ecr.eu-west-1.amazonaws.com....
image-2 : 123.dkr.ecr.eu-west-1.amazonaws.com....
image-3 : 123.dkr.ecr.eu-west-1.amazonaws.com....

And I was trying to run it with parallel, which in this case should run 3 jobs in parallel, but it gives me the following error message:

java.lang.IllegalArgumentException: Expected named arguments but got org.jenkinsci.plugins.workflow.cps.CpsClosure2@19027e83

What do I need to change in order to get this to work? From what I read it needs a map as input, which I'm already giving.

Upvotes: 1

Views: 1233

Answers (1)

João Amaro
João Amaro

Reputation: 496

In case anyone has a similar question, here is the answer that solved my problem:

groovy function:

def loopImages1(Images){
  **def parallelStage = [:]**
    Images.each { entry ->
      **parallelStage[entry] = {**
        stage('test-' + entry.key) {
          catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {

            script {
              sh """
                docker run -d $entry.value
              """
            }
          }
        }
      }
    }
  **parallel parallelStage**
}

Upvotes: 1

Related Questions