GitHub Actions CI Pipeline Go

Build a Go CI pipeline with matrix versions, module caching, lint gates, race-enabled tests, and release artifact uploads.

GoCli Tooling

Code

gosnippet
67 lines
// .github/workflows/ci.yml
name: go-ci

on:
  push:
    branches: [main]
  pull_request:

env:
  GOFLAGS: -trimpath

jobs:
  verify:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go: ['1.22', '1.23']
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go }}
          cache: true
          cache-dependency-path: go.sum

      - name: Check module hygiene
        run: |
          go mod tidy
          git diff --exit-code go.mod go.sum

      - name: Vet
        run: go vet ./...

      - name: Lint
        uses: golangci/golangci-lint-action@v6
        with:
          version: latest

      - name: Test with race detector
        run: go test -race -count=1 -coverprofile=cover.out ./...

      - name: Upload coverage
        if: matrix.go == '1.23'
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: cover.out

  release:
    needs: verify
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: '1.23'
          cache: true
      - name: Build release binaries
        run: |
          CGO_ENABLED=0 go build -ldflags="-s -w" -o dist/tool-linux-amd64 ./cmd/tool
          CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o dist/tool-darwin-arm64 ./cmd/tool
      - uses: actions/upload-artifact@v4
        with:
          name: release-binaries
          path: dist/

Inside the snippet

This is a CI workflow definition rather than application code: 2 jobs on hosted runners, executing 11 steps in total.

It runs on push, pull_request, release events, so every branch push and pull request gets the same checks.

A version matrix fans the job out across several toolchain versions, catching compatibility breaks a single-version run would miss.

Third-party actions in use: actions/checkout, actions/setup-go, golangci/golangci-lint-action, actions/upload-artifact — each is pinned to a tag in the source.

6 steps drop to raw shell commands; the rest delegate to packaged actions.

What to watch for

  • No dependencies section needed; the standard library is the whole story.

Key statements worth quoting

CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o dist/tool-darwin-arm64 ./cmd/tool
CGO_ENABLED=0 go build -ldflags="-s -w" -o dist/tool-linux-amd64 ./cmd/tool
if: startsWith(github.ref, 'refs/tags/v')

Interactive Tools for This Topic

FAQ

What triggers this workflow?
`push`, `pull_request`, `release` events, fanned out across the version matrix. Every matching push or pull request runs the same checks.
Does this workflow depend on third-party actions?
Yes — `actions/checkout`, `actions/setup-go`, `golangci/golangci-lint-action`, `actions/upload-artifact`, each referenced at a pinned tag in the source. Review them before adopting the workflow in a repo with stricter supply-chain rules.