-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
356 lines (306 loc) · 9.72 KB
/
Jenkinsfile
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
import hudson.AbortException
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
/* Send notifications to Github.
* If it is an on-demand run then no notifications are sent.
*/
class Notification {
def pipeline
String context
String details_url
String aws_url
boolean on_demand
/* @param pipeline Jenkins pipeline context.
* @param context Github notification context (the bold text).
* @param details_url Link for "details" button.
* @param aws_url Link to cloud where logs are stored.
* @param on_demand True if this is an on-demand run.
*
* There are two types of notifications:
* a) Summary (i.e. sssd-ci: Success. details: @details_url)
* b) Single build (i.e. sssd-ci/fedora28: Success. details: @aws_url)
*/
Notification(pipeline, context, details_url, aws_url, on_demand) {
this.pipeline = pipeline
this.context = context
this.details_url = details_url
this.aws_url = aws_url
this.on_demand = on_demand
}
/* Send notification. If system is not null single build is notified. */
def notify(status, message, system = null) {
def context = system ? "${this.context}/${system}" : this.context
this.pipeline.echo "[${context}] ${status}: ${message}"
if (this.on_demand) {
return
}
this.send(status, message, context, this.getTargetURL(system))
}
private def send(status, message, context, url) {
this.pipeline.githubNotify status: status,
context: context,
description: message,
targetUrl: url
}
private def getTargetURL(system) {
if (system) {
return String.format(
'%s/%s/%s/%s/index.html',
this.aws_url,
this.pipeline.env.BRANCH_NAME,
this.pipeline.env.BUILD_ID,
system
)
}
return this.details_url
}
}
/* Manage test run. */
class Test {
def pipeline
String system
Notification notification
String artifactsdir
String basedir
String codedir
String target
/* @param pipeline Jenkins pipeline context.
* @param system System to test on.
* @param notification Notification object.
*/
Test(pipeline, system, notification) {
this.pipeline = pipeline
this.system = system
this.notification = notification
this.basedir = "/home/fedora"
this.target = pipeline.env.CHANGE_TARGET
}
/* Test entry point. */
def run() {
/* These needs to be set here in order to get correct workspace. */
this.artifactsdir = "${this.pipeline.env.WORKSPACE}/artifacts/${this.system}"
this.codedir = "${this.pipeline.env.WORKSPACE}/sssd"
/* Clean-up previous artifacts just to be sure there are no leftovers. */
this.pipeline.sh "rm -fr ${this.artifactsdir} || :"
try {
this.pipeline.echo "Running on ${this.pipeline.env.NODE_NAME}"
this.notify('PENDING', 'Build is in progress.')
this.checkout()
try {
this.rebase()
} catch (e) {
this.pipeline.error "Unable to rebase on ${this.target}."
}
this.pipeline.echo "Executing tests, started at ${this.getCurrentTime()}"
def command = String.format(
'%s/sssd-test-suite -c "%s" run --sssd "%s" --artifacts "%s" --update --prune',
"${this.basedir}/sssd-test-suite",
"${this.basedir}/configs/${this.system}.json",
this.codedir,
this.artifactsdir
)
def rc = this.pipeline.sh script: command, returnStatus: true
if (rc == 255) {
this.pipeline.error "Timeout reached."
} else if (rc != 0) {
this.pipeline.error "Some tests failed."
}
this.pipeline.echo "Finished at ${this.getCurrentTime()}"
this.notify('SUCCESS', 'Success.')
} catch (FlowInterruptedException e) {
this.notify('ERROR', 'Aborted.')
throw e
} catch (AbortException e) {
this.notify('ERROR', e.getMessage())
throw e
} catch (e) {
this.notify('ERROR', 'Build failed.')
throw e
} finally {
this.archive()
}
}
def getCurrentTime() {
def date = new Date()
return date.format('dd. MM. yyyy HH:mm:ss')
}
def checkout() {
this.pipeline.dir('sssd') {
this.pipeline.checkout this.pipeline.scm
}
}
def rebase() {
/* Do not rebase if there is no target (not a pull request). */
if (!this.target) {
return
}
this.pipeline.echo "Rebasing on ${this.target}"
// Fetch refs
this.git(String.format(
"fetch --no-tags --progress origin +refs/heads/%s:refs/remotes/origin/%s",
this.target, this.target
))
// Remove left overs from previous rebase if there are any
this.git("rebase --abort || :")
// Just to be sure
this.pipeline.sh "rm -fr '${this.codedir}/.git/rebase-apply' || :"
// Rebase
this.git("rebase origin/${this.target}")
}
def git(command) {
this.pipeline.sh "git -C '${this.codedir}' ${command}"
}
def archive() {
this.pipeline.archiveArtifacts artifacts: "artifacts/**",
allowEmptyArchive: true
this.pipeline.sh String.format(
'%s/sssd-ci archive --name "%s" --system "%s" --artifacts "%s"',
"${this.basedir}/sssd-ci",
"${pipeline.env.BRANCH_NAME}/${pipeline.env.BUILD_ID}",
this.system,
"${artifactsdir}"
)
this.pipeline.sh "rm -fr ${this.artifactsdir}"
}
def notify(status, message) {
this.notification.notify(status, message, this.system)
}
}
/* Manage test run for on demand test. */
class OnDemandTest extends Test {
String repo
String branch
/* @param pipeline Jenkins pipeline context.
* @param system System to test on.
* @param notification Notification object.
* @param repo Repository fetch URL.
* @param branch Branch to checkout.
*/
OnDemandTest(pipeline, system, notification, repo, branch) {
super(pipeline, system, notification)
this.repo = repo
this.branch = branch
}
def run() {
this.pipeline.echo "Repository: ${this.repo}"
this.pipeline.echo "Branch: ${this.branch}"
super.run()
}
def checkout() {
this.pipeline.dir('sssd') {
this.pipeline.git branch: this.branch, url: this.repo
}
}
def rebase() {
/* Do nothing. */
}
def archive() {
this.pipeline.echo 'On demand run. Artifacts are not stored in the cloud.'
this.pipeline.echo 'They are accessible only from Jenkins.'
this.pipeline.echo "${this.pipeline.env.BUILD_URL}/artifact/artifacts/${this.system}"
this.pipeline.archiveArtifacts artifacts: "artifacts/**",
allowEmptyArchive: true
this.pipeline.sh "rm -fr ${this.artifactsdir}"
}
}
def systems = []
def on_demand = params.ON_DEMAND ? true : false
def notification = new Notification(
this, 'sssd-ci',
'https://github.com/SSSD/sssd/blob/master/contrib/test-suite/README.md',
'https://s3.eu-central-1.amazonaws.com/sssd-ci',
on_demand
)
try {
stage('Get system list') {
node('master') {
if (params.SYSTEMS && params.SYSTEMS != 'all') {
/* This is a parametrized custom build. System list is taken
* from provided parameter. */
systems = params.SYSTEMS.split()
} else {
/* This is automated build or custom build that requested
* tests on all systems (i.e. same systems as master branch) */
def branch = env.CHANGE_TARGET ? env.CHANGE_TARGET : 'master'
def config = "systems-${branch}"
echo "Using configuration: ${config}"
/* Configuration is read from Jenkins-managed configuration file.
* Path to the configuration is loaded into env.CONFIG_PATH */
configFileProvider([
configFile(fileId: config, variable: 'CONFIG_PATH')
]) {
def contents = readFile "${env.CONFIG_PATH}"
systems = contents.split()
}
}
echo 'Test will be done on following systems:'
echo systems.join(', ')
}
}
/* Setup nice build description so pull request are easy to find. */
stage('Setup description') {
node('master') {
if (on_demand) {
/* user: branch */
def build = currentBuild.rawBuild
def cause = build.getCause(hudson.model.Cause.UserIdCause.class)
def user = cause.getUserId()
currentBuild.description = "${user}: ${params.REPO_BRANCH}"
} else {
if (env.CHANGE_TARGET) {
/* PR XXX: pull request name */
def title = sh returnStdout: true, script: """
curl -s https://api.github.com/repos/SSSD/sssd/pulls/${env.CHANGE_ID} | \
python -c "import sys, json; print(json.load(sys.stdin).get('title'))"
"""
currentBuild.description = "PR ${env.CHANGE_ID}: ${title}"
} else {
/* Branch: name */
currentBuild.description = "Branch: ${env.BRANCH_NAME}"
}
}
}
}
stage('Prepare systems') {
notification.notify('PENDING', 'Pending.')
/* Notify that all systems are pending. */
for (system in systems) {
notification.notify('PENDING', 'Awaiting executor', system)
}
}
/* Run tests on multiple systems in parallel. */
stage('Run Tests') {
def stages = [:]
for (system in systems) {
def test = null
if (!on_demand) {
test = new Test(this, system, notification)
} else {
test = new OnDemandTest(
this, system, notification,
params.REPO_URL, params.REPO_BRANCH
)
}
stages.put("${system}", {
node("sssd-ci") {
stage("${system}") {
test.run()
}
}
})
}
parallel(stages)
}
stage('Report results') {
notification.notify('SUCCESS', 'All tests succeeded.')
}
} catch (FlowInterruptedException e) {
stage('Report results') {
notification.notify('ERROR', 'Aborted.')
throw e
}
} catch (e) {
stage('Report results') {
notification.notify('ERROR', 'Some tests failed.')
throw e
}
}