Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovy

How to run for loop in batch of 4 for 20 items at once in jenkinsfile


I have to run for loop in groovy for 40 items, but do wish to run it for 4 items in parallel then next batch and so on. I know of parallel deployments in jenkinsfile but it triggers all 40 at once.

          def i = 0
          mslist.collate(4).each{
               build job: 'deploy', parameters: [string(name: 'PROJECT', value: "${it[i]}"), string(name: 'ENVIRONMENT', value: params.ENVIRONMENT)]
               i=i+1
}

My Updated code:

stages {
    stage ('Parallel Deployments') {
      steps {
        script {
          def m = rc.ic()
          m = m.collect { "${it}" }
          println "$m"
          m.collate(4).each{
              def deployments = [:]
              batch.each {
                deployments[it] = {
                   build job: 'jb', parameters: [string(name: 'pt', value: it), string(name: 'pl', value: params.gh), string(name: 'dc', value: params.nb)]
                }   
              
            }  
            parallel deployments
          }
        deployments["failFast"] = false
        }
    }
  } 
 }

Solution

  • It can be done like this :

    node {
        def items = (1..40).collect { "item-${it}" }
        items.collate(4).each { List batch -> 
            def n=[:]
            
            batch.each {
                n[it] = {
                    stage(it) {
                        build job: 'x', parameters: [ string( name: "it", value : it) ]      
                    }    
                }
            }
            
          parallel n
        }
    }
    

    job: x Jenkinsfile content

    node {
        echo "Hello from Pipeline x"
        print params
    }
    

    This will invoke 4 jobs at a time and run parallelly. Make sure you have more than 4 executors configured on Jenkins.

    enter image description here