1. circleci/go@2.0.0

circleci/go@2.0.0

Certified
Sections
Common CircleCI tasks for the Go ( Golang ) programming language.
Created: July 29, 2019Version Published: November 27, 2024Releases: 28
Org Usage:
392
Categories:

Orb Quick Start Guide

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.0.0

Use go elements in your existing workflows and jobs.

Usage Examples

goreleaser-release

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

test

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 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: covermode: atomic failfast: true no_output_timeout: 15m race: true timeout: 15m workflows: main: jobs: - build

with-cache

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

Commands

goreleaser-release

Build Go applications and release them on GitHub. GoReleaser CLI must be installed and a GitHub Token with write:packages permissions is also required.

Show command Source
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

install

Install Go in a build. Supports Linux/amd64 and macOS/amd64.

Show command Source
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

install-goreleaser

GoReleaser is a release automation tool for managing Go projects. Install GoReleaser in your build. Golang must be installed.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
version
The GoReleaser version to install. Defaults to latest
No
latest
string

load-build-cache

Load cached Go build cache.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
key
User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
No
''
string

load-golangci-lint-cache

Load cached Go build cache.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
key
User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
No
''
string

load-mod-cache

Load cached Go modules.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
key
User-configurable component for cache key. Useful for avoiding collisions in complex workflows.
No
''
string

mod-download

Run 'go mod download'.

Show command Source

mod-download-cached

Download and cache Go modules

Show command Source

save-build-cache

Save Go build cache.

Show command Source
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

Save golangci-lint cache.

Show command Source
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-mod-cache

Save Go modules to cache.

Show command Source
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

test

Runs 'go test ./...' but includes extensive parameterization for finer tuning

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
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
Allow parallel execution of test functions that call t.Parallel. The value of this flag is the maximum number of tests to run simultaneously
No
'1'
string
race
run tests with -race option (if enabled, update covermode to be "atomic")
No
false
boolean
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

with-cache

Runs the given steps

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
build
Whether to use go-build cache.
No
true
boolean
golangci-lint
Whether to use golangci-lint cache. Useful only in steps with linting, so defaults to false.
No
false
boolean
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
steps
The steps to run with caching.
Yes
-
steps

Executors

default

The official CircleCI Go Docker image on Docker Hub. Found here: https://hub.docker.com/r/cimg/go

Show executor Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
tag
The `cimg/go` Docker image version tag.
Yes
-
string

Orb Source

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 # 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 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 : "${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 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 fi echo "Installing the requested version of Go." curl --fail --location -sS "https://dl.google.com/go/go${ORB_VAL_VERSION}.${OSD_FAMILY}-${HOSTTYPE}.tar.gz" \ | $SUDO tar --no-same-owner --strip-components=1 --gunzip -x -C /usr/local/go/ #shellcheck disable=SC2016 echo 'export PATH=$PATH:/usr/local/go/bin' >> "$BASH_ENV" $SUDO chown -R "$(whoami)": /usr/local/go 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: 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: "1" 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 type: string race: default: false description: | run tests with -race option (if enabled, update covermode to be "atomic") type: boolean 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 COVER_PROFILE=$(eval echo "$ORB_EVAL_COVER_PROFILE") if [ -n "$ORB_VAL_RACE" ]; then set -- "$@" -race 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" -coverprofile="$COVER_PROFILE" \ -p "$ORB_VAL_PARALLEL" -covermode="$ORB_VAL_COVER_MODE" \ "$ORB_VAL_PACKAGES" -coverpkg="$ORB_VAL_PACKAGES" \ -timeout="$ORB_VAL_TIMEOUT" \ "$@" set +x environment: ORB_EVAL_COVER_PROFILE: <<parameters.coverprofile>> ORB_VAL_COUNT: <<parameters.count>> ORB_VAL_COVER_MODE: <<parameters.covermode>> ORB_VAL_COVERPKG: <<parameters.coverpkg>> ORB_VAL_FAIL_FAST: <<parameters.failfast>> ORB_VAL_PACKAGES: <<parameters.packages>> ORB_VAL_PARALLEL: <<parameters.parallel>> ORB_VAL_RACE: <<parameters.race>> 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 golangci-lint: default: false description: Whether to use golangci-lint cache. Useful only in steps with linting, so defaults to false. type: boolean 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 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 >> - when: condition: << parameters.mod >> steps: - save-mod-cache: key: << parameters.key >> - when: condition: << parameters.golangci-lint >> steps: - save-golangci-lint-cache: key: << parameters.key >> 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: covermode: atomic failfast: true no_output_timeout: 15m race: true 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
Developer Updates
Get tips to optimize your builds
Or join our research panel and give feedback
By submitting this form, you are agreeing to ourTerms of UseandPrivacy Policy.