hammerkit

go

Build and test a Go module, with the module cache kept on the host.

Build and test a Go module, with the module cache kept on the host. build compiles the packages into bin, test runs their tests. Both are skipped while the .go files and the go.* files are unchanged, and downloaded modules are kept in ~/go/pkg/mod, so they are not fetched again.

.hammerkit.yaml
includes:
  go:
    git: https://github.com/no0dles/hammerkit.git
    ref: v1.19.0
    path: catalog/go
    with:
      GO_VERSION: '1.25'

tasks:
  check:
    deps: [go:test]

Tasks

TaskWhat it doesReadsProduces
buildBuild the packages into bin.go.mod, go.sum and go.work where present, every .go filebin
testRun the tests of the packages.go.mod, go.sum and go.work where present, every .go file

Inputs

InputDefaultMeaning
GO_VERSION1.25Tag of the golang image the tasks run in. The image is golang:<GO_VERSION>-alpine. Quote it in with ('1.30'), or YAML reads it as the number 1.3.
PACKAGES./...The packages to build and test.

The entry

catalog/go/build.yaml
# Builds and tests a Go module with the module cache kept on the host.
envs:
  # tag of the golang image the tasks run in, quoted so 1.30 stays 1.30
  GO_VERSION: '1.25'
  # the packages to build and test
  PACKAGES: ./...

tasks:
  build:
    description: build the packages into bin
    image: golang:${GO_VERSION}-alpine
    src:
      # go.mod, and go.sum, go.work and go.work.sum where the module has them
      - 'go.*'
      - '**/*.go'
    generates: [bin]
    mounts:
      - ~/go/pkg/mod:/go/pkg/mod
    cmds:
      - mkdir -p bin
      - go build -o bin/ ${PACKAGES}

  test:
    description: run the tests of the packages
    image: golang:${GO_VERSION}-alpine
    deps: [build]
    src:
      # go.mod, and go.sum, go.work and go.work.sum where the module has them
      - 'go.*'
      - '**/*.go'
    mounts:
      - ~/go/pkg/mod:/go/pkg/mod
    cmds:
      - go test ${PACKAGES}

On this page