Skip to main content

Go

You can build and test a Go application using a Linux platform on Harness Cloud or a self-managed Kubernetes cluster build infrastructure.

This guide assumes you've created a Harness CI pipeline.

Install dependencies

If necessary, add a Run step to install any dependencies.

- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
shell: Sh
command: |-
go get example.com/my-go-module

Cache dependencies

Add caching to your stage.

Cache your Go module dependencies with Cache Intelligence.

Add caching.enabled.true to your stage.spec:

- stage:
spec:
caching:
enabled: true

Build and run tests

Add Run steps to build and run your tests.

- step:
type: Run
identifier: build
name: Build
spec:
shell: Sh
command: |-
go build
- step:
type: Run
identifier: test
name: Test
spec:
shell: Sh
command: |-
go test -v ./...

Visualize test results

You can view test results on the Tests tab of your pipeline executions. Test results must be in JUnit XML format.

You can use go-junit-report to output compatible JUnit XML reports.

For your pipeline to produce test reports, you need to modify the Run step that runs your tests. Make sure the command generates JUnit XML reports and add the reports specification.

- step:
type: Run
identifier: test
name: Test
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go install github.com/jstemmer/go-junit-report/v2@latest
go test -v ./... | tee report.out
cat report.out | go-junit-report -set-exit-code > report.xml
reports:
type: JUnit
spec:
paths:
- report.xml

Test splitting

You can use test splitting (parallelism) to improve test times.

Specify version

Go is pre-installed on Hosted Cloud runners. For details about all available tools and versions, go to Platforms and image specifications.

If your application requires a specific version of Go, add a Run step to install it.

Install a specific version of Go
- step:
type: Run
identifier: installgo
name: Install Go
spec:
shell: Sh
# install version 1.20 of Go
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go install golang.org/dl/go1.20@latest
go1.20 download
Install multiple versions of Go
  1. Add the matrix looping strategy configuration to your stage.
strategy:
matrix:
# matrix strategy with Go versions 1.19 and 1.20
goVersion:
- "1.19"
- "1.20"
  1. Reference the matrix variable in your steps.
- step:
type: Run
identifier: installgo
name: Install Go
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go install golang.org/dl/go<+matrix.goVersion>@latest
go<+matrix.goVersion> download

Full pipeline examples

The following full pipeline examples are based on the partial examples above.

If you copy this example, replace the placeholder values with appropriate values for your code repo connector and repository name. Depending on your project and organization, you may also need to replace projectIdentifier and orgIdentifier.

Pipeline with one specific Go version

Here is a single-stage pipeline using Cache Intelligence with steps to install Go 1.20, build, and test.

pipeline:
name: Build and test Go app
identifier: Build_and_test_Go_app
projectIdentifier: default
orgIdentifier: default
stages:
- stage:
name: Build
identifier: Build
type: CI
spec:
caching:
enabled: true
cloneCodebase: true
execution:
steps:
- step:
type: Run
identifier: installgo
name: Install Go
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go install golang.org/dl/go1.20@latest
go1.20 download
- step:
type: Run
identifier: build
name: Build
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go1.20 build
- step:
type: Run
identifier: test
name: Test
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go1.20 install github.com/jstemmer/go-junit-report/v2@latest
go1.20 test -v | tee report.out
cat report.out | go-junit-report -set-exit-code > report.xml
reports:
type: JUnit
spec:
paths:
- report.xml
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}
properties:
ci:
codebase:
connectorRef: YOUR_CODE_REPO_CONNECTOR_ID
repoName: YOUR_REPO_NAME
build: <+input>
Pipeline with multiple Go versions

Here is a single-stage pipeline using Cache Intelligence with a matrix looping strategy for Go versions 1.19 and 1.20.

pipeline:
name: Build and test Go app
identifier: Build_and_test_Go_app
projectIdentifier: default
orgIdentifier: default
stages:
- stage:
name: Build
identifier: Build
type: CI
strategy:
matrix:
goVersion:
- "1.19"
- "1.20"
spec:
caching:
enabled: true
cloneCodebase: true
execution:
steps:
- step:
type: Run
identifier: installgo
name: Install Go
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go install golang.org/dl/go<+matrix.goVersion>@latest
go<+matrix.goVersion> download
- step:
type: Run
identifier: build
name: Build
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go<+matrix.goVersion> build
- step:
type: Run
name: Test
identifier: test
spec:
shell: Sh
command: |-
export PATH=$(go env GOPATH)/bin:$PATH
go<+matrix.goVersion> install github.com/jstemmer/go-junit-report/v2@latest
go<+matrix.goVersion> test -v ./... | tee report_<+matrix.goVersion>.out
cat report_<+matrix.goVersion>.out | go-junit-report -set-exit-code > report_<+matrix.goVersion>.xml
reports:
type: JUnit
spec:
paths:
- report_*.xml
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}
properties:
ci:
codebase:
connectorRef: YOUR_CODE_REPO_CONNECTOR_ID
repoName: YOUR_REPO_NAME
build: <+input>

Next steps

Now that you have created a pipeline that builds and tests a Go app, you could: