GitHub Actions CI Pipeline Go
Build a Go CI pipeline with matrix versions, module caching, lint gates, race-enabled tests, and release artifact uploads.
Code
// .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/Report a problem with this exampleOpens a pre-filled issue on GitHub.