This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
/
JenkinsJobManager.groovy
153 lines (120 loc) · 6.13 KB
/
JenkinsJobManager.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.entagen.jenkins
import java.util.regex.Pattern
class JenkinsJobManager {
String templateJobPrefix
String templateBranchName
String gitUrl
String nestedView
String jenkinsUrl
String branchNameRegex
Boolean dryRun = false
JenkinsApi jenkinsApi
GitApi gitApi
JenkinsJobManager(Map props) {
for (property in props) {
this."${property.key}" = property.value
}
initJenkinsApi()
initGitApi()
}
void syncWithRepo() {
List<String> allBranchNames = gitApi.branchNames
List<String> allJobNames = jenkinsApi.jobNames
// ensure that there is at least one job matching the template pattern, collect the set of template jobs
List<TemplateJob> templateJobs = findRequiredTemplateJobs(allJobNames)
// create any missing template jobs and delete any jobs matching the template patterns that no longer have branches
syncJobs(allBranchNames, allJobNames, templateJobs)
// create any missing branch views, scoped within a nested view if we were given one
syncViews(allBranchNames)
}
public void syncJobs(List<String> allBranchNames, List<String> allJobNames, List<TemplateJob> templateJobs) {
List<String> currentTemplateDrivenJobNames = templateDrivenJobNames(templateJobs, allJobNames)
List<String> nonTemplateBranchNames = allBranchNames - templateBranchName
List<ConcreteJob> expectedJobs = this.expectedJobs(templateJobs, nonTemplateBranchNames)
createMissingJobs(expectedJobs, currentTemplateDrivenJobNames, templateJobs)
deleteDeprecatedJobs(currentTemplateDrivenJobNames - expectedJobs.jobName)
}
public void createMissingJobs(List<ConcreteJob> expectedJobs, List<String> currentJobs, List<TemplateJob> templateJobs) {
List<ConcreteJob> missingJobs = expectedJobs.findAll { !currentJobs.contains(it.jobName) }
if (!missingJobs) return
for(ConcreteJob missingJob in missingJobs) {
println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}"
jenkinsApi.cloneJobForBranch(missingJob, templateJobs)
}
}
public void deleteDeprecatedJobs(List<String> deprecatedJobNames) {
if (!deprecatedJobNames) return
println "Deleting deprecated jobs:\n\t${deprecatedJobNames.join('\n\t')}"
deprecatedJobNames.each { String jobName ->
jenkinsApi.deleteJob(jobName)
}
}
public List<ConcreteJob> expectedJobs(List<TemplateJob> templateJobs, List<String> branchNames) {
branchNames.collect { String branchName ->
templateJobs.collect { TemplateJob templateJob -> templateJob.concreteJobForBranch(branchName) }
}.flatten()
}
public List<String> templateDrivenJobNames(List<TemplateJob> templateJobs, List<String> allJobNames) {
List<String> templateJobNames = templateJobs.jobName
List<String> templateBaseJobNames = templateJobs.baseJobName
// don't want actual template jobs, just the jobs that were created from the templates
return (allJobNames - templateJobNames).findAll { String jobName ->
templateBaseJobNames.find { String baseJobName -> jobName.startsWith(baseJobName)}
}
}
List<TemplateJob> findRequiredTemplateJobs(List<String> allJobNames) {
String regex = /^($templateJobPrefix-.*)-($templateBranchName)$/
List<TemplateJob> templateJobs = allJobNames.findResults { String jobName ->
TemplateJob templateJob = null
jobName.find(regex) { full, baseJobName, branchName ->
templateJob = new TemplateJob(jobName: full, baseJobName: baseJobName, templateBranchName: branchName)
}
return templateJob
}
assert templateJobs?.size() > 0, "Unable to find any jobs matching template regex: $regex\nYou need at least one job to match the templateJobPrefix and templateBranchName suffix arguments"
return templateJobs
}
public void syncViews(List<String> allBranchNames) {
List<String> existingViewNames = jenkinsApi.getViewNames(this.nestedView)
List<BranchView> expectedBranchViews = allBranchNames.collect { String branchName -> new BranchView(branchName: branchName, templateJobPrefix: this.templateJobPrefix) }
List<BranchView> missingBranchViews = expectedBranchViews.findAll { BranchView branchView -> !existingViewNames.contains(branchView.viewName)}
addMissingViews(missingBranchViews)
List<String> deprecatedViewNames = getDeprecatedViewNames(existingViewNames, expectedBranchViews)
deleteDeprecatedViews(deprecatedViewNames)
}
public void addMissingViews(List<BranchView> missingViews) {
println "Missing views: $missingViews"
for (BranchView missingView in missingViews) {
jenkinsApi.createViewForBranch(missingView, this.nestedView)
}
}
public List<String> getDeprecatedViewNames(List<String> existingViewNames, List<BranchView> expectedBranchViews) {
return existingViewNames?.findAll { it.startsWith(this.templateJobPrefix) } - expectedBranchViews?.viewName ?: []
}
public void deleteDeprecatedViews(List<String> deprecatedViewNames) {
println "Deprecated views: $deprecatedViewNames"
for(String deprecatedViewName in deprecatedViewNames) {
jenkinsApi.deleteView(deprecatedViewName, this.nestedView)
}
}
JenkinsApi initJenkinsApi() {
if (!jenkinsApi) {
assert jenkinsUrl != null
if (dryRun) {
println "DRY RUN! Not executing any POST commands to Jenkins, only GET commands"
this.jenkinsApi = new JenkinsApiReadOnly(jenkinsServerUrl: jenkinsUrl)
} else {
this.jenkinsApi = new JenkinsApi(jenkinsServerUrl: jenkinsUrl)
}
}
return this.jenkinsApi
}
GitApi initGitApi() {
if (!gitApi) {
assert gitUrl != null
this.gitApi = new GitApi(gitUrl: gitUrl)
gitApi.branchNameFilter = Pattern.compile(this.branchNameRegex)
}
return this.gitApi
}
}