-
Notifications
You must be signed in to change notification settings - Fork 411
/
findAllTemplatizedJobsByFolder.groovy
45 lines (39 loc) · 1.58 KB
/
findAllTemplatizedJobsByFolder.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//This script will find all templates in a Jenkins instance, then find all jobs using that template in the specified folder
import com.cloudbees.hudson.plugins.modeling.impl.jobTemplate.JobTemplate
import com.cloudbees.hudson.plugins.modeling.impl.jobTemplate.JobPropertyImpl
import com.cloudbees.hudson.plugins.folder.Folder
def jenkinsJobs = Jenkins.instance.getAllItems(Job.class)
def folderName = 'someFolder' //Folder name
folderItem = Jenkins.instance.getAllItems(Folder.class).find{it.name.equals(folderName)}
if(folderItem == null){
println "Folder " + folderName + " does not exist!"
return
}
findAllTemplates(((com.cloudbees.hudson.plugins.folder.Folder) folderItem).getItems())
def findAllTemplates(items){
def jenkinsTemplates = Jenkins.instance.getAllItems(JobTemplate.class)
def count = 0
jenkinsTemplates.each{ template ->
count = findTemplatizedJobs(items, template.displayName, count)
println "\n " + count + "\n"
count = 0
}
}
def findTemplatizedJobs(items, templateName, count){
println templateName
def currItem
for(item in items){
currItem = item
if(item instanceof AbstractProject || item instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob){
if(item.getProperty(JobPropertyImpl.class) != null){
if(item.getProperty(JobPropertyImpl.class).getModel().name == templateName){
count++
}
}
}
}
if (currItem instanceof com.cloudbees.hudson.plugins.folder.Folder){
findTemplatizedJobs(((com.cloudbees.hudson.plugins.folder.Folder) currItem).getItems(), templateName, count)
}
return count
}