Skip to content

Go

This guide provides a walkthrough for a Golang based application and how to configure common CI tasks on Razorops platform.

Demo project

To get started quickly, you can refer to our sample golang based pipeline configuration here.

The most simple pipeline that you can create is to fetch the code, download the Go modules and run go commands to build and test. If your CI tests pass, you may want to deploy your code or archive the executable.

Choosing Golang version

Usually the first step before writing CI spec is to decide the language runtime and support for the version compatible with your application. We support most active versions of Golang. You have the following options if you want to pin-down a version of Golang -

  • For Docker runner environment, you can specify a container image (razorci/golang with tag.
  • For Linux-VM environment, you can change the version (or install new version if not available) using g or govm.

Selecting Golang version in Linux-VM environment

In Linux-VM build environment, we use g to manage the various Golang versions. You can find currently supported versions in toolbox reference. You can also install a different version using rbenv install <version> command if it's not supported by us.

Here are few commands to use, switch and install a version -

    # part of .razorops.yaml file
    steps:
    - commands:
      # list installed versions
      - govm versions
        # 1.22.8
        # 1.23.2

      - go version

      - govm set 1.23.2
      - go version
      # go version go1.23.2 linux/amd64

      # install golang 1.21.13
      - govm install 1.21.13

      - govm set 1.21.13
      - go version
      # go version go1.21.13 linux/amd64

Note

Available golang versions pre-installed via g - 1.22.8 (default) and 1.23.2

Building executable

To get started quickly, add .razorops.yaml file at the root of your repository. This guide uses a customized docker image razorci/golang which has common dependencies installed already.

tasks:
  build-binary:
    runner: razorci/golang:1.22
    variables:
    - CGO_ENABLED=0
    - GO111MODULE=on
    steps:
    - checkout

    - cache/pull:
        key: go-mod-{{ checksum "go.sum" }}

    - comamnds:
        - go mod download -json
        - go build -ldflags "-X main.Version={{ .Env.CI_COMMIT_SHA }}" ./cmd

    - cache/push:
        key: go-mod-{{ checksum "go.sum" }}
        paths: [$GOPATH/pkg/mod]

Once you run this pipeline, Razorops will create an executable for your Golang application. You can use any other version of Go and dependency tool (dep or godep) as well.

Caching modules is a good way to speedup build process in subsequent runs using cache steps. For more details, see "Caching dependencies".

Note

If your code are using private Go module(s) hosted on the same git provider (Github/Gitlab/Bitbucket) as the pipeline, no extra steps are required.

Running tests and generate report

You can use the same commands that you use locally with run property in steps and combine with various lint and coverage tools to generate reports. For example -

tasks:
  build-binary:
    steps:
    ...
    # execute tests with coverage reports
    - commands:
      - go test -cover -html=coverage.out ./...
      - mkdir -p coverage
      - go tool cover -html=coverage.out -o coverage/index.html

    # upload report
    - reports/html: coverage/index.html
tasks:
  build-binary:
    steps:
    ...
    # execute tests with junit reports
    - commands:
      - go install github.com/jstemmer/go-junit-report@latest
      - go test -v 2>&1 | $GOPATH/bin/go-junit-report -set-exit-code > report.xml

    # upload report
    - reports/junit:
      - report.xml

You can checkout the reports for each workflow run in the Dashboard.

Publish artifacts

You can upload artifacts to view after a workflow completes. For example, you may need to save binaries, core dumps, test results, or screenshots. For more information, see "Persisting workflow data using artifacts."

The below example demonstrates how you can use the artifacts/push step to archive test results from running go build. For more information, see the artifacts/push step.

tasks:
  unit-test:
    steps:
    ...
    - commands:
      # for example - test: CGO_ENABLED=0 go build -o dist/server-$GOOS-$GOARCH ./cmd/server in Makefile
      - make test
    ...
    - artifacts/push:
        name: server-linux
        paths: [dest/server-linux-amd64]