-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_jobs.groovy
472 lines (470 loc) · 16.9 KB
/
generate_jobs.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
public class Settings {
public final gitUrl
public final gitCredentials
private final workspace
private final jobPropertiesDir
private final Map propFiles = [:]
private final Map jobTriggers = [:]
private final Map stackImageMap = [:]
private jobDefs = null
public Settings(remoteConfig, __FILE__) {
this.gitUrl = remoteConfig.url
this.gitCredentials = remoteConfig.credentialsId
this.workspace = new File(__FILE__).parentFile.absoluteFile
this.jobPropertiesDir = new File(workspace, "job-properties")
/**
* Get mappings for image jobs to trigger (and block) deploy jobs
**/
for (jobDef in this.getJobs()) {
def ( imageDir, gitBranch, jobType, stackName ) = jobDef.tokenize(':')
if ("stack" == jobType) {
def imageJobName = this.getJobName(gitBranch, imageDir, "image", "-")
def jobName = this.getJobName(gitBranch, imageDir, jobType, stackName)
if (imageJobName != null) {
stackImageMap["$gitBranch-$imageDir-$stackName"] = imageJobName
}
def properties = loadStackProps(gitBranch, imageDir, stackName)
def jobPrefix = properties.JENKINS_JOB_PREFIX
if ("y" != properties.MANUAL_DEPLOY) {
if (jobTriggers["$gitBranch-$imageDir"] == null) {
jobTriggers["$gitBranch-$imageDir"] = [jobName]
} else {
jobTriggers["$gitBranch-$imageDir"] << jobName
}
}
}
}
}
/**
* Runs `ndt list-jobs` and returns the result as a string array
**/
public String[] getJobs() {
if (this.jobDefs == null) {
def process = new ProcessBuilder(["ndt", "list-jobs"])
.redirectErrorStream(true)
.directory(this.workspace)
.start()
def ret = []
process.inputStream.eachLine {
println it
ret << it
}
process.waitFor();
this.jobDefs = ret;
}
return this.jobDefs
}
/**
* Loads of file into a properties object optionally ignoring FileNotFoundException
**/
public Properties loadProps(fileName, quiet=false) {
if (this.propFiles[fileName] != null) {
return this.propFiles[fileName]
} else {
def properties = new Properties()
this.propFiles[fileName] = properties
File propertiesF = new File(this.jobPropertiesDir, fileName)
try {
propertiesF.withInputStream {
properties.load(it)
}
} catch (java.io.FileNotFoundException ex) {
if (!quiet) {
println "Job properties " + propertiesF.absolutePath + " not found"
}
} finally {
return properties
}
}
}
/**
* Load properties for an image job
*/
public Properties loadImageProps(gitBranch, imageDir, quiet=false) {
return loadProps("image-$gitBranch-${imageDir}.properties", quiet)
}
/**
* Load properties for a deploy job
**/
public Properties loadStackProps(gitBranch, imageDir, stackName, quiet=false) {
return loadProps("stack-$gitBranch-$imageDir-${stackName}.properties", quiet)
}
/**
* Load properties for a docker job
**/
public Properties loadDockerProps(gitBranch, imageDir, dockerName, quiet=false) {
return loadProps("docker-$gitBranch-$imageDir-${dockerName}.properties", quiet)
}
/**
* Gets triggers defined for a bake job
**/
public List getJobTriggers(gitBranch, imageDir) {
return jobTriggers["$gitBranch-$imageDir"]
}
/**
* Get image job name that matches the stack job
**/
public String getImageJobForStack(gitBranch, imageDir, stackName) {
return stackImageMap["$gitBranch-$imageDir-$stackName"]
}
/**
* Resolves a name for the given job
**/
public String getJobName(gitBranch, imageDir, jobType, stackName) {
def fileName = "${gitBranch}-${imageDir}"
if (jobType == "image") {
fileName = "image-${fileName}.properties"
} else if (jobType == "stack"){
fileName = "stack-${fileName}-${stackName}.properties"
} else if (jobType == "docker") {
fileName = "docker-${fileName}-${stackName}.properties"
} else {
return null
}
def properties = loadProps(fileName, true)
if (properties.JENKINS_JOB_NAME != null) {
return properties.JENKINS_JOB_NAME
} else {
if (properties.JENKINS_JOB_PREFIX == null) {
return null
}
def jobPrefix = properties.JENKINS_JOB_PREFIX
if (stackName != null && stackName != "-") {
if (jobType == "stack") {
return "$jobPrefix-$imageDir-deploy-$stackName"
} else if (jobType == "docker") {
return "$jobPrefix-$imageDir-docker-bake-$stackName"
}
} else if (properties.BAKE_IMAGE_BRANCH != null && properties.BAKE_IMAGE_BRANCH != gitBranch) {
return "$jobPrefix-$imageDir-promote"
} else {
return "$jobPrefix-$imageDir-bake"
}
}
}
static {
}
}
remoteConfig = SEED_JOB.scm.userRemoteConfigs[0]
final Settings s = new Settings(remoteConfig, __FILE__)
def private addSCMTriggers(job, jobType, properties, s) {
if (properties.MANUAL_DEPLOY == "y") {
return;
}
job.with {
triggers {
if (jobType == "image" && properties.IMAGE_CRON != null) {
cron(properties.IMAGE_CRON)
}
if (jobType == "stack" && properties.STACK_CRON != null) {
cron(properties.STACK_CRON)
}
if (jobType == "docker" && properties.DOCKER_CRON != null) {
cron(properties.DOCKER_CRON)
}
if (s.gitUrl.indexOf("github.com") > -1) {
githubPush()
} else if (s.gitUrl.indexOf("bitbucket.org") > -1) {
bitbucketPush()
} else if (s.gitUrl.indexOf("gitlab.com") > -1) {
gitlabPush {
buildOnPushEvents(true)
}
} else {
scm('H/5 * * * *')
}
}
}
}
def private addParamTriggers(job, gitBranch, imageDir, s) {
if (s.getJobTriggers(gitBranch, imageDir) != null) {
job.with {
publishers {
downstreamParameterized {
trigger(s.getJobTriggers(gitBranch, imageDir)) {
condition('SUCCESS')
parameters {
propertiesFile("ami.properties")
}
}
}
}
}
}
}
viewMap = [:]
jobDefs = s.getJobs()
/**
* Do the actual job generation
**/
for (jobDef in jobDefs) {
def ( imageDir, gitBranch, jobType, stackName ) = jobDef.tokenize(':')
Properties properties
Properties imageProperties = s.loadImageProps(gitBranch, imageDir, true)
def jobPrefix = imageProperties.JENKINS_JOB_PREFIX
def jobName = s.getJobName(gitBranch, imageDir, jobType, stackName)
if (jobName == null) {
continue
}
def blockOnArray = [SEED_JOB.name]
if (jobType == "stack") {
properties = s.loadStackProps(gitBranch, imageDir, stackName)
jobPrefix = properties.JENKINS_JOB_PREFIX
if ("y" == properties.SKIP_STACK_JOB || (imageDir == "bootstrap" && "n" != properties.SKIP_STACK_JOB)) {
continue
}
println "Generating stack deploy job $jobName"
imageJob = s.getImageJobForStack(gitBranch, imageDir, stackName)
if (imageJob != null) {
imageTag = imageJob.replaceAll("-", "_")
blockOnArray << imageJob
} else {
imageTag = ""
}
def dryRun = ""
if ((gitBranch == "prod" && "n" != properties.MANUAL_ACCEPT_DEPLOY) ||
"y" == properties.MANUAL_ACCEPT_DEPLOY) {
dryRun = "stage \"Dry run to accept changeset\"\n" +
" sh \"ndt deploy-stack -d $imageDir $stackName \\\"\$AMI_ID\\\" $imageTag\"\n" +
" input(message: \"Does the changeset above look ok?\")\n "
}
def postDeploy=""
if (properties.POST_DEPLOY != null) {
postDeploy="sh \"" + properties.POST_DEPLOY + "\""
}
def job = pipelineJob(jobName) {
parameters {
stringParam('AMI_ID', '', 'Ami id if triggered from a bake job')
}
definition {
cps{
script("""env.GIT_BRANCH=\"$gitBranch\"
node {
checkout([\$class: 'GitSCM', branches: [[name: \"*/$gitBranch\"]],
doGenerateSubmoduleConfigurations: false,
extensions: [
[\$class: 'PathRestriction',
includedRegions: '\\\\Q$imageDir/stack-$stackName/\\\\E.*',
excludedRegions: '']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: \"$s.gitCredentials\",
url: \"$s.gitUrl\"]]])
wrap([\$class: 'AnsiColorBuildWrapper']) {
${dryRun}stage \"Deploy or update stack\"
sh \"ndt deploy-stack $imageDir $stackName \\\"\$AMI_ID\\\" $imageTag\"
${postDeploy}
}
archiveArtifacts artifacts: 'ami.properties'
}
""")
}
}
description("nameless-deploy-tools deploy stack job")
blockOn(blockOnArray)
}
addSCMTriggers(job, jobType, properties, s)
if (viewMap[jobPrefix] == null) {
viewMap[jobPrefix] = [jobName]
} else {
viewMap[jobPrefix] << jobName
}
if (properties.AUTOPROMOTE_TO_BRANCH != null) {
targetJobs = []
for (toBranch in properties.AUTOPROMOTE_TO_BRANCH.split(",")) {
if (toBranch != gitBranch) {
targetJobs << s.getJobName(toBranch, imageDir, "image", "-")
}
}
job.with {
publishers {
downstreamParameterized {
trigger(targetJobs) {
condition('SUCCESS')
parameters {
propertiesFile("ami.properties")
}
}
}
}
}
}
if ("y" != properties.DISABLE_RAMPDOWN) {
def undeployJobName = jobName.replaceAll("deploy", "undeploy")
if (undeployJobName == jobName) {
undeployJobName += "-undeploy"
}
def undeployJob = freeStyleJob(undeployJobName) {
parameters {
choiceParam('CONFIRM', ["No", "Yes"], 'Are you sure?')
}
scm {
git {
remote {
name("origin")
url(s.gitUrl)
credentials(s.gitCredentials)
}
branch(gitBranch)
}
}
steps {
shell("""if ! [ \"Yes\" = \"\$CONFIRM\" ]; then
exit 1
fi
ndt undeploy-stack $imageDir $stackName
""")
}
description("nameless-deploy-tools undeploy stack job")
blockOn([jobName])
}
viewMap[jobPrefix] << undeployJobName
}
} else if (jobType == "docker") {
properties = s.loadDockerProps(gitBranch, imageDir, stackName)
jobPrefix = properties.JENKINS_JOB_PREFIX
if ("y" == properties.SKIP_DOCKER_JOB || (imageDir == "bootstrap" && "n" != properties.SKIP_DOCKER_JOB)) {
continue
}
if (properties.BAKE_IMAGE_BRANCH != null && properties.BAKE_IMAGE_BRANCH != gitBranch) {
continue
}
println "Generating docker bake job $jobName"
def job = freeStyleJob(jobName) {
scm {
git {
remote {
name("origin")
url(s.gitUrl)
credentials(s.gitCredentials)
}
branch(gitBranch)
}
}
blockOn(blockOnArray)
}
addSCMTriggers(job, jobType, properties, s)
job.with {
steps {
shell("ndt bake-docker " + imageDir + " " + stackName)
}
description("nameless-deploy-tools bake docker job")
configure { project ->
project / 'scm' / 'extensions' << 'hudson.plugins.git.extensions.impl.PathRestriction' {
includedRegions "\\Q$imageDir/docker-\\E.*"
}
project / 'buildWrappers' << 'hudson.plugins.ansicolor.AnsiColorBuildWrapper' {
colorMapName 'xterm'
}
}
}
if (viewMap[jobPrefix] == null) {
viewMap[jobPrefix] = [jobName]
} else {
viewMap[jobPrefix] << jobName
}
} else if (jobType == "image") {
properties = imageProperties
if ("y" == properties.SKIP_IMAGE_JOB || (imageDir == "bootstrap" && "n" != properties.SKIP_IMAGE_JOB)) {
continue
}
jobPrefix = properties.JENKINS_JOB_PREFIX
def job = freeStyleJob(jobName) {
scm {
git {
remote {
name("origin")
url(s.gitUrl)
credentials(s.gitCredentials)
}
branch(gitBranch)
}
}
blockOn(blockOnArray)
}
job.with {
configure { project ->
project / 'scm' / 'extensions' << 'hudson.plugins.git.extensions.impl.PathRestriction' {
includedRegions "\\Q$imageDir/\\E.*"
excludedRegions "\\Q$imageDir/stack-\\E.*\n\\Q$imageDir/docker-\\E.*"
}
project / 'buildWrappers' << 'hudson.plugins.ansicolor.AnsiColorBuildWrapper' {
colorMapName 'xterm'
}
}
}
if (properties.BAKE_IMAGE_BRANCH != null && properties.BAKE_IMAGE_BRANCH != gitBranch) {
/**
* Create a image promotion job instead of a image baking job
**/
def promotableJob = s.getJobName(properties.BAKE_IMAGE_BRANCH, imageDir, "image", "-")
println "Generating image promote job $jobName from $promotableJob"
job.with {
steps {
shell("ndt promote-image \$AMI_ID $jobName")
}
description("nameless-deploy-tools promote image job")
configure { project ->
project / 'properties' / 'hudson.model.ParametersDefinitionProperty' / 'parameterDefinitions' << 'jp.ikedam.jenkins.plugins.extensible__choice__parameter.ExtensibleChoiceParameterDefinition' {
name 'AMI_ID'
editable 'false'
choiceListProvider(class: "jp.ikedam.jenkins.plugins.extensible_choice_parameter.SystemGroovyChoiceListProvider") {
groovyScript {
script """def process = new ProcessBuilder(["ndt", "get-images", "$promotableJob"])
.redirectErrorStream(true)
.start()
def ret = []
process.inputStream.eachLine {
ret << it
}
process.waitFor();
return ret
"""
sandbox "false"
}
usePredefinedVariables "false"
}
}
}
}
} else {
println "Generating image bake job $jobName"
job.with {
steps {
shell("ndt bake-image " + imageDir)
}
description("nameless-deploy-tools bake image job")
}
}
addSCMTriggers(job, jobType, properties, s)
addParamTriggers(job, gitBranch, imageDir, s)
if (viewMap[jobPrefix] == null) {
viewMap[jobPrefix] = [jobName]
} else {
viewMap[jobPrefix] << jobName
}
}
}
/**
* Genereate views for all the jobs
**/
for (item in viewMap) {
println item
listView(item.key) {
columns {
status()
buildButton()
name()
progressBar()
cronTrigger()
lastBuildConsole()
lastSuccess()
lastDuration()
lastFailure()
}
jobs {
for (jobName in item.value) {
name(jobName)
}
}
}
}