For the complete documentation index, see llms.txt. This page is also available as Markdown.

Go

Use a CI pipeline to build and test a Go application.

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
- step:
    type: Run
    identifier: dependencies
    name: Dependencies
    spec:
      connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
      image: golang:latest
      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

You can use built-in steps to:

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

Build and run tests

Add Run steps to build and run your tests.

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.

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
Install multiple versions of Go
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in your steps.

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
Build using multiple versions of Go
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in the image field of your steps.

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

If you copy this example, replace the placeholder values with appropriate 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 with multiple Go versions

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

Next steps

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

Last updated

Was this helpful?