forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolangBuild.go
572 lines (465 loc) · 17.4 KB
/
golangBuild.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
package cmd
import (
"bytes"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/SAP/jenkins-library/pkg/buildsettings"
"github.com/SAP/jenkins-library/pkg/certutils"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/goget"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperenv"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/multiarch"
"github.com/SAP/jenkins-library/pkg/versioning"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)
const (
coverageFile = "cover.out"
golangUnitTestOutput = "TEST-go.xml"
golangIntegrationTestOutput = "TEST-integration.xml"
golangCoberturaPackage = "github.com/boumenot/gocover-cobertura@latest"
golangTestsumPackage = "gotest.tools/gotestsum@latest"
golangCycloneDXPackage = "github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@latest"
sbomFilename = "bom.xml"
)
type golangBuildUtils interface {
command.ExecRunner
goget.Client
piperutils.FileUtils
piperhttp.Uploader
DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error
getDockerImageValue(stepName string) (string, error)
// Add more methods here, or embed additional interfaces, or remove/replace as required.
// The golangBuildUtils interface should be descriptive of your runtime dependencies,
// i.e. include everything you need to be able to mock in tests.
// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
}
type golangBuildUtilsBundle struct {
*command.Command
*piperutils.Files
piperhttp.Uploader
goget.Client
// Embed more structs as necessary to implement methods or interfaces you add to golangBuildUtils.
// Structs embedded in this way must each have a unique set of methods attached.
// If there is no struct which implements the method you need, attach the method to
// golangBuildUtilsBundle and forward to the implementation of the dependency.
}
func (g *golangBuildUtilsBundle) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
return fmt.Errorf("not implemented")
}
func (g *golangBuildUtilsBundle) getDockerImageValue(stepName string) (string, error) {
return GetDockerImageValue(stepName)
}
func newGolangBuildUtils(config golangBuildOptions) golangBuildUtils {
httpClientOptions := piperhttp.ClientOptions{}
if len(config.CustomTLSCertificateLinks) > 0 {
httpClientOptions.TransportSkipVerification = false
httpClientOptions.TrustedCerts = config.CustomTLSCertificateLinks
}
httpClient := piperhttp.Client{}
httpClient.SetOptions(httpClientOptions)
utils := golangBuildUtilsBundle{
Command: &command.Command{},
Files: &piperutils.Files{},
Uploader: &httpClient,
Client: &goget.ClientImpl{
HTTPClient: &httpClient,
},
}
// Reroute command output to logging framework
utils.Stdout(log.Writer())
utils.Stderr(log.Writer())
return &utils
}
func golangBuild(config golangBuildOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *golangBuildCommonPipelineEnvironment) {
// Utils can be used wherever the command.ExecRunner interface is expected.
// It can also be used for example as a mavenExecRunner.
utils := newGolangBuildUtils(config)
// Error situations will be bubbled up until they reach the line below which will then stop execution
// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
err := runGolangBuild(&config, telemetryData, utils, commonPipelineEnvironment)
if err != nil {
log.Entry().WithError(err).Fatal("execution of golang build failed")
}
}
func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomData, utils golangBuildUtils, commonPipelineEnvironment *golangBuildCommonPipelineEnvironment) error {
goModFile, err := readGoModFile(utils) // returns nil if go.mod doesnt exist
if err != nil {
return err
}
if err = prepareGolangEnvironment(config, goModFile, utils); err != nil {
return err
}
// install test pre-requisites only in case testing should be performed
if config.RunTests || config.RunIntegrationTests {
if err := utils.RunExecutable("go", "install", golangTestsumPackage); err != nil {
return fmt.Errorf("failed to install pre-requisite: %w", err)
}
}
if config.CreateBOM {
if err := utils.RunExecutable("go", "install", golangCycloneDXPackage); err != nil {
return fmt.Errorf("failed to install pre-requisite: %w", err)
}
}
failedTests := false
if config.RunTests {
success, err := runGolangTests(config, utils)
if err != nil {
return err
}
failedTests = !success
}
if config.RunTests && config.ReportCoverage {
if err := reportGolangTestCoverage(config, utils); err != nil {
return err
}
}
if config.RunIntegrationTests {
success, err := runGolangIntegrationTests(config, utils)
if err != nil {
return err
}
failedTests = failedTests || !success
}
if failedTests {
log.SetErrorCategory(log.ErrorTest)
return fmt.Errorf("some tests failed")
}
if config.CreateBOM {
if err := runBOMCreation(utils, sbomFilename); err != nil {
return err
}
}
ldflags := ""
if len(config.LdflagsTemplate) > 0 {
var err error
ldflags, err = prepareLdflags(config, utils, GeneralConfig.EnvRootPath)
if err != nil {
return err
}
log.Entry().Infof("ldflags from template: '%v'", ldflags)
}
var binaries []string
platforms, err := multiarch.ParsePlatformStrings(config.TargetArchitectures)
if err != nil {
return err
}
for _, platform := range platforms {
binaryNames, err := runGolangBuildPerArchitecture(config, utils, ldflags, platform)
if err != nil {
return err
}
if len(binaryNames) > 0 {
binaries = append(binaries, binaryNames...)
}
}
log.Entry().Debugf("creating build settings information...")
stepName := "golangBuild"
dockerImage, err := utils.getDockerImageValue(stepName)
if err != nil {
return err
}
buildConfig := buildsettings.BuildOptions{
CreateBOM: config.CreateBOM,
Publish: config.Publish,
BuildSettingsInfo: config.BuildSettingsInfo,
DockerImage: dockerImage,
}
buildSettingsInfo, err := buildsettings.CreateBuildSettingsInfo(&buildConfig, stepName)
if err != nil {
log.Entry().Warnf("failed to create build settings info: %v", err)
}
commonPipelineEnvironment.custom.buildSettingsInfo = buildSettingsInfo
if config.Publish {
if len(config.TargetRepositoryURL) == 0 {
return fmt.Errorf("there's no target repository for binary publishing configured")
}
artifactVersion := config.ArtifactVersion
if len(artifactVersion) == 0 {
artifactOpts := versioning.Options{
VersioningScheme: "library",
}
artifact, err := versioning.GetArtifact("golang", "", &artifactOpts, utils)
if err != nil {
return err
}
artifactVersion, err = artifact.GetVersion()
if err != nil {
return err
}
}
if goModFile == nil {
return fmt.Errorf("go.mod file not found")
} else if goModFile.Module == nil {
return fmt.Errorf("go.mod doesn't declare a module path")
}
repoClientOptions := piperhttp.ClientOptions{
Username: config.TargetRepositoryUser,
Password: config.TargetRepositoryPassword,
TrustedCerts: config.CustomTLSCertificateLinks,
}
utils.SetOptions(repoClientOptions)
var binaryArtifacts piperenv.Artifacts
for _, binary := range binaries {
targetPath := fmt.Sprintf("go/%s/%s/%s", goModFile.Module.Mod.Path, config.ArtifactVersion, binary)
separator := "/"
if strings.HasSuffix(config.TargetRepositoryURL, "/") {
separator = ""
}
targetURL := fmt.Sprintf("%s%s%s", config.TargetRepositoryURL, separator, targetPath)
log.Entry().Infof("publishing artifact: %s", targetURL)
response, err := utils.UploadRequest(http.MethodPut, targetURL, binary, "", nil, nil, "binary")
if err != nil {
return fmt.Errorf("couldn't upload artifact: %w", err)
}
if !(response.StatusCode == 200 || response.StatusCode == 201) {
return fmt.Errorf("couldn't upload artifact, received status code %d", response.StatusCode)
}
binaryArtifacts = append(binaryArtifacts, piperenv.Artifact{
Name: binary,
})
}
commonPipelineEnvironment.custom.artifacts = binaryArtifacts
}
return nil
}
func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.File, utils golangBuildUtils) error {
// configure truststore
err := certutils.CertificateUpdate(config.CustomTLSCertificateLinks, utils, utils, "/etc/ssl/certs/ca-certificates.crt") // TODO reimplement
if config.PrivateModules == "" {
return nil
}
if config.PrivateModulesGitToken == "" {
return fmt.Errorf("please specify a token for fetching private git modules")
}
// pass private repos to go process
os.Setenv("GOPRIVATE", config.PrivateModules)
repoURLs, err := lookupGolangPrivateModulesRepositories(goModFile, config.PrivateModules, utils)
if err != nil {
return err
}
// configure credentials git shall use for pulling repos
for _, repoURL := range repoURLs {
if match, _ := regexp.MatchString("(?i)^https?://", repoURL); !match {
continue
}
authenticatedRepoURL := strings.Replace(repoURL, "://", fmt.Sprintf("://%s@", config.PrivateModulesGitToken), 1)
err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), fmt.Sprintf("%s", repoURL))
if err != nil {
return err
}
}
return nil
}
func runGolangTests(config *golangBuildOptions, utils golangBuildUtils) (bool, error) {
// execute gotestsum in order to have more output options
if err := utils.RunExecutable("gotestsum", "--junitfile", golangUnitTestOutput, "--", fmt.Sprintf("-coverprofile=%v", coverageFile), "./..."); err != nil {
exists, fileErr := utils.FileExists(golangUnitTestOutput)
if !exists || fileErr != nil {
log.SetErrorCategory(log.ErrorBuild)
return false, fmt.Errorf("running tests failed - junit result missing: %w", err)
}
exists, fileErr = utils.FileExists(coverageFile)
if !exists || fileErr != nil {
log.SetErrorCategory(log.ErrorBuild)
return false, fmt.Errorf("running tests failed - coverage output missing: %w", err)
}
return false, nil
}
return true, nil
}
func runGolangIntegrationTests(config *golangBuildOptions, utils golangBuildUtils) (bool, error) {
// execute gotestsum in order to have more output options
// for integration tests coverage data is not meaningful and thus not being created
if err := utils.RunExecutable("gotestsum", "--junitfile", golangIntegrationTestOutput, "--", "-tags=integration", "./..."); err != nil {
exists, fileErr := utils.FileExists(golangIntegrationTestOutput)
if !exists || fileErr != nil {
log.SetErrorCategory(log.ErrorBuild)
return false, fmt.Errorf("running tests failed: %w", err)
}
return false, nil
}
return true, nil
}
func reportGolangTestCoverage(config *golangBuildOptions, utils golangBuildUtils) error {
if config.CoverageFormat == "cobertura" {
// execute gocover-cobertura in order to create cobertura report
// install pre-requisites
if err := utils.RunExecutable("go", "install", golangCoberturaPackage); err != nil {
return fmt.Errorf("failed to install pre-requisite: %w", err)
}
coverageData, err := utils.FileRead(coverageFile)
if err != nil {
return fmt.Errorf("failed to read coverage file %v: %w", coverageFile, err)
}
utils.Stdin(bytes.NewBuffer(coverageData))
coverageOutput := bytes.Buffer{}
utils.Stdout(&coverageOutput)
options := []string{}
if config.ExcludeGeneratedFromCoverage {
options = append(options, "-ignore-gen-files")
}
if err := utils.RunExecutable("gocover-cobertura", options...); err != nil {
log.SetErrorCategory(log.ErrorTest)
return fmt.Errorf("failed to convert coverage data to cobertura format: %w", err)
}
utils.Stdout(log.Writer())
err = utils.FileWrite("cobertura-coverage.xml", coverageOutput.Bytes(), 0666)
if err != nil {
return fmt.Errorf("failed to create cobertura coverage file: %w", err)
}
log.Entry().Info("created file cobertura-coverage.xml")
} else {
// currently only cobertura and html format supported, thus using html as fallback
if err := utils.RunExecutable("go", "tool", "cover", "-html", coverageFile, "-o", "coverage.html"); err != nil {
return fmt.Errorf("failed to create html coverage file: %w", err)
}
}
return nil
}
func prepareLdflags(config *golangBuildOptions, utils golangBuildUtils, envRootPath string) (string, error) {
cpe := piperenv.CPEMap{}
err := cpe.LoadFromDisk(path.Join(envRootPath, "commonPipelineEnvironment"))
if err != nil {
log.Entry().Warning("failed to load values from commonPipelineEnvironment")
}
log.Entry().Debugf("ldflagsTemplate in use: %v", config.LdflagsTemplate)
tmpl, err := template.New("ldflags").Parse(config.LdflagsTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse ldflagsTemplate '%v': %w", config.LdflagsTemplate, err)
}
ldflagsParams := struct {
CPE map[string]interface{}
}{
CPE: map[string]interface{}(cpe),
}
var generatedLdflags bytes.Buffer
err = tmpl.Execute(&generatedLdflags, ldflagsParams)
if err != nil {
return "", fmt.Errorf("failed to execute ldflagsTemplate '%v': %w", config.LdflagsTemplate, err)
}
return generatedLdflags.String(), nil
}
func runGolangBuildPerArchitecture(config *golangBuildOptions, utils golangBuildUtils, ldflags string, architecture multiarch.Platform) ([]string, error) {
var binaryNames []string
envVars := os.Environ()
envVars = append(envVars, fmt.Sprintf("GOOS=%v", architecture.OS), fmt.Sprintf("GOARCH=%v", architecture.Arch))
if !config.CgoEnabled {
envVars = append(envVars, "CGO_ENABLED=0")
}
utils.SetEnv(envVars)
buildOptions := []string{"build", "-trimpath"}
if len(config.Output) > 0 {
if len(config.Packages) > 1 {
binaries, outputDir, err := getOutputBinaries(config.Output, config.Packages, utils, architecture)
if err != nil {
log.SetErrorCategory(log.ErrorBuild)
return nil, fmt.Errorf("failed to calculate output binaries or directory, error: %s", err.Error())
}
buildOptions = append(buildOptions, "-o", outputDir)
binaryNames = append(binaryNames, binaries...)
} else {
fileExtension := ""
if architecture.OS == "windows" {
fileExtension = ".exe"
}
binaryName := fmt.Sprintf("%s-%s.%s%s", strings.TrimRight(config.Output, string(os.PathSeparator)), architecture.OS, architecture.Arch, fileExtension)
buildOptions = append(buildOptions, "-o", binaryName)
binaryNames = append(binaryNames, binaryName)
}
}
buildOptions = append(buildOptions, config.BuildFlags...)
if len(ldflags) > 0 {
buildOptions = append(buildOptions, "-ldflags", ldflags)
}
buildOptions = append(buildOptions, config.Packages...)
if err := utils.RunExecutable("go", buildOptions...); err != nil {
log.Entry().Debugf("buildOptions: %v", buildOptions)
log.SetErrorCategory(log.ErrorBuild)
return nil, fmt.Errorf("failed to run build for %v.%v: %w", architecture.OS, architecture.Arch, err)
}
return binaryNames, nil
}
// lookupPrivateModulesRepositories returns a slice of all modules that match the given glob pattern
func lookupGolangPrivateModulesRepositories(goModFile *modfile.File, globPattern string, utils golangBuildUtils) ([]string, error) {
if globPattern == "" {
return []string{}, nil
}
if goModFile == nil {
return nil, fmt.Errorf("couldn't find go.mod file")
} else if goModFile.Require == nil {
return []string{}, nil // no modules referenced, nothing to do
}
privateModules := []string{}
for _, goModule := range goModFile.Require {
if !module.MatchPrefixPatterns(globPattern, goModule.Mod.Path) {
continue
}
repo, err := utils.GetRepositoryURL(goModule.Mod.Path)
if err != nil {
return nil, err
}
privateModules = append(privateModules, repo)
}
return privateModules, nil
}
func runBOMCreation(utils golangBuildUtils, outputFilename string) error {
if err := utils.RunExecutable("cyclonedx-gomod", "mod", "-licenses", "-test", "-output", outputFilename); err != nil {
return fmt.Errorf("BOM creation failed: %w", err)
}
return nil
}
func readGoModFile(utils golangBuildUtils) (*modfile.File, error) {
modFilePath := "go.mod"
if modFileExists, err := utils.FileExists(modFilePath); err != nil {
return nil, err
} else if !modFileExists {
return nil, nil
}
modFileContent, err := utils.FileRead(modFilePath)
if err != nil {
return nil, err
}
return modfile.Parse(modFilePath, modFileContent, nil)
}
func getOutputBinaries(out string, packages []string, utils golangBuildUtils, architecture multiarch.Platform) ([]string, string, error) {
var binaries []string
outDir := fmt.Sprintf("%s-%s-%s%c", strings.TrimRight(out, string(os.PathSeparator)), architecture.OS, architecture.Arch, os.PathSeparator)
for _, pkg := range packages {
ok, err := isMainPackage(utils, pkg)
if err != nil {
return nil, "", err
}
if ok {
fileExt := ""
if architecture.OS == "windows" {
fileExt = ".exe"
}
binaries = append(binaries, filepath.Join(outDir, filepath.Base(pkg)+fileExt))
}
}
return binaries, outDir, nil
}
func isMainPackage(utils golangBuildUtils, pkg string) (bool, error) {
outBuffer := bytes.NewBufferString("")
utils.Stdout(outBuffer)
utils.Stderr(outBuffer)
err := utils.RunExecutable("go", "list", "-f", "{{ .Name }}", pkg)
if err != nil {
return false, err
}
if outBuffer.String() != "main" {
return false, nil
}
return true, nil
}