Parallel jobs using build thread loop plugin in jenkins

I am creating a jenkins thread using an assembly thread plugin that will capture all jobs, compare the name with the regular expression, and if it matches it, it will call the job assembly.
This works fine for me:

import jenkins.model.Jenkins import java.util.regex.* Pattern myRegex = ~/release_status.*/ for (item in jenkins.model.Jenkins.instance.items) { if (item.name ==~ myRegex) { build( "$item.name" ) } } 

However, it takes a lot of time to create all the suitable tasks (now there are 20, but there can be many).
I try to make it run every work in parallel, but I cannot understand the groovy syntax.
I tried 3 different ways:

 import jenkins.model.Jenkins import java.util.regex.* Pattern myRegex = ~/release_status.*/ parallel ( { for (item in jenkins.model.Jenkins.instance.items) { if (item.name ==~ myRegex) { build( "$item.name" ) } } } ) 

^^ This still works, but works the same as before. He follows one task at a time and does not build the next until the previous one is completed.

 import jenkins.model.Jenkins import java.util.regex.* Pattern myRegex = ~/release_status.*/ parallel ( { for (item in jenkins.model.Jenkins.instance.items) { if (item.name ==~ myRegex) { { build( "$item.name" ) }, } } } ) 

^^ These are errors with

 Script1.groovy: 9: Ambiguous expression could be either a parameterless closure expression or an isolated open code block; solution: Add an explicit closure parameter list, eg {it -> ...}, or force it to be treated as an open block by giving it a label, eg L:{...} @ line 9, column 9. { build( "$item.name" ) }, 

the gap

 import jenkins.model.Jenkins import java.util.regex.* Pattern myRegex = ~/release_status.*/ parallel ( [ for (item in jenkins.model.Jenkins.instance.items) { if (item.name ==~ myRegex) { useless: { build( "$item.name" ) }, } } ] ) 

the gap

 import jenkins.model.Jenkins import java.util.regex.* Pattern myRegex = ~/release_status.*/ parallel ( for (item in jenkins.model.Jenkins.instance.items) { if (item.name ==~ myRegex) { { build( "$item.name" ) }, } } ) 

Both of the blocks above error with the following:

 org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 5: unexpected token: for @ line 5, column 5. for (item in jenkins.model.Jenkins.instance.items) 

There is a lot of code here, but its pretty simple. Looking around, I cannot find many good resources on groovy.

+2
source share
2 answers

parallel takes a Closures list, so you can use collect to return the 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 returns only closure if the name passes (and not closure for each element, much of which will end earlier):

 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") } } 
+5
source

Here is my solution that may be useful with the 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) } } } 
0
source

All Articles