Setting up a matrix for GitHub Actions with Go's go.mod and specific versions

Featured image for sharing metadata for article

When you're building a library or set of tooling in Go, you may want to test against different versions of Go to give confidence in the project for both you and your users.

I set about doing this today for oapi-codegen, and found that I wanted to take the following configuration:

name: Build project
on: [ push, pull_request ]
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Check out source code
        uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version-file: 'go.mod'

      # ...

But I also wanted to specify that this should run against Go 1.21, without hardcoding 1.20 and 1.21 here, so I could use the go.mod as the guide for the version.

I came up with the following, somewhat complicated, setup:

name: Build project
on: [ push, pull_request ]
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      # perform matrix testing to give us an earlier insight into issues with different versions of supported major versions of Go
      matrix:
        # strategy is used to allow us to pin to a specific Go version, or use the version available in our `go.mod`
        strategy: ['go-version']
        version: [1.21]
        include:
          # pick up the Go version from the `go.mod`
          - strategy: 'go-version-file'
            version: 'go.mod'
    steps:
      - name: Check out source code
        uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          ${{ matrix.strategy }}: ${{ matrix.version }}

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #github-actions #go.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.