-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoJobBuilder.groovy
79 lines (61 loc) · 2.15 KB
/
AutoJobBuilder.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
#!groovy
package com.chumbok.sdlc
import groovy.json.JsonSlurper
def org = 'mmahmoodictbd'
println("Fetching repositories from ")
def repos = new JsonSlurper()
.parse(new URL("https://api.github.com/users/${org}/repos?per_page=100").newReader())
for (repo in repos) {
println("https://raw.githubusercontent.com/${org}/${repo.name}/master/Jenkinsfile")
jenkinsfileUrl = new URL("https://raw.githubusercontent.com/${org}/${repo.name}/master/Jenkinsfile")
def hasJenkinsfile = false
try {
hasJenkinsfile = !jenkinsfileUrl.text.isEmpty()
} catch (e) {}
if (!hasJenkinsfile) {
println("${repo.name} does not have Jenkinsfile.")
continue
}
def jobDef = [name: repo.name, jenkinsFolder: org, gitUrl: repo.svn_url]
println(jobDef)
println "Creating job: ${jobDef.name} for ${jobDef.gitUrl}"
println "Creating Project Folder: ${jobDef.jenkinsFolder}"
folder(jobDef.jenkinsFolder) {
}
pipelineJob(jobDef.jenkinsFolder + "/" + jobDef.name) {
definition {
cpsScm {
scm {
git {
remote {
url(jobDef.gitUrl + '.git')
}
branches('master', '**/feature*')
scriptPath('Jenkinsfile')
}
}
}
}
}
println "Pipeline job: ${jobDef.jenkinsFolder}/${jobDef.name} created successfully."
multibranchPipelineJob(jobDef.jenkinsFolder + "/" + jobDef.name + "-ci") {
description("On SCM: ${jobDef.gitUrl}")
triggers {
cron('H H * * *')
}
branchSources {
github {
id("repo-${jobDef.name}")
repoOwner(jobDef.jenkinsFolder)
repository(jobDef.name)
scanCredentialsId('github-ci')
}
}
orphanedItemStrategy {
discardOldItems {
daysToKeep(1) // remove merged pipelines every day
}
}
}
println "Multibranch Pipeline job: ${jobDef.jenkinsFolder}/${jobDef.name} created successfully."
}