Go Checks

go-checks@1.0.0 Template

Verify modules, vet, test with the race detector and build a Go module in one job.

Start from it

flightstack template use go-checks

What it does

The usual Go CI job in one step: go mod verify, go vet, go test -race and go build, each switchable, stopping at the first failure.

Inputs

workingDir string default .
Directory holding go.mod.
packages string default ./...
Package pattern to vet, test and build.
vet boolean default true
Run go vet.
race boolean default true
Run tests with -race. Needs cgo; turn off on agents without a C toolchain.
build boolean default true
Run go build after the tests.
testArgs string
Extra arguments for go test, e.g. "-count=1 -timeout 10m".
goVersion string
Fail early unless go version reports this prefix, e.g. "1.25".
env object
Extra environment variables, e.g. GOFLAGS or GOPRIVATE.

Definition

name: Go Checks
description: >-
  Verify modules, vet, test with the race detector and build a Go module in
  one job.

triggers:
  - type: push
    branches: [main]
  - type: pullRequest

jobs:
  checks:
    name: Vet, test and build
    type: plugin/go-checks
    with:
      packages: ./...

plugin:
  name: go-checks
  displayName: Go Checks
  description: >-
    The usual Go CI job: `go mod verify`, `go vet`, `go test -race` and
    `go build`, each switchable, stopping at the first failure.
  category: quality

  supportedPlatforms:
    - linux
    - macos
    - windows

  parameters:
    type: object
    properties:
      workingDir:
        type: string
        default: "."
        description: Directory holding go.mod.
      packages:
        type: string
        default: ./...
        description: Package pattern to vet, test and build.
      vet:
        type: boolean
        default: true
        description: Run `go vet`.
      race:
        type: boolean
        default: true
        description: Run tests with `-race`. Needs cgo; turn off on agents without a C toolchain.
      build:
        type: boolean
        default: true
        description: Run `go build` after the tests.
      testArgs:
        type: string
        default: ""
        description: Extra arguments for `go test`, e.g. "-count=1 -timeout 10m".
      goVersion:
        type: string
        default: ""
        description: Fail early unless `go version` reports this prefix, e.g. "1.25".
      env:
        type: object
        description: Extra environment variables, e.g. GOFLAGS or GOPRIVATE.

  script: |
    set -euo pipefail
    cd "{{workingDir}}"

    if ! command -v go >/dev/null 2>&1; then
      echo "Go is not installed on this agent." >&2
      exit 1
    fi
    version="$(go env GOVERSION)"
    echo "$version"
    wanted="go{{goVersion}}"
    if [ "$wanted" != "go" ] && [ "${version#"$wanted"}" = "$version" ]; then
      echo "This job wants $wanted but the agent has $version." >&2
      exit 1
    fi

    {{#each env}}
    export {{@key}}="{{this}}"
    {{/each}}

    echo "--- go mod download && verify"
    go mod download
    go mod verify

    if [ "{{vet}}" = "true" ]; then
      echo "--- go vet {{packages}}"
      go vet {{packages}}
    fi

    race=""
    if [ "{{race}}" = "true" ]; then race="-race"; fi
    echo "--- go test $race {{packages}}"
    go test $race {{testArgs}} {{packages}}

    if [ "{{build}}" = "true" ]; then
      echo "--- go build {{packages}}"
      go build {{packages}}
    fi