Go application
Build and test a Go application using a Linux platform on Harness Cloud build infrastructure, or on a self-hosted Kubernetes cluster.
Create pipeline
- Under Project Setup, select Get Started.
- Select your Go application repository from the repository list.
- Select Generate my Pipeline configuration, then select Create Pipeline.
- Select YAML to switch to the YAML editor.
Build and run tests
Add Run steps to build and run your tests.
- Harness Cloud
- Self-hosted
- 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 ./...
- step:
type: Run
identifier: build
name: Build
spec:
connectorRef: account.harnessImage
image: golang
command: |-
go build
- step:
type: Run
identifier: test
name: Test
spec:
connectorRef: account.harnessImage
image: golang
command: |-
go test -v ./...
Install dependencies
If necessary, add a Run step to install any dependencies.
- Harness Cloud
- Self-hosted
- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
shell: Sh
command: |-
go get example.com/my-go-module
- step:
type: Run
identifier: dependencies
name: Dependencies
spec:
connectorRef: account.harnessImage
image: golang
command: |-
go get example.com/my-go-module
Cache dependencies
Add caching to your stage.
- Harness Cloud
- Self-hosted
Cache your Go module dependencies with Cache Intelligence.
- stage:
spec:
caching:
enabled: true
With self-hosted build infrastructures, you can:
Go cache key and path requirements
Go pipelines must reference go.sum
for spec.key
in Save Cache and Restore Cache steps, for example:
spec:
key: cache-{{ checksum "go.sum" }}
Additionally, spec.sourcePaths
must include /go/pkg/mod
and /root/.cache/go-build
in the Save Cache step, for example:
spec:
sourcePaths:
- /go/pkg/mod
- /root/.cache/go-build
YAML example: Save and restore cache steps
Here's an example of a pipeline with Save Cache to S3 and Restore Cache from S3 steps.
steps:
- step:
type: RestoreCacheS3
name: Restore Cache From S3
identifier: Restore_Cache_From_S3
spec:
connectorRef: AWS_Connector
region: us-east-1
bucket: your-s3-bucket
key: cache-{{ checksum "go.sum" }}
archiveFormat: Tar
- step:
type: Run
...
- step:
type: BuildAndPushDockerRegistry
...
- step:
type: SaveCacheS3
name: Save Cache to S3
identifier: Save_Cache_to_S3
spec:
connectorRef: AWS_Connector
region: us-east-1
bucket: your-s3-bucket
key: cache-{{ checksum "go.sum" }}
sourcePaths:
- /go/pkg/mod
- /root/.cache/go-build
archiveFormat: Tar
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.
- Harness Cloud
- Self-hosted
- step:
type: Run
identifier: test
name: Test
spec:
shell: Sh
command: |-
go install github.com/jstemmer/go-junit-report/v2@latest
go test -v ./... | tee report.out
cat report.out | $HOME/go/bin/go-junit-report -set-exit-code > report.xml
reports:
type: JUnit
spec:
paths:
- report.xml
- step:
type: Run
identifier: test
name: Test
spec:
connectorRef: account.harnessImage
image: golang
command: |-
go install github.com/jstemmer/go-junit-report/v2@latest
go test -v ./... | tee report.out
cat report.out | $GOPATH/bin/go-junit-report -set-exit-code > report.xml
reports:
type: JUnit
spec:
paths:
- report.xml
Specify version
- Harness Cloud
- Self-hosted
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 GOPATH=$HOME/go
go install golang.org/dl/go1.20@latest
$GOPATH/bin/go1.20 download
Install multiple versions of Go
- 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"
- Reference the matrix variable in your steps.
- step:
type: Run
identifier: installgo
name: Install Go
spec:
shell: Sh
command: |-
export GOPATH=$HOME/go
go install golang.org/dl/go<+matrix.goVersion>@latest
$GOPATH/bin/go<+matrix.goVersion> download
Specify the desired Golang Docker image tag in your steps. There is no need for a separate install step when using Docker.
Build using a specific version of Go
- step:
type: Run
identifier: build
name: Build
spec:
connectorRef: account.harnessImage
# use version 1.20 of Go
image: golang:1.20
command: |-
go build
Build using multiple versions of Go
- 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"
- Reference the matrix variable in the
image
field of your steps.
- step:
type: Run
identifier: build
name: Build
spec:
connectorRef: account.harnessImage
image: golang:<+matrix.goVersion>
command: |-
go build
Full pipeline examples
Full pipeline examples based on the steps above.
- Harness Cloud
- Self-hosted
Replace the bracketed values with corresponding 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 GOPATH=$HOME/go
go install golang.org/dl/go1.20@latest
$GOPATH/bin/go1.20 download
- step:
type: Run
identifier: build
name: Build
spec:
shell: Sh
command: |-
export GOPATH=$HOME/go
$GOPATH/bin/go1.20 build
- step:
type: Run
identifier: test
name: Test
spec:
shell: Sh
command: |-
export GOPATH=$HOME/go
$GOPATH/bin/go1.20 install github.com/jstemmer/go-junit-report/v2@latest
$GOPATH/bin/go1.20 test -v | tee report.out
cat report.out | $GOPATH/bin/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] # replace with your connector ID
repoName: [your-repository-name] # replace with your repository 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 GOPATH=$HOME/go
go install golang.org/dl/go<+matrix.goVersion>@latest
$GOPATH/bin/go<+matrix.goVersion> download
- step:
type: Run
identifier: build
name: Build
spec:
shell: Sh
command: |-
export GOPATH=$HOME/go
$GOPATH/bin/go<+matrix.goVersion> build
- step:
type: Run
name: Test
identifier: test
spec:
shell: Sh
command: |-
export GOPATH=$HOME/go
$GOPATH/bin/go<+matrix.goVersion> install github.com/jstemmer/go-junit-report/v2@latest
$GOPATH/bin/go<+matrix.goVersion> test -v ./... | tee report_<+matrix.goVersion>.out
cat report_<+matrix.goVersion>.out | $GOPATH/bin/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] # replace with your connector ID
repoName: [your-repository-name] # replace with your repository name
build: <+input>
Replace the bracketed values with corresponding values for your code repo connector, kubernetes cluster connector, kubernetes namespace, 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, 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:
cloneCodebase: true
execution:
steps:
- step:
type: Run
identifier: build
name: Build
spec:
connectorRef: account.harnessImage
image: golang:1.20
command: |-
go build
- step:
type: Run
identifier: test
name: Test
spec:
connectorRef: account.harnessImage
image: golang:1.20
command: |-
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
infrastructure:
spec:
connectorRef: [your-kube-connector-ID] # replace with your connector ID
namespace: [your-kube-namespace] # replace with your namespace
type: KubernetesDirect
platform:
arch: Amd64
os: Linux
properties:
ci:
codebase:
connectorRef: [your-code-repo-connector-ID] # replace with your connector ID
repoName: [your-repository-name] # replace with your repository name
build: <+input>
Pipeline with multiple Go versions
Here is a single-stage pipeline, 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:
cloneCodebase: true
execution:
steps:
- step:
type: Run
identifier: build
name: Build
spec:
connectorRef: account.harnessImage
image: golang:<+matrix.goVersion>
command: |-
go build
- step:
type: Run
name: Test
identifier: test
spec:
connectorRef: account.harnessImage
image: golang:<+matrix.goVersion>
command: |-
go install github.com/jstemmer/go-junit-report/v2@latest
go test -v ./... | tee report_<+matrix.goVersion>.out
cat report_<+matrix.goVersion>.out | $GOPATH/bin/go-junit-report -set-exit-code > report_<+matrix.goVersion>.xml
reports:
type: JUnit
spec:
paths:
- report_*.xml
infrastructure:
spec:
connectorRef: [your-kube-connector-ID] # replace with your connector ID
namespace: [your-kube-namespace] # replace with your namespace
type: KubernetesDirect
platform:
arch: Amd64
os: Linux
properties:
ci:
codebase:
connectorRef: [your-code-repo-connector-ID] # replace with your connector ID
repoName: [your-repository-name] # replace with your repository name
build: <+input>
Next steps
Now that you have created a pipeline that builds and tests a Go app, you could:
- Create triggers to automatically run your pipeline.
- Add steps to build and upload artifacts.
- Add a step to build and push an image to a Docker registry.