How to set up `go mod tidy` on GitHub Actions

Posted: May 08, 2020

When you develop with golang in a team, sometimes someone (it’s me!) forget to include go.mod , or forget to run go mod tidy .

Then the diffs will be included in other pull request. (That sucks)

To keep correct versioning and make code (and team member’s emotions) healthy, it’s good to setup go mod tidy on CI.

GitHub PullRequest Checks

It’s quite easy, run go mod tidy then git diff --exit-code -- go.mod go.sum

So CI can detect diffs and emit fail signal to GitHub pull request.

This is example of GitHub Actions job.

# .github/workflows/test_mod_tidy.yml

name: tidy

on: [push]

    name: go mod tidy
    runs-on: ubuntu-latest

    - name: Set up Go 1.14
      uses: actions/setup-go@v2
        go-version: 1.14.2
      id: go

    - name: setup env
      run: |
        echo "::set-env name=GOPATH::$(go env GOPATH)"
        echo "::add-path::$(go env GOPATH)/bin"
      shell: bash

    - name: Check out code
      uses: actions/checkout@v2

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

If you did go mod tidy but somehow fail in CI , try these things

  • Use same version on your local environment and CI.
    • Especially, use and set same patch version.
  • Clear module caches on your local environment.
    • go clean -modcache

GitHub Actions result

Live example is here