-
Notifications
You must be signed in to change notification settings - Fork 3
/
dslJob.groovy
212 lines (192 loc) · 9.55 KB
/
dslJob.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
import java.net.*
import groovy.json.JsonSlurper
def jsonPayload = new URL("https://raw.githubusercontent.com/zackliu/jenkins/master/config.json").getText();
def state = new JsonSlurper().parseText(jsonPayload);
def jsonPayloadJobs = readFileFromWorkspace(FILE)
def jsonPayloadNode = new URL("https://raw.githubusercontent.com/zackliu/jenkins/master/nodeDefault.json").getText()
def jsonPayloadE2e = new URL("https://raw.githubusercontent.com/zackliu/jenkins/master/e2eDefault.json").getText()
jobsSettings = new JsonSlurper().parseText(jsonPayloadJobs)
nodeSettings = new JsonSlurper().parseText(jsonPayloadNode)
e2eSettings = new JsonSlurper().parseText(jsonPayloadE2e)
def getParameter(variable, defaultSettings, userSettings)
{
if(userSettings != null && userSettings["$variable"] != null) return userSettings["$variable"]
if(defaultSettings != null && defaultSettings["$variable"] != null) return defaultSettings["$variable"]
return null;
}
def createJob(defaultSettings, userSettings)
{
def jobName = getParameter("name", defaultSettings, userSettings)
def folderName = ""
if(jobName == null) return
if(jobsSettings.folder != null)
{
folderName = jobsSettings.folder
jobName = folderName + '/' + jobName
}
job(jobName)
{
def labelExpression = getParameter("label",defaultSettings, userSettings)
label(labelExpression)
def logRotatorEnabled = getParameter("logRotatorEnabled", defaultSettings, userSettings)
if(logRotatorEnabled == true)
{
logRotator
{
daysToKeep(getParameter("daysToKeep", defaultSettings.logRotator, userSettings.logRotator)) // If specified, build records are only kept up to this number of days.
numToKeep(getParameter("numToKeep", defaultSettings.logRotator, userSettings.logRotator)) // If specified, only up to this number of build records are kept.
artifactDaysToKeep(getParameter("artifactDaysToKeep", defaultSettings.logRotator, userSettings.logRotator)) // If specified, artifacts from builds older than this number of days will be deleted, but the logs, history, reports, etc for the build will be kept.
artifactNumToKeep(getParameter("artifactNumToKeep", defaultSettings.logRotator, userSettings.logRotator)) // If specified, only up to this number of builds have their artifacts retained.
}
}
def gitEnabled = getParameter("gitEnabled",defaultSettings, userSettings)
if(gitEnabled == true)
{
scm
{
git
{
remote
{
name(getParameter("name",defaultSettings.git, userSettings.git))
url(getParameter("repo",defaultSettings.git, userSettings.git))
credentials(getParameter("credentials",defaultSettings.git, userSettings.git))
}
branch(getParameter("branches",defaultSettings.git, userSettings.git))
extensions
{
def mergeOptionsEnabled = getParameter("mergeOptionsEnabled",defaultSettings.git, userSettings.git)
if(mergeOptionsEnabled == true)
{
mergeOptions
{
branch(getParameter("branch",defaultSettings.git.mergeOptions, userSettings.git.mergeOptions))
remote(getParameter("remote",defaultSettings.git.mergeOptions, userSettings.git.mergeOptions))
}
}
}
}
}
}
def triggerEnabled = getParameter("triggerEnabled", defaultSettings, userSettings)
if(triggerEnabled == true)
{
triggers
{
upstream(getParameter("upstream", defaultSettings.triggers, userSettings.triggers), "SUCCESS")
}
}
def shellEnabled = getParameter("shellEnabled", defaultSettings, userSettings)
if(shellEnabled == true)
{
steps
{
shell(getParameter("shell", defaultSettings, userSettings).join('\n'))
}
}
def publishersEnabled = getParameter("publishersEnabled", defaultSettings, userSettings)
if(publishersEnabled == true)
{
publishers
{
if(getParameter("extendedEmailEnabled", defaultSettings.publishers, userSettings.publishers) == true)
{
extendedEmail
{
recipientList(getParameter("recipientList", defaultSettings.publishers.extendedEmail, userSettings.publishers.extendedEmail))
defaultSubject(getParameter("defaultSubject", defaultSettings.publishers.extendedEmail, userSettings.publishers.extendedEmail))
defaultContent(getParameter("defaultContent", defaultSettings.publishers.extendedEmail, userSettings.publishers.extendedEmail))
contentType(getParameter("contentType", defaultSettings.publishers.extendedEmail, userSettings.publishers.extendedEmail))
if(getParameter("triggersAlwaysEnabled", defaultSettings.publishers.extendedEmail, userSettings.publishers.extendedEmail) == true)
{
triggers
{
always
{
sendTo
{
if(getParameter("developers", defaultSettings.publishers.extendedEmail.triggersAlways, userSettings.publishers.extendedEmail.triggersAlways) == true) developers()
if(getParameter("recipientList", defaultSettings.publishers.extendedEmail.triggersAlways, userSettings.publishers.extendedEmail.triggersAlways) == true) recipientList()
}
}
}
}
if(getParameter("triggersFailureEnabled", defaultSettings.publishers.extendedEmail, userSettings.publishers.extendedEmail) == true)
{
triggers
{
failure
{
sendTo
{
if(getParameter("developers", defaultSettings.publishers.extendedEmail.triggersFailure, userSettings.publishers.extendedEmail.triggersFailure) == true) developers()
if(getParameter("recipientList", defaultSettings.publishers.extendedEmail.triggersFailure, userSettings.publishers.extendedEmail.triggersFailure) == true) recipientList()
}
}
}
}
if(getParameter("triggersFixedEnabled", defaultSettings.publishers.extendedEmail, userSettings.publishers.extendedEmail) == true)
{
triggers
{
fixed
{
sendTo
{
if(getParameter("developers", defaultSettings.publishers.extendedEmail.triggersFixed, userSettings.publishers.extendedEmail.triggersFixed) == true) developers()
if(getParameter("recipientList", defaultSettings.publishers.extendedEmail.triggersFixed, userSettings.publishers.extendedEmail.triggersFixed) == true) recipientList()
}
}
}
}
}
}
if(getParameter("gitEnabled", defaultSettings.publishers, userSettings.publishers) == true)
{
git
{
if(getParameter("pushOnlyIfSuccess", defaultSettings.publishers.git, userSettings.publishers.git) == true) pushOnlyIfSuccess()
if(getParameter("pushMerge", defaultSettings.publishers.git, userSettings.publishers.git) == true) pushMerge()
}
}
}
}
}
}
def createBranch(userSettings)
{
branch = userSettings.branch
userSettings.jobs.each
{
jobSettings ->
if(jobSettings.template == "nodeDefault")
{
createJob(nodeSettings, jobSettings)
}
else if (jobSettings.template == "e2eDefault")
{
createJob(e2eSettings, jobSettings)
}
else
{
createJob(null, jobSettings)
}
}
}
def createWorkflow(userSettings)
{
if(userSettings.folder != null)
{
folder(userSettings.folder)
{
displayName(userSettings.folder)
}
}
partner = userSettings.partner
userSettings.workflow.each
{
branchSettings ->
createBranch(branchSettings)
}
}
createWorkflow(jobsSettings)