Parallel jobs using build flow plugin with loop in jenkins

心已入冬 提交于 2019-12-01 01:20:41

parallel takes a list of Closures, so you should be able to use collect to return a list:

import jenkins.model.Jenkins
import java.util.regex.*

Pattern myRegex = ~/release_status.*/
parallel jenkins.model.Jenkins.instance.items.collect { item ->
    { -> 
        if (item.name ==~ myRegex) {
            build( "$item.name" )
        }
    }
}

An alternative that only returns a Closure if the name passes (rather than a Closure for every item, a lot of which will finish early) is:

import jenkins.model.Jenkins
import java.util.regex.*

Pattern myRegex = ~/release_status.*/

parallel Jenkins.instance.items.findAll { item -> item.name ==~ myRegex}
                               .collect { item -> { -> build("$item.name") } }

Here is my solution that might be helpful with Folders plugin:

import jenkins.model.Jenkins
//Configuration block
def dryRun = true
def projectFolder = 'Path/To/Folder'
def phases = ['.*-Build','.*-Deploy', '.*-Regression', '.*Service-Full-Performance-Test']
//Configuration block

def phasesRegex = []
phases.each{
  phasesRegex.push(~/${projectFolder}\/${it}/)
}

def jobs = []

items = jenkins.model.Jenkins.instance.getItemByFullName(projectFolder);

items.getAllJobs().collect{
  jobs.push(it.getFullName())
}


phasesRegex.each{
  run = jobs.grep(it)
  parallel run.collect{ job ->
    { ->
      if (dryRun) println "Dry Run of Job: ${job}"
      else build(job)
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!