A shareable package of CircleCI configuration to integrate with go, written by circleci
CertifiedLanguage/FrameworkUse CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1Add the orbs stanza below your version, invoking the orb:
1
2
orbs:
go: circleci/go@3.0.4Use 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.
| A GitHub Token is with write:packages permissions is required.
| No | GITHUB_TOKEN type: string | string |
project-path The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| No | $HOME type: string | string |
publish-release Set to true to publish release to GitHub
| Set to true to publish release to GitHub
| No | false type: boolean | boolean |
validate-yaml Set to true to validate .goreleaser.yaml.
| Set to true to validate .goreleaser.yaml.
| No | false type: boolean | boolean |
Runs 'go test ./...' but includes extensive parameterization for finer tuning
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
covermode mode used to count coverage | mode used to count coverage | No | atomic type: string | string |
coverprofile file to save coverage profile | file to save coverage profile | No | cover-source.out type: string | string |
junitfile file to save junit format xml file | file to save junit format xml file | No | unit-tests.xml type: string | string |
packages import tests to run, by path glob. | import tests to run, by path glob. | No | ./... type: string | string |
project-path The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| No | '' type: string | 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. | Whether or not to cache the binary. | No | true type: boolean | boolean |
cache-key String to use in cache key. Typically overridden when needed to bust cache.
| String to use in cache key. Typically overridden when needed to bust cache.
| No | v2 type: string | string |
version The Go version. | The Go version. | No | 1.16.5 type: string | string |
Install goreleaser
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
shell The shell to use for running the script, windows should be bash but alpine may be sh.
| The shell to use for running the script, windows should be bash but alpine may be sh.
| No | bash type: string | string |
version The version of goreleaser to install. If not specified, the latest version will be installed.
| The version of goreleaser to install. If not specified, the latest version will be installed.
| No | '' type: string | string |
Load cached Go build cache.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
checksum Checksum template instruction | Checksum template instruction | No | '{{ checksum "go.sum" }}' type: string | string |
key User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' type: string | string |
Load cached Go build cache.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
checksum Checksum template instruction | Checksum template instruction | No | '{{ checksum "go.sum" }}' type: string | string |
key User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' type: string | string |
Load cached Go modules.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
checksum Checksum template instruction | Checksum template instruction | No | '{{ checksum "go.sum" }}' type: string | string |
key User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' type: string | string |
Save Go build cache.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
checksum Checksum template instruction | Checksum template instruction | No | '{{ checksum "go.sum" }}' type: string | string |
key User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' type: string | string |
path Path to cache. | Path to cache. | No | ~/.cache/go-build type: string | string |
Save golangci-lint cache.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
checksum Checksum template instruction | Checksum template instruction | No | '{{ checksum "go.sum" }}' type: string | string |
key User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' type: string | string |
path Path to cache. | Path to cache. | No | ~/.cache/golangci-lint type: string | string |
Save Go modules to cache.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
checksum Checksum template instruction | Checksum template instruction | No | '{{ checksum "go.sum" }}' type: string | string |
key User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' type: string | string |
path Path to cache. | Path to cache. | No | ~/go/pkg/mod type: string | 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. | Arguments to pass on each go tool link invocation. | No | '' type: string | string |
build_tags A comma-separated list of additional build tags to consider satisfied during the build. | A comma-separated list of additional build tags to consider satisfied during the build. | No | '' type: string | string |
count run each test and benchmark n times | run each test and benchmark n times | No | '1' type: string | 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")
| 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 type: enum | enum |
coverpkg Apply coverage analysis in each test to packages matching the patterns.
(Sets -cover.)
| Apply coverage analysis in each test to packages matching the patterns.
(Sets -cover.)
| No | ./... type: string | string |
coverprofile file to save coverage profile | file to save coverage profile | No | cover-source.out type: string | string |
failfast do not start new tests after the first test failure | do not start new tests after the first test failure | No | false type: boolean | boolean |
no_output_timeout The amount of time to allow the command to run before timing out. | The amount of time to allow the command to run before timing out. | No | 10m type: string | string |
packages Import tests to run, by path glob. | Import tests to run, by path glob. | No | ./... type: string | 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.
| 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 type: string | 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.
| 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 type: string | string |
project-path The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
| No | '' type: string | string |
race run tests with -race option
(if enabled, update covermode to be "atomic")
| run tests with -race option
(if enabled, update covermode to be "atomic")
| No | false type: boolean | boolean |
run A regexp matching the names of the tests and examples that will be executed. | A regexp matching the names of the tests and examples that will be executed. | No | '' type: string | string |
short tell long-running tests to shorten their run time | tell long-running tests to shorten their run time | No | false type: boolean | boolean |
timeout timeout the test and panic after value - for situations where a test has a valid usecase to go longer than 10m | timeout the test and panic after value - for situations where a test has a valid usecase to go longer than 10m | No | 10m type: string | string |
verbose log all tests as they are run. Also print all text from Log and Logf calls even if the test succeeds. | log all tests as they are run. Also print all text from Log and Logf calls even if the test succeeds. | No | false type: boolean | boolean |
Runs the given steps
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
build Whether to use go-build cache. | Whether to use go-build cache. | No | true type: boolean | boolean |
build-path Location of go-build cache. | Location of go-build cache. | No | ~/.cache/go-build type: string | string |
checksum Checksum template instruction | Checksum template instruction | No | '{{ checksum "go.sum" }}' type: string | string |
golangci-lint Whether to use golangci-lint cache. Useful only in steps with linting, so defaults to false. | Whether to use golangci-lint cache. Useful only in steps with linting, so defaults to false. | No | false type: boolean | boolean |
golangci-lint-path Location of golangci-lint cache. | Location of golangci-lint cache. | No | ~/.cache/golangci-lint type: string | string |
key User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | User-configurable component for cache key. Useful for avoiding collisions in complex workflows. | No | '' type: string | 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`.
| 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 type: boolean | boolean |
mod-path Location of go module cache. | Location of go module cache. | No | ~/go/pkg/mod type: string | string |
steps The steps to run with caching. Required | The steps to run with caching. | Yes | - type: steps | 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. Required | The `cimg/go` Docker image version tag. | Yes | - type: string | 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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
# 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 \"\\$$GO_ENV_VAR_NAME_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:
GO_BOOL_PUBLISH_RELEASE: << parameters.publish-release >>
GO_BOOL_VALIDATE_YAML: << parameters.validate-yaml >>
GO_ENV_VAR_NAME_GITHUB_TOKEN: << parameters.github-token >>
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
RETRY_ATTEMPTS=3
RETRY_DELAY=5
function retry {
local attempt=1
while [ "$attempt" -le "$RETRY_ATTEMPTS" ]; do
echo "Attempt $attempt of $RETRY_ATTEMPTS: $*" >&2
if "$@"; then
return 0
fi
echo "Attempt $attempt failed." >&2
attempt=$((attempt + 1))
if [ "$attempt" -le "$RETRY_ATTEMPTS" ]; then
echo "Retrying in ${RETRY_DELAY} seconds..." >&2
sleep "$RETRY_DELAY"
fi
done
echo "All $RETRY_ATTEMPTS attempts failed." >&2
return 1
}
function alpine_install {
cd /opt || exit 200
apk add bash gcc musl-dev go
retry wget --timeout=300 "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."
retry curl -O --fail --location -sS --connect-timeout 30 --max-time 300 "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: |
Install goreleaser
parameters:
shell:
default: bash
description: |
The shell to use for running the script, windows should be bash but alpine may be sh.
type: string
version:
default: ""
description: |
The version of goreleaser to install. If not specified, the latest version will be installed.
type: string
steps:
- run:
command: |
#!/usr/bin/env bash
set -euo pipefail
RETRY_ATTEMPTS=3
RETRY_DELAY=5
function retry {
local attempt=1
while [ "$attempt" -le "$RETRY_ATTEMPTS" ]; do
echo "Attempt $attempt of $RETRY_ATTEMPTS: $*" >&2
if "$@"; then
return 0
fi
echo "Attempt $attempt failed." >&2
attempt=$((attempt + 1))
if [ "$attempt" -le "$RETRY_ATTEMPTS" ]; then
echo "Retrying in ${RETRY_DELAY} seconds..." >&2
sleep "$RETRY_DELAY"
fi
done
echo "All $RETRY_ATTEMPTS attempts failed." >&2
return 1
}
function alpine_install_curl() {
apk add curl
}
function get_os() {
local os
os=$(uname -o)
case "$os" in
GNU/Linux) echo "Linux" ;;
Linux) echo "Linux" ;;
Msys) echo "Windows" ;;
Darwin) echo "Darwin" ;;
*) echo "Unsupported OS: $os" ; exit 1;;
esac
}
function get_architecture() {
local arch
arch=$(uname -m)
case "$arch" in
aarch64) echo "arm64" ;;
arm64) echo "arm64" ;;
x86_64) echo "x86_64" ;;
*) echo "Unsupported architecture: $arch" ; exit 1;;
esac
}
download_goreleaser() {
echo "Downloading goreleaser for ${architecture}..."
if ! which curl &>/dev/null; then
echo "curl is not installed. Please install curl and try again."
exit 1
fi
if [ -z "${GO_STR_VERSION:-}" ]; then
# Taken from https://goreleaser.com/install/#bash-script
GO_STR_VERSION="$(retry curl -sf --connect-timeout 30 --max-time 30 https://goreleaser.com/static/latest)"
fi
URL="https://github.com/goreleaser/goreleaser/releases/download/${GO_STR_VERSION}/goreleaser_${os}_${architecture}.${file_extension}"
echo "Downloading goreleaser from ${URL}"
if ! retry curl --fail --location "${URL}" --output "${TMP_FILE}" --connect-timeout 30 --max-time 600; then
echo "Failed to download goreleaser"
exit 1
fi
}
extract_goreleaser_win() {
echo "Running on Windows, using unzip to extract..."
mkdir --parent "${HOME}/go/bin"
if ! unzip -n /tmp/goreleaser.zip -d "${HOME}/go/bin" goreleaser.exe; then
echo "Failed to extract goreleaser"
exit 1
fi
}
extract_goreleaser_nix() {
echo "Running on Linux, Attempting to extract to /usr/local/bin..."
if ! tar -xvzf "${TMP_FILE}" -C /usr/local/bin goreleaser ; then
echo "Failed to extract goreleaser to /usr/local/bin with normal privileges, trying with sudo..."
if ! sudo tar -xvzf "${TMP_FILE}" -C /usr/local/bin goreleaser; then
echo "Failed to extract goreleaser"
exit 1
fi
fi
}
extract_goreleaser() {
architecture="$(get_architecture)"
os="$(get_os)"
if [[ "${os}" == "Windows" ]]; then
extract_goreleaser_win
else
extract_goreleaser_nix
fi
}
if grep alpinelinux /etc/os-release; then
if which curl; then
echo curl found in alpine
else
echo Installing curl in alpine
alpine_install_curl
fi
fi
if ! which goreleaser &>/dev/null; then
echo "Installing goreleaser..."
architecture="$(get_architecture)"
os="$(get_os)"
if [[ "${os}" == "Windows" ]]; then
file_extension="zip"
else
file_extension="tar.gz"
fi
TMP_FILE="/tmp/goreleaser.${file_extension}"
download_goreleaser
extract_goreleaser
else
echo "goreleaser is already installed. Exiting..."
exit 0
fi
goreleaser --version
environment:
GO_STR_VERSION: << parameters.version >>
name: Install goreleaser
shell: << parameters.shell >>
load-build-cache:
description: Load cached Go build cache.
parameters:
checksum:
default: '{{ checksum "go.sum" }}'
description: Checksum template instruction
type: string
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 }}-<< parameters.checksum >>-{{ epoch | round "72h" }}
load-golangci-lint-cache:
description: Load cached Go build cache.
parameters:
checksum:
default: '{{ checksum "go.sum" }}'
description: Checksum template instruction
type: string
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 }}-<< parameters.checksum >>-{{ epoch | round "72h" }}
load-mod-cache:
description: Load cached Go modules.
parameters:
checksum:
default: '{{ checksum "go.sum" }}'
description: Checksum template instruction
type: string
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 }}-<< parameters.checksum >>
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:
checksum:
default: '{{ checksum "go.sum" }}'
description: Checksum template instruction
type: string
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
path:
default: ~/.cache/go-build
description: Path to cache.
type: string
steps:
- save_cache:
key: v1-<< parameters.key >>-go-build-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-<< parameters.checksum >>-{{ epoch | round "72h" }}
paths:
- << parameters.path >>
save-golangci-lint-cache:
description: Save golangci-lint cache.
parameters:
checksum:
default: '{{ checksum "go.sum" }}'
description: Checksum template instruction
type: string
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
path:
default: ~/.cache/golangci-lint
description: Path to cache.
type: string
steps:
- save_cache:
key: v1-<< parameters.key >>-golangci-lint-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-<< parameters.checksum >>-{{ epoch | round "72h" }}
paths:
- << parameters.path >>
save-mod-cache:
description: Save Go modules to cache.
parameters:
checksum:
default: '{{ checksum "go.sum" }}'
description: Checksum template instruction
type: string
key:
default: ""
description: User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
type: string
path:
default: ~/go/pkg/mod
description: Path to cache.
type: string
steps:
- save_cache:
key: v1-<< parameters.key >>-go-mod-{{ arch }}-<< parameters.checksum >>
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 [ "$ORB_VAL_RACE" -eq 1 ]; then
set -- "$@" -race
ORB_VAL_COVER_MODE=atomic
fi
if [ "$ORB_VAL_FAILFAST" -eq 1 ]; then
set -- "$@" -failfast
fi
if [ "$ORB_VAL_SHORT" -eq 1 ]; then
set -- "$@" -short
fi
if [ "$ORB_VAL_VERBOSE" -eq 1 ]; 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: ~/.cache/go-build
description: Location of go-build cache.
type: string
checksum:
default: '{{ checksum "go.sum" }}'
description: Checksum template instruction
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: ~/.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: ~/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:
checksum: << parameters.checksum >>
key: << parameters.key >>
- when:
condition: << parameters.mod >>
steps:
- load-mod-cache:
checksum: << parameters.checksum >>
key: << parameters.key >>
- when:
condition: << parameters.golangci-lint >>
steps:
- load-golangci-lint-cache:
checksum: << parameters.checksum >>
key: << parameters.key >>
- steps: << parameters.steps >>
- when:
condition: << parameters.build >>
steps:
- save-build-cache:
checksum: << parameters.checksum >>
key: << parameters.key >>
path: << parameters.build-path >>
- when:
condition: << parameters.mod >>
steps:
- save-mod-cache:
checksum: << parameters.key >>
key: << parameters.key >>
path: << parameters.mod-path >>
- when:
condition: << parameters.golangci-lint >>
steps:
- save-golangci-lint-cache:
checksum: << parameters.checksum >>
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