Skip to content
Latchkey

Test and Build workflow (hashicorp/go-memdb)

The Test and Build workflow from hashicorp/go-memdb, explained and optimized by Latchkey.

C

CI health: C - fair

The optimized version below adds run de-duplication, job timeouts.

Source: hashicorp/go-memdb.github/workflows/test-and-build.ymlLicense MPL-2.0View source

What it does

This is the Test and Build workflow from the hashicorp/go-memdb repository, a real project running GitHub Actions. It is shown here with attribution under its MPL-2.0 license.

Below, Latchkey shows a faster, safer version produced by its optimization engine.

The workflow

workflow (.yml)
name: Test and Build

on:
  - push
  - pull_request

permissions:
  contents: read

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
      - name: Setup Go
        uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0
        with:
          go-version-file: go.mod
      - name: Download go modules
        run: go mod download
      - name: Check Formatting
        run: |-
          files=$(go fmt ./...)
          if [ -n "$files" ]; then
            echo "The following file(s) do not conform to go fmt:"
            echo "$files"
            exit 1
          fi
      - name: Vet code
        run: go vet ./...
      - name: Run golangci-lint
        uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5

  go-test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version:
          - '1.23' # named in go.mod
          - 'oldstable'
          - 'stable'
    env:
      TEST_RESULTS_PATH: '/tmp/test-results'
    steps:
      - name: Checkout Code
        uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
      - name: Setup Go
        uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0
        with:
          go-version: ${{ matrix.go-version }}
      - name: Install gotestsum
        uses: autero1/action-gotestsum@7263b9d73912eec65f46337689e59fac865c425f # v2.0.0
        with:
          gotestsum_version: 1.9.0

      - name: Create test directory
        run: mkdir -p "$TEST_RESULTS_PATH"
      - name: Run go tests
        run: |
          gotestsum --format=short-verbose --junitfile "$TEST_RESULTS_PATH/gotestsum-report.xml" -- -p 2 -cover -coverprofile=coverage.out ./...
      - name: Upload and save artifacts
        uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808
        with:
          path: ${{ env.TEST_RESULTS_PATH }}
          name: tests-linux-${{matrix.go-version}}
      - name: Upload coverage report
        uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808
        with:
          path: coverage.out
          name: Coverage-report-${{matrix.go-version}}
      - name: Display coverage report
        run: go tool cover -func=coverage.out

The same workflow, on Latchkey

Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.

name: Test and Build on:  - push  - pull_request permissions:  contents: read concurrency:  group: ${{ github.workflow }}-${{ github.ref }}  cancel-in-progress: true jobs:  lint:    timeout-minutes: 30    runs-on: latchkey-small    steps:      - name: Checkout Code        uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0      - name: Setup Go        uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0        with:          go-version-file: go.mod      - name: Download go modules        run: go mod download      - name: Check Formatting        run: |-          files=$(go fmt ./...)          if [ -n "$files" ]; then            echo "The following file(s) do not conform to go fmt:"            echo "$files"            exit 1          fi      - name: Vet code        run: go vet ./...      - name: Run golangci-lint        uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5   go-test:    timeout-minutes: 30    runs-on: latchkey-small    strategy:      matrix:        go-version:          - '1.23' # named in go.mod          - 'oldstable'          - 'stable'    env:      TEST_RESULTS_PATH: '/tmp/test-results'    steps:      - name: Checkout Code        uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0      - name: Setup Go        uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0        with:          go-version: ${{ matrix.go-version }}      - name: Install gotestsum        uses: autero1/action-gotestsum@7263b9d73912eec65f46337689e59fac865c425f # v2.0.0        with:          gotestsum_version: 1.9.0       - name: Create test directory        run: mkdir -p "$TEST_RESULTS_PATH"      - name: Run go tests        run: |          gotestsum --format=short-verbose --junitfile "$TEST_RESULTS_PATH/gotestsum-report.xml" -- -p 2 -cover -coverprofile=coverage.out ./...      - name: Upload and save artifacts        uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808        with:          path: ${{ env.TEST_RESULTS_PATH }}          name: tests-linux-${{matrix.go-version}}      - name: Upload coverage report        uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808        with:          path: coverage.out          name: Coverage-report-${{matrix.go-version}}      - name: Display coverage report        run: go tool cover -func=coverage.out 

What changed

  • Run on Latchkey managed runners with one line (runs-on), which apply the fixes below automatically and self-heal transient failures. This example uses latchkey-small; pick the runner size that fits the job.
  • Cancel superseded runs when a branch or PR gets a newer push.
  • Add a job timeout so a hung step cannot burn hours of runner time.

What Latchkey heals here

This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:

  • Dependency installs

This workflow runs 2 jobs (4 with the matrix expanded) per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow

Frequently asked questions

What does the Test and Build workflow (hashicorp/go-memdb) workflow do?
This is the Test and Build workflow from the hashicorp/go-memdb repository, a real project running GitHub Actions. It is shown here with attribution under its MPL-2.0 license.
What CI health grade does this workflow get?
This Go workflow grades C. Paste your own workflow into the Latchkey grader to see its grade and the exact fixes.
How can I improve this Go workflow?
Apply run de-duplication, job timeouts. Latchkey applies these automatically on managed runners when you point runs-on at Latchkey.

References