¯\_(ツ)_/¯

thunder@home:~$

This is my home blog, mostly to share some useful info or code snippets
< 1 min

Hello, Jenkins geeks.

Once upon a time, I had a bunch of the downstream jobs to run in parallel. But, the number was quite big… Let’s limit parallel builds!

This is easy!

import java.util.stream.Collectors

...
Map parallelResults = [:]
List parallelData = [
  "service1", "service2", "service3", "service4", "service5", "service6", "service7", "service8", "service9", "service10", "service11", "service12", "service13", "service14", "service15", "service16",
]
if (parallelData.size() == 0) {
  log.info('Looks like there is nothing to build...')
} else {
  def parallelCount = 5
  def parallelSkip = 0
  def parallelTotal = parallelData.size()
  def parallelLimitReached = 0
  while (parallelTotal != parallelLimitReached) {

    List parallelDataSlice = parallelData.stream().skip(parallelSkip).limit(parallelCount).collect(Collectors.toList())

    parallelLimitReached += parallelDataSlice.size()
    parallelSkip += parallelCount
    Map parallelSteps = parallelDataSlice.collectEntries {
      return ["Building ${it}" : getParallelStep(it)]
    }
    parallelSteps.put('failFast', false)
    parallelResults.putAll(parallel(parallelSteps))
  }
}

...

def getParallelStep(String serviceName) {
  return {
    build(
    job: serviceName,
    parameters: [
      string(name: "parameter", value: "value"),
    ],
    propagate: false,
    wait: true
  )
  }
}

Parameter wait is mandatory!

So, concurrency is 5 builds at a time (parameter parallelCount). The only issue here is that all 5 should finish job run, only after that next 5 will be queued up.

Happy Scripting!

Thank You For Reading