Skip to content

Go

Create workflows that builds and tests Golang based applications.

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.

Building executable

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

tasks:
  build-binary:
    runner: razorci/golang
    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: [vendor]

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/[email protected]
      - 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]