-
Notifications
You must be signed in to change notification settings - Fork 15
/
menderqa_pipeline.go
661 lines (572 loc) · 18 KB
/
menderqa_pipeline.go
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package main
import (
"bytes"
"context"
"reflect"
"sort"
"strconv"
"strings"
"text/template"
"github.com/google/go-github/v28/github"
"github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
clientgitlab "github.com/mendersoftware/integration-test-runner/client/gitlab"
)
const LatestStableYoctoBranch = "kirkstone"
func say(
ctx context.Context,
tmplString string,
data interface{},
log *logrus.Entry,
conf *config,
pr *github.PullRequestEvent,
) error {
tmpl, err := template.New("Main").Parse(tmplString)
if err != nil {
log.Errorf(
"Failed to parse the build matrix template. Should never happen! Error: %s\n",
err.Error(),
)
}
var buf bytes.Buffer
if err = tmpl.Execute(&buf, data); err != nil {
log.Errorf("Failed to execute the build matrix template. Error: %s\n", err.Error())
}
// Comment with a pipeline-link on the PR
commentBody := buf.String()
comment := github.IssueComment{
Body: &commentBody,
}
err = githubClient.CreateComment(ctx,
conf.githubOrganization, pr.GetRepo().GetName(), pr.GetNumber(), &comment)
if err != nil {
log.Infof("Failed to comment on the pr: %v, Error: %s", pr, err.Error())
}
return err
}
func parsePullRequest(
log *logrus.Entry,
conf *config,
action string,
pr *github.PullRequestEvent,
) []buildOptions {
log.Info("Pull request event with action: ", action)
var builds []buildOptions
// github pull request events to trigger a CI job for
if action == "opened" || action == "edited" || action == "reopened" ||
action == "synchronize" || action == "ready_for_review" {
return getBuilds(log, conf, pr)
}
return builds
}
func getBuilds(log *logrus.Entry, conf *config, pr *github.PullRequestEvent) []buildOptions {
var builds []buildOptions
repo := pr.GetRepo().GetName()
commitSHA := pr.PullRequest.Head.GetSHA()
//GetLabel returns "mendersoftware:master", we just want the branch
baseBranch := strings.Split(pr.PullRequest.Base.GetLabel(), ":")[1]
makeQEMU := false
// we need to have the latest integration/master branch in order to use the release_tool.py
if err := updateIntegrationRepo(conf); err != nil {
log.Warnf(err.Error())
}
allRepositories, err := getListOfAllRepositories(conf)
if err != nil {
log.Warnf(err.Error())
}
for _, watchRepo := range allRepositories {
// make sure the repo that the pull request is performed against is
// one that we are watching.
if watchRepo == repo {
// check if we need to build/test yocto
for _, qemuRepo := range qemuBuildRepositories {
if repo == qemuRepo {
makeQEMU = true
}
}
switch repo {
case "meta-mender", "integration":
build := buildOptions{
pr: strconv.Itoa(pr.GetNumber()),
repo: repo,
baseBranch: baseBranch,
commitSHA: commitSHA,
makeQEMU: makeQEMU,
}
builds = append(builds, build)
default:
var err error
var integrationsToTest []string
if integrationsToTest, err = getIntegrationVersionsUsingMicroservice(
log,
repo,
baseBranch,
conf,
); err != nil {
log.Errorf(
"failed to get related microservices for repo: %s version: %s, failed "+
"with: %s\n",
repo,
baseBranch,
err.Error(),
)
return nil
}
log.Infof(
"the following integration branches: %s are using %s/%s",
integrationsToTest,
repo,
baseBranch,
)
// one pull request can trigger multiple builds
for _, integrationBranch := range integrationsToTest {
buildOpts := buildOptions{
pr: strconv.Itoa(pr.GetNumber()),
repo: repo,
baseBranch: integrationBranch,
commitSHA: commitSHA,
makeQEMU: makeQEMU,
}
builds = append(builds, buildOpts)
}
}
}
}
return builds
}
func filterOutEmptyVariables(
optionsIn []*gitlab.PipelineVariableOptions,
) []*gitlab.PipelineVariableOptions {
var optionsOut []*gitlab.PipelineVariableOptions
for _, option := range optionsIn {
if *option.Value != "" {
optionsOut = append(optionsOut, option)
}
}
return optionsOut
}
func isLegacyBuild(build *buildOptions, buildParameters []*gitlab.PipelineVariableOptions) bool {
// We define a legacy build as either:
// * A PR in integration repo with baseBranch 3.7.x
// * A PR in any repo for which INTEGRATION_REV variable is set to 3.7.x
if build.repo == "integration" {
return build.baseBranch == "3.7.x"
}
for _, param := range buildParameters {
if *param.Key == "INTEGRATION_REV" {
return *param.Value == "3.7.x"
}
}
// This should never happen, INTEGRATION_REV must be found! But just in case...
return false
}
func getMenderQARef(build *buildOptions, buildParameters []*gitlab.PipelineVariableOptions) string {
if isLegacyBuild(build, buildParameters) {
return "legacy-mender-3.7-lts"
}
return "master"
}
func triggerBuild(
log *logrus.Entry,
conf *config,
build *buildOptions,
pr *github.PullRequestEvent,
buildOptions *BuildOptions,
) error {
gitlabClient, err := clientgitlab.NewGitLabClient(
conf.gitlabToken,
conf.gitlabBaseURL,
conf.dryRunMode,
)
if err != nil {
return err
}
buildParameters, err := getBuildParameters(log, conf, build, buildOptions)
if err != nil {
return err
}
// first stop old pipelines with the same buildParameters
stopStalePipelines(log, gitlabClient, buildParameters)
// trigger the new pipeline
integrationPipelinePath := "Northern.tech/Mender/mender-qa"
ref := getMenderQARef(build, buildParameters)
opt := &gitlab.CreatePipelineOptions{
Ref: &ref,
Variables: &buildParameters,
}
variablesString := ""
for _, variable := range *opt.Variables {
variablesString += *variable.Key + ":" + *variable.Value + ", "
}
log.Infof(
"Creating pipeline in project %s:%s with variables: %s",
integrationPipelinePath,
*opt.Ref,
variablesString,
)
pipeline, err := gitlabClient.CreatePipeline(integrationPipelinePath, opt)
if err != nil {
log.Errorf("Could not create pipeline: %s", err.Error())
return err
}
log.Infof("Created pipeline: %s", pipeline.WebURL)
// Add the build variable matrix to the pipeline comment under a
// drop-down tab
// nolint:lll
tmplString := `
Hello :smiley_cat: I created a pipeline for you here: [Pipeline-{{.Pipeline.ID}}]({{.Pipeline.WebURL}})
<details>
<summary>Build Configuration Matrix</summary><p>
| Key | Value |
| ----- | ----- |
{{range $i, $var := .BuildVars}}{{if $var.Value}}| {{$var.Key}} | {{$var.Value}} |{{printf "\n"}}{{end}}{{end}}
</p></details>
`
tmpl, err := template.New("Main").Parse(tmplString)
if err != nil {
log.Errorf(
"Failed to parse the build matrix template. Should never happen! Error: %s\n",
err.Error(),
)
return err
}
var buf bytes.Buffer
if err = tmpl.Execute(&buf, struct {
BuildVars []*gitlab.PipelineVariableOptions
Pipeline *gitlab.Pipeline
}{
BuildVars: filterOutEmptyVariables(*opt.Variables),
Pipeline: pipeline,
}); err != nil {
log.Errorf("Failed to execute the build matrix template. Error: %s\n", err.Error())
return err
}
// Comment with a pipeline-link on the PR
commentBody := buf.String()
comment := github.IssueComment{
Body: &commentBody,
}
err = githubClient.CreateComment(context.Background(),
conf.githubOrganization, pr.GetRepo().GetName(), pr.GetNumber(), &comment)
if err != nil {
log.Infof("Failed to comment on the pr: %v, Error: %s", pr, err.Error())
}
return err
}
func stopStalePipelines(
log *logrus.Entry,
client clientgitlab.Client,
vars []*gitlab.PipelineVariableOptions,
) {
integrationPipelinePath := "Northern.tech/Mender/mender-qa"
sort.SliceStable(vars, func(i, j int) bool {
return *vars[i].Key < *vars[j].Key
})
username := githubBotName
status := gitlab.Pending
opt := &gitlab.ListProjectPipelinesOptions{
Username: &username,
Status: &status,
}
pipelinesPending, err := client.ListProjectPipelines(integrationPipelinePath, opt)
if err != nil {
log.Errorf("stopStalePipelines: Could not list pending pipelines: %s", err.Error())
}
status = gitlab.Running
opt = &gitlab.ListProjectPipelinesOptions{
Username: &username,
Status: &status,
}
pipelinesRunning, err := client.ListProjectPipelines(integrationPipelinePath, opt)
if err != nil {
log.Errorf("stopStalePipelines: Could not list running pipelines: %s", err.Error())
}
for _, pipeline := range append(pipelinesPending, pipelinesRunning...) {
variables, err := client.GetPipelineVariables(integrationPipelinePath, pipeline.ID)
if err != nil {
log.Errorf("stopStalePipelines: Could not get variables for pipeline: %s", err.Error())
continue
}
sort.SliceStable(variables, func(i, j int) bool {
return variables[i].Key < variables[j].Key
})
if reflect.DeepEqual(vars, variables) {
log.Infof("Cancelling stale pipeline %d, url: %s", pipeline.ID, pipeline.WebURL)
err := client.CancelPipelineBuild(integrationPipelinePath, pipeline.ID)
if err != nil {
log.Errorf("stopStalePipelines: Could not cancel pipeline: %s", err.Error())
}
}
}
}
func getBuildParameters(
log *logrus.Entry,
conf *config,
build *buildOptions,
buildOptions *BuildOptions,
) ([]*gitlab.PipelineVariableOptions, error) {
var err error
readHead := "pull/" + build.pr + "/head"
var buildParameters []*gitlab.PipelineVariableOptions
var versionedRepositories []string
if build.repo == "meta-mender" {
// For meta-mender, pick master versions of all Mender release repos.
versionedRepositories, err = getListOfVersionedRepositories("origin/master", conf)
} else {
versionedRepositories, err = getListOfVersionedRepositories(
"origin/"+build.baseBranch,
conf,
)
}
if err != nil {
log.Errorf("Could not get list of repositories: %s", err.Error())
return nil, err
}
for _, versionedRepo := range versionedRepositories {
// iterate over all the repositories (except the one we are testing) and
// set the correct microservice versions
// use the default "master" for both mender-qa, and meta-mender (set in CI)
if versionedRepo != build.repo &&
versionedRepo != "integration" &&
build.repo != "meta-mender" {
repoKey := repoToBuildParameter(versionedRepo)
if _, exists := buildOptions.PullRequests[versionedRepo]; exists {
prVersion := buildOptions.PullRequests[versionedRepo]
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{
Key: &repoKey,
Value: &prVersion,
},
)
continue
}
version, err := getServiceRevisionFromIntegration(
versionedRepo,
"origin/"+build.baseBranch,
conf,
)
if err != nil {
log.Errorf("failed to determine %s version: %s", versionedRepo, err.Error())
return nil, err
}
log.Infof("%s version %s is being used in %s", versionedRepo, version, build.baseBranch)
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{
Key: &repoKey,
Value: &version,
},
)
}
}
// set the correct integration branches if we aren't performing a pull request against
// integration
if build.repo != "integration" && build.repo != "meta-mender" {
integrationRevision := build.baseBranch
if _, exists := buildOptions.PullRequests["integration"]; exists {
integrationRevision = buildOptions.PullRequests["integration"]
}
integrationRepoKey := repoToBuildParameter("integration")
buildParameters = append(buildParameters,
&gitlab.PipelineVariableOptions{
Key: &integrationRepoKey,
Value: &integrationRevision})
if _, exists := buildOptions.PullRequests["meta-mender"]; exists {
metaMenderRevision := buildOptions.PullRequests["meta-mender"]
metaMenderRepoKey := repoToBuildParameter("meta-mender")
buildParameters = append(buildParameters,
&gitlab.PipelineVariableOptions{
Key: &metaMenderRepoKey,
Value: &metaMenderRevision})
}
}
// Set poky (& friends) and meta-mender revisions:
// - If building a master PR, leave everything at defaults, which generally means
// meta-mender/master and poky/LatestStableYoctoBranch.
// - If building meta-mender @ non-master, set poky branches to its baseBranch.
// - If building any other repo @ non-master, set both meta-mender and poky to
// LatestStableYoctoBranch.
if build.baseBranch != "master" {
var pokyBranch string
if build.repo == "meta-mender" {
pokyBranch = build.baseBranch
} else {
pokyBranch = LatestStableYoctoBranch
metaMenderBranch := pokyBranch
metaMenderBranchKey := repoToBuildParameter("meta-mender")
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{
Key: &metaMenderBranchKey,
Value: &metaMenderBranch,
},
)
}
pokyBranchKey := repoToBuildParameter("poky")
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &pokyBranchKey, Value: &pokyBranch},
)
metaOEPokyBranchKey := repoToBuildParameter("meta-openembedded")
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{
Key: &metaOEPokyBranchKey,
Value: &pokyBranch,
},
)
metaRPIPokyBranchKey := repoToBuildParameter("meta-raspberrypi")
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{
Key: &metaRPIPokyBranchKey,
Value: &pokyBranch,
},
)
}
// set the rest of the CI build parameters
runIntegrationTests := "true"
runIntegrationTestsKey := "RUN_INTEGRATION_TESTS"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &runIntegrationTestsKey, Value: &runIntegrationTests},
)
// Backend integration tests, from mender-qa pipeline, are only relevant for legacy builds
if isLegacyBuild(build, buildParameters) {
runBackendIntegrationTests := "true"
if buildOptions.Fast {
runIntegrationTests = "false"
}
runBackendIntegrationTestsKey := "RUN_BACKEND_INTEGRATION_TESTS"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{
Key: &runBackendIntegrationTestsKey, Value: &runBackendIntegrationTests,
},
)
}
buildRepoKey := repoToBuildParameter(build.repo)
buildParameters = append(buildParameters,
&gitlab.PipelineVariableOptions{
Key: &buildRepoKey,
Value: &readHead,
})
return getClientBuildParameters(buildParameters, build)
}
func getClientBuildParameters(
buildParameters []*gitlab.PipelineVariableOptions,
build *buildOptions,
) ([]*gitlab.PipelineVariableOptions, error) {
var qemuParam string
if build.makeQEMU {
qemuParam = "true"
} else {
qemuParam = ""
}
buildX86UefiGrubKey := "BUILD_QEMUX86_64_UEFI_GRUB"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildX86UefiGrubKey, Value: &qemuParam},
)
testX86UefiGrubKey := "TEST_QEMUX86_64_UEFI_GRUB"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &testX86UefiGrubKey, Value: &qemuParam},
)
buildX86BiosGrubKey := "BUILD_QEMUX86_64_BIOS_GRUB"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildX86BiosGrubKey, Value: &qemuParam},
)
testX86BiosGrubKey := "TEST_QEMUX86_64_BIOS_GRUB"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &testX86BiosGrubKey, Value: &qemuParam},
)
buildX86BiosGrubGptKey := "BUILD_QEMUX86_64_BIOS_GRUB_GPT"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildX86BiosGrubGptKey, Value: &qemuParam},
)
testX86BiosGrubGptKey := "TEST_QEMUX86_64_BIOS_GRUB_GPT"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &testX86BiosGrubGptKey, Value: &qemuParam},
)
buildVexpress := "BUILD_VEXPRESS_QEMU"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildVexpress, Value: &qemuParam},
)
testVexpress := "TEST_VEXPRESS_QEMU"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &testVexpress, Value: &qemuParam},
)
buildVexpressFlash := "BUILD_VEXPRESS_QEMU_FLASH"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildVexpressFlash, Value: &qemuParam},
)
testVexpressFlash := "TEST_VEXPRESS_QEMU_FLASH"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &testVexpressFlash, Value: &qemuParam},
)
buildVexpressUbootUefiGrub := "BUILD_VEXPRESS_QEMU_UBOOT_UEFI_GRUB"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildVexpressUbootUefiGrub, Value: &qemuParam},
)
testVexpressUbootUefiGrub := "TEST_VEXPRESS_QEMU_UBOOT_UEFI_GRUB"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &testVexpressUbootUefiGrub, Value: &qemuParam},
)
buildBBBKey := "BUILD_BEAGLEBONEBLACK"
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildBBBKey, Value: &qemuParam},
)
// Set BUILD_CLIENT = false, if target repo not in the qemuBuildRepositories list
buildClientKey := "BUILD_CLIENT"
buildClient := "false"
for _, prebuiltClientRepo := range qemuBuildRepositories {
if build.repo == prebuiltClientRepo {
buildClient = "true"
}
}
buildParameters = append(
buildParameters,
&gitlab.PipelineVariableOptions{Key: &buildClientKey, Value: &buildClient},
)
return buildParameters, nil
}
// stopBuildsOfStalePRs stops any running pipelines on a PR which has been merged.
func stopBuildsOfStalePRs(log *logrus.Entry, pr *github.PullRequestEvent, conf *config) error {
// If the action is "closed" the pull request was merged or just closed,
// stop builds in both cases.
if pr.GetAction() != "closed" {
log.Debugf("stopBuildsOfStalePRs: PR not closed, therefore not stopping it's pipeline")
return nil
}
log.Debug("stopBuildsOfStalePRs: Find any running pipelines and kill mercilessly!")
for _, build := range getBuilds(log, conf, pr) {
gitlabClient, err := clientgitlab.NewGitLabClient(
conf.gitlabToken,
conf.gitlabBaseURL,
conf.dryRunMode,
)
if err != nil {
return err
}
buildParams, err := getBuildParameters(log, conf, &build, NewBuildOptions())
if err != nil {
log.Debug("stopBuildsOfStalePRs: Failed to get the build-parameters for the build")
return err
}
stopStalePipelines(log, gitlabClient, buildParams)
}
return nil
}