Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
go: circleci/go@2.2.4
Use go
elements in your existing workflows and jobs.
Build Go applications and release them on GitHub. Install GoReleaser CLI and add a GitHub Token with write:packages permissions and store as environment variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: '2.1'
orbs:
go: circleci/go@x.y
jobs:
go-build-release:
executor:
name: go/default
tag: 1.20.8
steps:
- go/install-goreleaser
- go/goreleaser-release:
project-path: /path/to/Go/project
publish-release: true
validate-yaml: true
workflows:
main:
jobs:
- go-build-release
Run go test ./... with additional (optional) parameters
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
version: '2.1'
orbs:
go: circleci/go@x.y
jobs:
build:
executor:
name: go/default
tag: '1.16'
steps:
- checkout
- go/with-cache:
steps:
- go/mod-download
- go/test:
build_ldflags: '-X ''main.Version=v1.0.0'''
build_tags: integration,e2e
covermode: atomic
failfast: true
no_output_timeout: 15m
race: true
run: ^TestName$
timeout: 15m
workflows:
main:
jobs:
- build
Speeding up builds with the Go modules cache on CircleCI.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: '2.1'
orbs:
go: circleci/go@x.y
jobs:
build:
executor:
name: go/default
tag: '1.23'
steps:
- checkout
- go/with-cache:
steps:
- go/mod-download
- go/test
workflows:
main:
jobs:
- build
Build Go applications and release them on GitHub. GoReleaser CLI must be installed and a GitHub Token with write:packages permissions is also required.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
github-token | A GitHub Token is with write:packages permissions is required.
| No | GITHUB_TOKEN | string |
project-path | The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| No | $HOME | string |
publish-release | Set to true to publish release to GitHub
| No | false | boolean |
validate-yaml | Set to true to validate .goreleaser.yaml.
| No | false | boolean |
Runs 'go test ./...' but includes extensive parameterization for finer tuning
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
covermode | mode used to count coverage | No | atomic | string |
coverprofile | file to save coverage profile | No | cover-source.out | string |
junitfile | file to save junit format xml file | No | unit-tests.xml | string |
packages | import tests to run, by path glob. | No | ./... | string |
project-path | The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| No | '' | string |
Install Go in a build. Supports Linux/amd64 and macOS/amd64.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
cache | Whether or not to cache the binary. | No | true | boolean |
cache-key | String to use in cache key. Typically overridden when needed to bust cache.
| No | v2 | string |
version | The Go version. | No | 1.16.5 | string |
GoReleaser is a release automation tool for managing Go projects. Install GoReleaser in your build. Golang must be installed.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
version | The GoReleaser version to install. Defaults to latest | No | latest | string |
Load cached Go build cache.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
key | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' | string |
Load cached Go build cache.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
key | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' | string |
Load cached Go modules.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
key | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' | string |
Save Go build cache.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
key | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' | string |
path | Path to cache. | No | /home/circleci/.cache/go-build | string |
Save golangci-lint cache.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
key | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' | string |
path | Path to cache. | No | /home/circleci/.cache/golangci-lint | string |
Save Go modules to cache.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
key | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' | string |
path | Path to cache. | No | /home/circleci/go/pkg/mod | string |
Runs 'go test ./...' but includes extensive parameterization for finer tuning
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
build_ldflags | Arguments to pass on each go tool link invocation. | No | '' | string |
build_tags | A comma-separated list of additional build tags to consider satisfied during the build. | No | '' | string |
count | run each test and benchmark n times | No | '1' | string |
covermode | flag to set the coverage mode
(the go default is: "set", unless -race is enabled, in which case the go default is "atomic")
| No | set | enum |
coverpkg | Apply coverage analysis in each test to packages matching the patterns.
(Sets -cover.)
| No | ./... | string |
coverprofile | file to save coverage profile | No | cover-source.out | string |
failfast | do not start new tests after the first test failure | No | false | boolean |
no_output_timeout | The amount of time to allow the command to run before timing out. | No | 10m | string |
packages | Import tests to run, by path glob. | No | ./... | string |
parallel | The number of programs, such as build commands or
test binaries, that can be run in parallel.
The default is GOMAXPROCS, normally the number of CPUs available.
| No | m | string |
parallel-tests | Allow parallel execution of test functions that call t.Parallel.
The value of this flag is the maximum number of tests to run
simultaneously.
By default, -parallel is set to the value of GOMAXPROCS.
| No | m | string |
project-path | The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| No | '' | string |
race | run tests with -race option
(if enabled, update covermode to be "atomic")
| No | false | boolean |
run | A regexp matching the names of the tests and examples that will be executed. | No | '' | string |
short | tell long-running tests to shorten their run time | No | false | boolean |
timeout | timeout the test and panic after value - for situations where a test has a valid usecase to go longer than 10m | No | 10m | string |
verbose | log all tests as they are run. Also print all text from Log and Logf calls even if the test succeeds. | No | false | boolean |
Runs the given steps
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
build | Whether to use go-build cache. | No | true | boolean |
build-path | Location of go-build cache. | No | /home/circleci/.cache/go-build | string |
golangci-lint | Whether to use golangci-lint cache. Useful only in steps with linting, so defaults to false. | No | false | boolean |
golangci-lint-path | Location of golangci-lint cache. | No | /home/circleci/.cache/golangci-lint | string |
key | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' | string |
mod | Whether to use go module cache. If most of your dependencies are public, it is faster to use the public
Go module proxy, so this defaults to `false`.
| No | false | boolean |
mod-path | Location of go module cache. | No | /home/circleci/go/pkg/mod | string |
steps | The steps to run with caching. | Yes | - | steps |
The official CircleCI Go Docker image on Docker Hub. Found here: https://hub.docker.com/r/cimg/go
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
tag | The `cimg/go` Docker image version tag. | Yes | - | string |
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
Common CircleCI tasks for the Go ( Golang ) programming language.
display:
home_url: https://golang.org/
source_url: https://github.com/CircleCI-Public/go-orb
orbs:
os-detect: circleci/os-detect@0.2
commands:
goreleaser-release:
description: |
Build Go applications and release them on GitHub.
GoReleaser CLI must be installed and a GitHub Token
with write:packages permissions is also required.
parameters:
github-token:
default: GITHUB_TOKEN
description: |
A GitHub Token is with write:packages permissions is required.
type: string
project-path:
default: $HOME
description: |
The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
type: string
publish-release:
default: false
description: |
Set to true to publish release to GitHub
type: boolean
validate-yaml:
default: false
description: |
Set to true to validate .goreleaser.yaml.
type: boolean
steps:
- run:
command: "#!/bin/bash\n\nGITHUB_TOKEN=\"$(echo \"\\$$GITHUB_TOKEN\" | circleci env subst)\"\nGO_EVAL_PROJECT_PATH=\"$(eval echo \"${GO_EVAL_PROJECT_PATH}\")\"\n\n# Change to directory containing project files\ncd \"${GO_EVAL_PROJECT_PATH}\" || return\n\nif [ -z \"${GITHUB_TOKEN}\" ]; then\n echo \"No GitHub Token provided. Please add token as environment variable in CircleCI.\"\n exit 1\nfi\n\nif [ \"$GO_BOOL_VALIDATE_YAML\" -eq 1 ]; then \n # Validate .goreleaser.yaml file\n goreleaser check\nfi\n\nif [ \"$GO_BOOL_PUBLISH_RELEASE\" -eq 1 ]; then \n # Build binaries and publish release to GitHub\n goreleaser release \nelse\n # Build binaries and test release locally\n goreleaser release --snapshot --clean\nfi\n\n\n"
environment:
GITHUB_TOKEN: << parameters.github-token >>
GO_BOOL_PUBLISH_RELEASE: << parameters.publish-release >>
GO_BOOL_VALIDATE_YAML: << parameters.validate-yaml >>
GO_EVAL_PROJECT_PATH: << parameters.project-path >>
name: Build and release Go Binaries to GitHub with GoReleaser
gotestsum:
description: |
Runs 'go test ./...' but includes extensive parameterization for finer tuning
parameters:
covermode:
default: atomic
description: mode used to count coverage
type: string
coverprofile:
default: cover-source.out
description: file to save coverage profile
type: string
junitfile:
default: unit-tests.xml
description: file to save junit format xml file
type: string
packages:
default: ./...
description: import tests to run, by path glob.
type: string
project-path:
default: ""
description: |
The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
type: string
steps:
- run:
command: |
#! /bin/bash
if [ ! "$(which gotestsum)" ]; then
echo The gotestsum command is not available in this executor.
exit 1
fi
if [ -n "${ORB_EVAL_PROJECT_PATH}" ]; then
cd "${ORB_EVAL_PROJECT_PATH}" || exit
fi
if [ -n "$ORB_VAL_PACKAGES" ];then
set -- "$@" --packages "${ORB_VAL_PACKAGES}"
fi
if [ -n "$ORB_VAL_JUNITFILE" ];then
set -- "$@" --junitfile "${ORB_VAL_JUNITFILE}"
fi
if [ -n "$ORB_VAL_COVERMODE" ];then
COVERMODE="-covermode=${ORB_VAL_COVERMODE}"
fi
if [ -n "$ORB_EVAL_COVER_PROFILE" ];then
INPUT_COVER_PROFILE=$(eval echo "$ORB_EVAL_COVER_PROFILE")
COVER_PROFILE="-coverprofile=${INPUT_COVER_PROFILE}"
fi
set -x
gotestsum \
"$@" \
-- \
"${COVERMODE}" \
"${COVER_PROFILE}" \
-coverpkg="$ORB_VAL_PACKAGES"
set +x
environment:
ORB_EVAL_COVER_PROFILE: <<parameters.coverprofile>>
ORB_EVAL_PROJECT_PATH: <<parameters.project-path>>
ORB_VAL_COVERMODE: <<parameters.covermode>>
ORB_VAL_JUNITFILE: <<parameters.junitfile>>
ORB_VAL_PACKAGES: <<parameters.packages>>
name: gotestsum
install:
description: |
Install Go in a build. Supports Linux/amd64 and macOS/amd64.
parameters:
cache:
default: true
description: Whether or not to cache the binary.
type: boolean
cache-key:
default: v2
description: |
String to use in cache key. Typically overridden when needed to bust cache.
type: string
version:
default: 1.16.5
description: The Go version.
type: string
steps:
- os-detect/init
- when:
condition:
equal:
- <<parameters.cache>>
- true
steps:
- run:
command: |
$SUDO rm -rf /usr/local/go
$SUDO mkdir -p /usr/local/go
$SUDO chown -R $(whoami): /usr/local/go
name: Prep cache restore
- restore_cache:
keys:
- go-binary-<<parameters.cache-key>>-<<parameters.version>>-{{ arch }}
- run:
command: |
#!/usr/bin/env bash
function alpine_install {
cd /opt || exit 200
apk add bash gcc musl-dev go
wget "https://go.dev/dl/go${ORB_VAL_VERSION}.src.tar.gz"
tar xzf "go${ORB_VAL_VERSION}.src.tar.gz"
mv go "go${ORB_VAL_VERSION}"
cd "go${ORB_VAL_VERSION}/src" || exit 201
./make.bash
apk del go
ln -sf "/opt/go${ORB_VAL_VERSION}/bin/go" /usr/local/bin/go
ln -sf "/opt/go${ORB_VAL_VERSION}/bin/gofmt" /usr/local/bin/gofmt
}
function standard_install {
if command -v go >/dev/null; then
if go version | grep -q -F "go\<< parameters.version >> "; then
echo "Binary already exists, skipping download."
exit 0
fi
echo "Found a different version of Go."
OSD_FAMILY="$(go env GOHOSTOS)"
HOSTTYPE="$(go env GOHOSTARCH)"
$SUDO rm -rf /usr/local/go
$SUDO install --owner="${USER}" -d /usr/local/go
$SUDO rm -rf /opt/go
fi
echo "Installing the requested version of Go."
curl -O --fail --location -sS "https://dl.google.com/go/go${ORB_VAL_VERSION}.${OSD_FAMILY}-${HOSTTYPE}.tar.gz"
$SUDO tar xzf "go${ORB_VAL_VERSION}.${OSD_FAMILY}-${HOSTTYPE}.tar.gz" -C /opt
$SUDO rm "go${ORB_VAL_VERSION}.${OSD_FAMILY}-${HOSTTYPE}.tar.gz"
$SUDO ln -sf /opt/go/bin/go /usr/local/bin/go
$SUDO ln -sf /opt/go/bin/gofmt /usr/local/bin/gofmt
#shellcheck disable=SC2016
$SUDO chown -R "$(whoami)": /usr/local/bin/go /usr/local/bin/gofmt
}
: "${OSD_FAMILY:="linux"}"
: "${HOSTTYPE:="amd64"}"
if [ "${HOSTTYPE}" = "x86_64" ]; then HOSTTYPE="amd64"; fi
if [ "${HOSTTYPE}" = "aarch64" ]; then HOSTTYPE="arm64"; fi
case "${HOSTTYPE}" in *86) HOSTTYPE=i386 ;; esac
if grep alpinelinux /etc/os-release; then
alpine_install
else
standard_install
fi
environment:
ORB_VAL_VERSION: << parameters.version >>
name: Install Go
- run:
command: echo "Installed " && go version
name: Verify Go Installation
- when:
condition:
equal:
- <<parameters.cache>>
- true
steps:
- save_cache:
key: go-binary-<<parameters.cache-key>>-<<parameters.version>>-{{ arch }}
paths:
- /usr/local/go
install-goreleaser:
description: |
GoReleaser is a release automation tool for managing Go projects.
Install GoReleaser in your build. Golang must be installed.
parameters:
version:
default: latest
description: The GoReleaser version to install. Defaults to latest
type: string
steps:
- run:
command: |-
#!/bin/bash
GO_STR_VERSION="$(echo "${GO_STR_VERSION}"| circleci env subst)"
go install github.com/goreleaser/goreleaser@"${GO_STR_VERSION}"
goreleaser --version
environment:
GO_STR_VERSION: << parameters.version >>
name: Installing GoReleaser
load-build-cache:
description: Load cached Go build cache.
parameters:
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
steps:
- restore_cache:
keys:
- v1-<< parameters.key >>-go-build-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
load-golangci-lint-cache:
description: Load cached Go build cache.
parameters:
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
steps:
- restore_cache:
keys:
- v1-<< parameters.key >>-golangci-lint-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
load-mod-cache:
description: Load cached Go modules.
parameters:
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
steps:
- restore_cache:
keys:
- v1-<< parameters.key >>-go-mod-{{ arch }}-{{ checksum "go.sum" }}
mod-download:
description: Run 'go mod download'.
steps:
- run:
command: go mod download
name: go mod download
mod-download-cached:
description: Download and cache Go modules
steps:
- load-mod-cache
- mod-download
- save-mod-cache
save-build-cache:
description: Save Go build cache.
parameters:
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
path:
default: /home/circleci/.cache/go-build
description: Path to cache.
type: string
steps:
- save_cache:
key: v1-<< parameters.key >>-go-build-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
paths:
- << parameters.path >>
save-golangci-lint-cache:
description: Save golangci-lint cache.
parameters:
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
path:
default: /home/circleci/.cache/golangci-lint
description: Path to cache.
type: string
steps:
- save_cache:
key: v1-<< parameters.key >>-golangci-lint-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
paths:
- << parameters.path >>
save-mod-cache:
description: Save Go modules to cache.
parameters:
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
path:
default: /home/circleci/go/pkg/mod
description: Path to cache.
type: string
steps:
- save_cache:
key: v1-<< parameters.key >>-go-mod-{{ arch }}-{{ checksum "go.sum" }}
paths:
- << parameters.path >>
test:
description: |
Runs 'go test ./...' but includes extensive parameterization for finer tuning
parameters:
build_ldflags:
default: ""
description: Arguments to pass on each go tool link invocation.
type: string
build_tags:
default: ""
description: A comma-separated list of additional build tags to consider satisfied during the build.
type: string
count:
default: "1"
description: run each test and benchmark n times
type: string
covermode:
default: set
description: |
flag to set the coverage mode
(the go default is: "set", unless -race is enabled, in which case the go default is "atomic")
enum:
- set
- count
- atomic
type: enum
coverpkg:
default: ./...
description: |
Apply coverage analysis in each test to packages matching the patterns.
(Sets -cover.)
type: string
coverprofile:
default: cover-source.out
description: file to save coverage profile
type: string
failfast:
default: false
description: do not start new tests after the first test failure
type: boolean
no_output_timeout:
default: 10m
description: The amount of time to allow the command to run before timing out.
type: string
packages:
default: ./...
description: Import tests to run, by path glob.
type: string
parallel:
default: m
description: |
The number of programs, such as build commands or
test binaries, that can be run in parallel.
The default is GOMAXPROCS, normally the number of CPUs available.
type: string
parallel-tests:
default: m
description: |
Allow parallel execution of test functions that call t.Parallel.
The value of this flag is the maximum number of tests to run
simultaneously.
By default, -parallel is set to the value of GOMAXPROCS.
type: string
project-path:
default: ""
description: |
The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
type: string
race:
default: false
description: |
run tests with -race option
(if enabled, update covermode to be "atomic")
type: boolean
run:
default: ""
description: A regexp matching the names of the tests and examples that will be executed.
type: string
short:
default: false
description: tell long-running tests to shorten their run time
type: boolean
timeout:
default: 10m
description: timeout the test and panic after value - for situations where a test has a valid usecase to go longer than 10m
type: string
verbose:
default: false
description: log all tests as they are run. Also print all text from Log and Logf calls even if the test succeeds.
type: boolean
steps:
- run:
command: |
#!/usr/bin/env bash
if [ "${ORB_VAL_PARALLEL}" = "m" ] || [ "${ORB_VAL_PARALLEL_TESTS}" = "m" ]; then
mkdir -p /tmp/go-orb/max_parallelism/
echo "$MAX_PARALLELISM_GO_CONTENTS" > /tmp/go-orb/max_parallelism/main.go
GOMAXPROCS=$(go run /tmp/go-orb/max_parallelism/main.go)
rm -rf /tmp/go-orb
fi
if [ "${ORB_VAL_PARALLEL}" = "m" ]; then
ORB_VAL_PARALLEL=$GOMAXPROCS
fi
if [ "${ORB_VAL_PARALLEL_TESTS}" = "m" ]; then
ORB_VAL_PARALLEL_TESTS=$GOMAXPROCS
fi
if [ -n "${ORB_EVAL_PROJECT_PATH}" ]; then
cd "${ORB_EVAL_PROJECT_PATH}" || exit
fi
COVER_PROFILE=$(eval echo "$ORB_EVAL_COVER_PROFILE")
if [ -n "$ORB_VAL_RACE" ]; then
set -- "$@" -race
ORB_VAL_COVER_MODE=atomic
fi
if [ "$ORB_VAL_FAILFAST" != "false" ]; then
set -- "$@" -failfast
fi
if [ -n "$ORB_VAL_SHORT" ]; then
set -- "$@" -short
fi
if [ -n "$ORB_VAL_VERBOSE" ]; then
set -- "$@" -v
fi
set -x
go test -count="$ORB_VAL_COUNT" -p "${ORB_VAL_PARALLEL}" \
-parallel "${ORB_VAL_PARALLEL_TESTS}" \
-coverprofile="$COVER_PROFILE" -covermode="$ORB_VAL_COVER_MODE" \
"$ORB_VAL_PACKAGES" -coverpkg="$ORB_VAL_PACKAGES" \
-timeout="$ORB_VAL_TIMEOUT" -tags="$ORB_VAL_BUILD_TAGS" \
-ldflags="$ORB_VAL_BUILD_LDFLAGS" -run="$ORB_VAL_RUN" \
"$@"
set +x
environment:
MAX_PARALLELISM_GO_CONTENTS: |
package main
import (
"fmt"
"runtime"
)
func MaxParallelism() int {
maxProcs := runtime.GOMAXPROCS(0)
numCPU := runtime.NumCPU()
if maxProcs < numCPU {
return maxProcs
}
return numCPU
}
func main() {
fmt.Println(MaxParallelism())
}
ORB_EVAL_COVER_PROFILE: <<parameters.coverprofile>>
ORB_EVAL_PROJECT_PATH: <<parameters.project-path>>
ORB_VAL_BUILD_LDFLAGS: <<parameters.build_ldflags>>
ORB_VAL_BUILD_TAGS: <<parameters.build_tags>>
ORB_VAL_COUNT: <<parameters.count>>
ORB_VAL_COVER_MODE: <<parameters.covermode>>
ORB_VAL_COVERPKG: <<parameters.coverpkg>>
ORB_VAL_FAILFAST: <<parameters.failfast>>
ORB_VAL_PACKAGES: <<parameters.packages>>
ORB_VAL_PARALLEL: <<parameters.parallel>>
ORB_VAL_PARALLEL_TESTS: <<parameters.parallel-tests>>
ORB_VAL_RACE: <<parameters.race>>
ORB_VAL_RUN: <<parameters.run>>
ORB_VAL_SHORT: <<parameters.short>>
ORB_VAL_TIMEOUT: <<parameters.timeout>>
ORB_VAL_VERBOSE: <<parameters.verbose>>
name: go test
no_output_timeout: <<parameters.no_output_timeout>>
with-cache:
description: |
Runs the given steps
parameters:
build:
default: true
description: Whether to use go-build cache.
type: boolean
build-path:
default: /home/circleci/.cache/go-build
description: Location of go-build cache.
type: string
golangci-lint:
default: false
description: Whether to use golangci-lint cache. Useful only in steps with linting, so defaults to false.
type: boolean
golangci-lint-path:
default: /home/circleci/.cache/golangci-lint
description: Location of golangci-lint cache.
type: string
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
mod:
default: false
description: |
Whether to use go module cache. If most of your dependencies are public, it is faster to use the public
Go module proxy, so this defaults to `false`.
type: boolean
mod-path:
default: /home/circleci/go/pkg/mod
description: Location of go module cache.
type: string
steps:
description: The steps to run with caching.
type: steps
steps:
- when:
condition: << parameters.build >>
steps:
- load-build-cache:
key: << parameters.key >>
- when:
condition: << parameters.mod >>
steps:
- load-mod-cache:
key: << parameters.key >>
- when:
condition: << parameters.golangci-lint >>
steps:
- load-golangci-lint-cache:
key: << parameters.key >>
- steps: << parameters.steps >>
- when:
condition: << parameters.build >>
steps:
- save-build-cache:
key: << parameters.key >>
path: << parameters.build-path >>
- when:
condition: << parameters.mod >>
steps:
- save-mod-cache:
key: << parameters.key >>
path: << parameters.mod-path >>
- when:
condition: << parameters.golangci-lint >>
steps:
- save-golangci-lint-cache:
key: << parameters.key >>
path: << parameters.golangci-lint-path >>
executors:
default:
description: |
The official CircleCI Go Docker image on Docker Hub.
Found here: https://hub.docker.com/r/cimg/go
docker:
- image: cimg/go:<< parameters.tag >>
parameters:
tag:
description: The `cimg/go` Docker image version tag.
type: string
examples:
goreleaser-release:
description: |
Build Go applications and release them on GitHub.
Install GoReleaser CLI and add a GitHub Token with
write:packages permissions and store as environment variable.
usage:
version: "2.1"
orbs:
go: circleci/go@x.y
jobs:
go-build-release:
executor:
name: go/default
tag: 1.20.8
steps:
- go/install-goreleaser
- go/goreleaser-release:
project-path: /path/to/Go/project
publish-release: true
validate-yaml: true
workflows:
main:
jobs:
- go-build-release
test:
description: Run go test ./... with additional (optional) parameters
usage:
version: "2.1"
orbs:
go: circleci/go@x.y
jobs:
build:
executor:
name: go/default
tag: "1.16"
steps:
- checkout
- go/with-cache:
steps:
- go/mod-download
- go/test:
build_ldflags: -X 'main.Version=v1.0.0'
build_tags: integration,e2e
covermode: atomic
failfast: true
no_output_timeout: 15m
race: true
run: ^TestName$
timeout: 15m
workflows:
main:
jobs:
- build
with-cache:
description: Speeding up builds with the Go modules cache on CircleCI.
usage:
version: "2.1"
orbs:
go: circleci/go@x.y
jobs:
build:
executor:
name: go/default
tag: "1.23"
steps:
- checkout
- go/with-cache:
steps:
- go/mod-download
- go/test
workflows:
main:
jobs:
- build