Test Go code with GitHub Actions
suggest changeGitHub Actions can build and test a repository after each push or pull request.
Workflows are YAML files stored in .github/workflows.
Create .github/workflows/go.yml:
name: Go
on:
push:
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version-file: go.mod
cache: true
- name: Test
run: go test ./...
The workflow has one job named test. GitHub runs it in a fresh Ubuntu
environment. The steps check out the repository, install the Go version selected
by the module's go.mod file, restore the module and build caches, and test every
package in the module.
Pin go-version instead of using go-version-file when the repository does not
have a go.mod file. A version matrix is useful when a library supports several
Go releases:
strategy:
matrix:
go-version: ["1.24.x", "1.25.x"]
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}
- run: go test ./...
Put strategy on the job, next to runs-on, not at the top of the file.
The repository's Actions tab and the check attached to each commit show the command output when a job fails. See GitHub's Go workflow documentation for artifact uploads, service containers, and other workflow options.