This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgolang.groovy
576 lines (530 loc) · 18.2 KB
/
golang.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
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
import com.concur.*;
workflowDoc = '''
title: Golang
overview: Steps for building and testing with Golang.
additional_resources:
- name: Glide
url: https://github.com/Masterminds/glide
- name: Dep
url: https://github.com/golang/dep
- name: Golang
url: https://golang.org
- name: Docker Images
url: https://hub.docker.com/_/golang/
tools:
- type: String
name: buildImage
section: golang
required: true
description: Docker image that has Golang/Glide/Godep installed.
- type: List
name: additionalArgs
section: glide
description: Any additional arguments to Glide as a YAML style List.
- type: String
name: command
section: glide
default: install
description: Which Glide command to run.
- type: List
name: additionalArgs
section: godep
description: Any additional arguments to Godep as a YAML style List.
- type: String
name: command
section: godep
default: restore
description: Which Godep command to run.
- type: String
name: goPath
section: golang
default: determined by SCM
description: The path within the container to mount the project into.
- type: String
name: outFile
section: golang
description: Where the built Go binary will be put instead of the current directory.
- type: Map
name: env
section: golang
description: Setup for the build environment, for example setting GOOS or GOARCH.
- type: List
name: additionalArgs
section: golang
description: Any additional arguments to `go build` as a YAML style List.
- type: String
name: mainPath
section: golang
description: Path to the main .go file to build.
- type: List
name: additionalArgs
section: test
description: Additional arguments to the test binary specified.
- type: String
name: binary
section: test
default: go test
description: The binary to use for the test, in case a different framework is being used.
- type: String
name: resultsPath
section: test
default: test_results
description: If a test framework, such as Gingko, that can output to Junit is being used this is the path to the directory.
- type: Boolean
section: golang
name: gatherJunit
default: false
description: If a test framework, such as Gingko, that can output to Junit this will ensure that the test results are published in Jenkins.
- type: String
name: junitPattern
section: golang
default: ${resultsPath}/*.xml
description: An ant style pattern for the junit plugin, should match where your test results get stored.
full_example: |
pipelines:
tools:
branches:
patterns:
feature: .+
branches:
feature:
steps:
- golang:
- glide:
- test:
binary: ginkgo
gatherJunit: true
- build:
mainPath: cmd/app/main.go
outFile: publish/app
env:
GOOS: darwin
GOARCH: amd64
'''
concurPipeline = new Commands()
concurGit = new Git()
concurUtil = new Util()
/*
description: Vendor Package Management for your Go projects.
parameters:
- type: String
name: buildImage
required: true
description: Docker image that has Glide installed.
- type: List
name: additionalArgs
description: Any additional arguments to Glide as a YAML style List.
- type: String
name: command
default: install
description: Which Glide command to run.
- type: String
name: goPath
default: determined by SCM
description: The path within the container to mount the project into.
example: |
branches:
feature:
steps:
- golang:
# Simple
- glide:
# Advanced
- glide:
command: install
additionalArgs:
- "--force"
*/
public glide(Map yml, Map args) {
String dockerImage = args?.buildImage ?: yml.tools?.golang?.buildImage
List additionalArgs = args?.additionalArgs ?: yml.tools?.glide?.additionalArgs
String command = args?.command ?: yml.tools?.glide?.command ?: "install"
String goPath = args?.goPath ?: yml.tools?.golang?.goPath ?: getGoPath()
assert goPath : "Workflows :: Golang :: glide :: [goPath] is required in [tools.golang] or as a parameter to the test step."
assert dockerImage : "Workflows :: Golang :: glide :: [buildImage] is needed in [tools.golang] or as a parameter to the test step."
dockerImage = concurUtil.mustacheReplaceAll(dockerImage)
def glideCommand = "glide ${command}"
/**
* Define additional args like the following
* ----------------------------------------------
* - golang:
* - glide:
* additionalArgs:
* - "--force"
* - "--skip-test"
* - "-v"
*/
if (additionalArgs) {
glideCommand = "$glideCommand ${additionalArgs.join(' ')}"
}
glideCommand = concurUtil.mustacheReplaceAll(glideCommand)
concurPipeline.debugPrint('Workflows :: golang :: glide', [
'dockerImage' : dockerImage,
'goPath' : goPath,
'command' : command,
'additionalArgs' : additionalArgs,
'glideCommand' : glideCommand
])
// -u 0:0 runs as root, -v mounts the current workspace to your gopath
runCommandInDockerImage(dockerImage, goPath, {
concurUtil.installGoPkg('glide', 'github.com/Masterminds/glide')
sh "cd ${goPath} && ${glideCommand}"
})
}
/*
description: Dep is a tool for managing Go package dependencies.
parameters:
- type: String
name: buildImage
required: true
description: Docker image that has Godep installed.
- type: List
name: additionalArgs
description: Any additional arguments to Godep as a YAML style List.
- type: String
name: command
default: restore
description: Which Godep command to run.
- type: String
name: goPath
default: determined by SCM
description: The path within the container to mount the project into.
example: |
branches:
feature:
steps:
- golang:
# Simple
- dep:
# Advanced
- dep:
additionalArgs:
- "-v"
- "-update"
*/
public dep(Map yml, Map args) {
String dockerImage = args?.buildImage ?: yml.tools?.golang?.buildImage
List additionalArgs = args?.additionalArgs ?: yml.tools?.dep?.additionalArgs
String command = args?.command ?: yml.tools?.dep?.command ?: "ensure"
String goPath = args?.goPath ?: yml.tools?.golang?.goPath ?: getGoPath()
assert goPath : "Workflows :: Golang :: dep :: [goPath] is required in [tools.golang] or as a parameter to the test step."
assert dockerImage : "Workflows :: Golang :: dep :: [buildImage] is needed in [tools.golang] or as a parameter to the test step."
def depCommand = "dep ${command}"
/**
* Define additional args as any of the following
* ----------------------------------------------
* - golang:
* - dep:
* additionalArgs:
* - "--force"
* - "--skip-test"
* - "-v"
*/
if (additionalArgs) {
depCommand = "$depCommand ${additionalArgs.join(' ')}"
}
depCommand = concurUtil.mustacheReplaceAll(depCommand)
concurPipeline.debugPrint('Workflows :: golang :: dep', [
'dockerImage' : dockerImage,
'goPath' : goPath,
'command' : command,
'additionalArgs' : additionalArgs,
'depCommand' : depCommand
])
runCommandInDockerImage(dockerImage, goPath, {
concurUtil.installGoPkg('dep', 'github.com/golang/dep/cmd/dep')
sh "cd ${goPath} && ${depCommand}"
})
}
/*
description: Build a Golang project.
parameters:
- type: String
name: buildImage
description: Docker image that has the linting tool installed.
- type: List
name: additionalFlags
description: Any additional arguments to the linting tool as a YAML style List.
- type: List
name: enable
description: A list of linters to enable.
default: []
- type: String
name: binary
description: The binary you want to use for linting.
default: gometalinter
- type: String
name: goPath
description: The path within the container to mount the project into.
default: getGoPath()
example: |
branches:
feature:
steps:
- golang:
# Simple
- lint:
# Advanced
- lint:
binary: gometalinter.v1
enable:
- vet
- deadcode
- goconst
- errcheck
- goimports
additionalFlags:
- tests
*/
public lint(Map yml, Map args) {
String dockerImage = args?.buildImage ?: yml.tools?.golang?.buildImage
List additionalFlags = args?.additionalFlags ?: yml.tools?.golang?.lint?.additionalFlags
List enable = args?.enable ?: yml.tools?.golang?.lint?.enable ?: []
String binary = args?.binary ?: yml.tools?.golang?.lint?.binary ?: 'gometalinter'
String installer = args?.installer ?: yml.tools?.golang?.lint?.installer ?: 'github.com/alecthomas/gometalinter'
String goPath = args?.goPath ?: yml.tools?.golang?.goPath ?: getGoPath()
String lintCommand = binary
if (enable) {
lintCommand = "$lintCommand --disable-all ${enable.collect { "--enable=$it" }.join(' ')}"
}
if (additionalFlags) {
lintCommand = "$lintCommand ${additionalFlags.collect { "--$it" }.join(' ')}"
}
concurPipeline.debugPrint('Workflows :: Golang :: Lint', [
'dockerImage' : dockerImage,
'additionalFlags' : additionalFlags,
'enable' : enable,
'binary' : binary,
'installer' : installer,
'goPath' : goPath
])
runCommandInDockerImage(dockerImage, goPath, {
concurUtil.installGoPkg(binary, installer)
try {
sh "$binary --install"
} catch (e) { error("Failed to install linters for $binary [$e]") }
if (additionalFlags.find { it == 'checkstyle' }) {
def lintResults = sh returnStdout: true, script: "cd ${goPath} && ${lintCommand}"
writeFile file: 'checkstyle.xml', text: lintResults
println 'Wrote checkstyle.xml file.'
if (concurPipeline.getPluginVersion('checkstyle')) {
println 'Checkstyle plugin installed, calling plugin.'
checkstyle canComputeNew: false, canRunOnFailed: true, defaultEncoding: '', healthy: '', pattern: 'checkstyle.xml', unHealthy: ''
}
} else {
sh "cd ${goPath} && ${lintCommand}"
}
})
}
/*
description: Build a Golang project.
parameters:
- type: String
name: buildImage
required: true
description: Docker image that has any Golang installed.
- type: String
name: goPath
default: determined by SCM
description: The path within the container to mount the project into.
- type: String
name: outFile
section: golang
description: Where the built Go binary will be put instead of the current directory.
- type: Map
name: env
section: golang
description: Setup for the build environment, for example setting GOOS or GOARCH.
- type: String
name: mainPath
section: golang
description: Path to the main .go file to build.
example: |
branches:
feature:
steps:
- golang:
# Simple
- build:
# Advanced
- build:
outFile: "publish/example-binary"
mainPath: "cmd/app/main.go"
env:
GOOS: linux
GOARCH: amd64
*/
public build(Map yml, Map args) {
String dockerImage = args?.buildImage ?: yml.tools?.golang?.buildImage
String outFile = args?.outFile ?: yml.tools?.golang?.outFile
Map goEnv = args?.env ?: yml.tools?.golang?.env
String mainPath = args?.mainPath ?: yml.tools?.golang?.mainPath
List additionalArgs = args?.additionalArgs ?: yml.tools?.golang?.additionalArgs
String goPath = args?.goPath ?: yml.tools?.golang?.goPath ?: getGoPath()
assert goPath : "Workflows :: Golang :: build :: [goPath] is required in [tools.golang] or as a parameter to the test step."
assert dockerImage : "Workflows :: Golang :: build :: [buildImage] is needed in [tools.golang] or as a parameter to the test step."
def goCommand = "go build"
if (goEnv) {
def envStr = goEnv.collect { "${it.key}=${it.value}" }.join(' ')
goCommand = "${envStr} ${goCommand}"
}
if (outFile) {
goCommand = "${goCommand} -o ${outFile}"
}
/**
* Define additional args as any of the following
* ----------------------------------------------
* - golang:
* - build:
* additionalArgs:
* - "-a"
* - "-n"
* - "-p 5"
* ----------------------------------------------
*/
if (additionalArgs) {
goCommand = "${goCommand} ${additionalArgs.join(' ')}"
}
if (mainPath) {
goCommand = "${goCommand} ${mainPath}"
}
goCommand = concurUtil.mustacheReplaceAll(goCommand)
concurPipeline.debugPrint('Workflow :: golang :: build', [
'dockerImage' : dockerImage,
'goPath' : goPath,
'outFile' : outFile,
'goEnv' : goEnv,
'additionalArgs': additionalArgs,
'goCommand' : goCommand
])
runCommandInDockerImage(dockerImage, goPath, {
sh "cd ${goPath} && ${goCommand}"
})
}
/*
description: Build a Golang project.
parameters:
- type: String
name: buildImage
required: true
description: Docker image that has any Golang installed.
- type: String
name: goPath
default: determined by SCM
description: The path within the container to mount the project into.
- type: List
name: additionalArgs
description: Any additional arguments to Glide as a YAML style List.
- type: String
name: binary
default: go test
description: The binary to use for the test, in case a different framework is being used.
- type: String
name: resultsPath
default: test_results
description: If a test framework, such as Gingko, that can output to Junit is being used this is the path to the directory.
- type: Boolean
name: gatherJunit
default: false
description: If a test framework, such as Gingko, that can output to Junit this will ensure that the test results are published in Jenkins.
- type: String
name: junitPattern
default: ${resultsPath}/*.xml
description: An ant style pattern for the junit plugin, should match where your test results get stored.
example: |
branches:
feature:
steps:
- golang:
# Simple
- test:
# Advanced
- test:
binary: ginkgo
additionalArgs:
- "./..."
gatherJunit: true
resultsPath: results
*/
public test(Map yml, Map args) {
String dockerImage = args?.buildImage ?: yml.tools?.golang?.buildImage
List additionalArgs = args?.additionalArgs ?: yml.tools?.golang?.test?.additionalArgs
String goPath = args?.goPath ?: yml.tools?.golang?.goPath ?: getGoPath()
String testBinary = args?.binary ?: yml.tools?.golang?.test?.binary ?: "go test"
String resultsPath = args?.resultsPath ?: yml.tools?.golang?.test?.resultsPath ?: "test_results"
Boolean gatherJunit = args?.gatherJunit ?: yml.tools?.golang?.gatherJunit ?: false
String junitPattern = args?.junitPattern ?: yml.tools?.golang?.junitPattern ?: "${resultsPath}/*.xml"
assert dockerImage : "Workflows :: Golang :: [buildImage] is needed in [tools.golang] or as a parameter to the test step."
assert goPath : "Workflows :: Golang :: [goPath] is required in [tools.golang] or as a parameter to the test step."
assert junitPattern : "Workflows :: Golang :: [junitPattern] is required in [tools.golang] or as a parameter to the test step."
String testCommand = testBinary
/**
* Define additional args as any of the following
* ----------------------------------------------
* - golang:
* - test:
* additionalArgs:
* - "-a"
* - "-n"
* - "-p 5"
*/
if (additionalArgs) {
testCommand = "${testCommand} ${additionalArgs.join(' ')}"
}
def shCmd = "cd ${goPath} && mkdir -p ${goPath}/${resultsPath} && ${testCommand}"
shCmd = concurUtil.mustacheReplaceAll(shCmd)
concurPipeline.debugPrint('Workflows :: golang :: test', [
'dockerImage' : dockerImage,
'goPath' : goPath,
'testBinary' : testBinary,
'additionalArgs': additionalArgs,
'resultsPath' : resultsPath,
'gatherJunit' : gatherJunit,
'junitPattern' : junitPattern,
'testCommand' : testCommand,
'shCmd' : shCmd
])
runCommandInDockerImage(dockerImage, goPath, {
sh shCmd
})
if (gatherJunit) {
junit junitPattern
}
}
private getGoPath() {
return "/go/src/${GIT_HOST}/${env.GIT_OWNER}/${env.GIT_REPO}"
}
private runCommandInDockerImage(String dockerImage, String goPath, Closure work) {
dockerImage = concurUtil.mustacheReplaceAll(dockerImage)
docker.image(dockerImage).inside("-u 0:0 -v ${pwd()}:${goPath}") {
work()
}
}
/*
* Set the name of the stage dynamically.
*/
public getStageName(Map yml, Map args, String stepName) {
switch(stepName) {
case 'glide':
String glideCommand = args?.command ?: "install"
return "golang: glide: $glideCommand"
case 'godep':
String godepCommand = args?.command ?: "ensure"
return "golang: godep: $godepCommand"
case 'lint':
String binary = args?.binary ?: yml.tools?.lint?.binary ?: "gometalinter"
return "golang: $binary"
case 'build':
String os = args?.env?.GOOS ?: yml.tools?.golang?.env?.GOOS
String arch = args?.env?.GOARCH ?: yml.tools?.golang?.env?.GOARCH
return os ? arch ? "golang: build: $arch/$os" : "golang: build: $os" : 'golang: build'
case 'test':
String testCommand = args?.additionalArgs.join(' ')
return testCommand ? "golang: test: ${testCommand}" : 'golang: test'
}
}
public tests(Map yml, Map args) {
String workflowName = 'golang'
println "Testing $workflowName"
}
return this;