A GitHub action for using the latest released Go version and your projects minimal support Go version (from go.mod), and a build matrix of them, and all versions in between.
Being consistent is hard.
I used to hard-code the Go versions my projects needed for test and builds in my GitHub Actions workflow files.
Of course, the result was that I used different versions in the
go.mod file and my workflow files.
Whenever a new version of Go was released, I forgot to add the new version to my build matrix and my projects weren't tested on the new release(s).
So, I build this action.
The action reads the minimal supported Go version from your go.mod
file and exposes it as a variable to your workflow.
It also pulls the list of release tags from https://go.dev/dl/ and exposes the latest released Go version as a variable as well.
From the list of released Go versions and the minimal version, your module supports we also build a "matrix" variable to be used as a build matrix.
While we are at it, we also extract the module path from the go.mod
file, even though it hasn't really anything to do with versions ;)
If your go mod file is located in a non-standard location, you can
specify the working directory where it is located:
working-directory:
description: Working directory where your go.mod file is locatedrequired: falsedefault: .unstable:
description: Include unstable versions of Go (beta, release candidates)required: falsedefault: 'false'unsupported:
description: Include unsupported versions of Gorequired: falsedefault: 'true'patch-level:
description: Include the patch levels on the versions (default is major.minor)required: falsedefault: 'false'latest-patches-only:
description: When patch-level is true, only include the latest patch version of each major.minor version in the matrix. Does nothing if patch-level is false. Cannot be used with unstable.required: falsedefault: 'false'strict-semver:
default: Use strict semver version. E.g. `1.16` -> `1.16.0` and `1.18beta2` -> `1.18.0-beta.2`.required: falsedefault: 'false'go-mod-version:
description: The Go version specified by go.modlatest:
description: The latest Go versionminimal:
description: The minimal Go versionmatrix:
description: A (stringified) array of Go versions from the minimal supported version to the latest released versionmodule:
description: The Go module path (as specified by go.mod)Let's say your go.mod specifies Go 1.13 as the minimal supported
version and you want your workflow to set up Go version 1.13 using the
actions/setup-go action:
name: My Go workflowon: pull_requestjobs:
my-go-workflow:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v3
- uses: arnested/go-version-action@v2id: go-version
- name: Install Go ${{ steps.go-version.outputs.minimal }}uses: actions/setup-go@v3with:
go-version: ${{ steps.go-version.outputs.minimal }}check-latest: trueIf you want do a matrix test of all Go versions from your minimally supported version up to the latest released version we need to do a bit more.
We have to run the version lookup as a separate job and let the test job depend on it:
on: pushname: Testjobs:
go-versions:
name: Lookup Go versionsruns-on: ubuntu-latestoutputs:
matrix: ${{ steps.versions.outputs.matrix }}steps:
- uses: actions/checkout@v3
- uses: arnested/go-version-action@v2id: versionstest:
name: Testruns-on: ubuntu-latestneeds: go-versionsstrategy:
matrix:
version: ${{ fromJSON(needs.go-versions.outputs.matrix) }}steps:
- uses: actions/checkout@v3
- name: Install Gouses: actions/setup-go@v3with:
go-version: ${{ matrix.version }}check-latest: true
- name: Go testrun: go test -v -race -cover -covermode=atomic -coverprofile=coverage.txt ./...The action writes a GitHub Actions Job Summary with values it identified.
This example is from running with:
unsupported: falsepatch-level: true

