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.

An image from Notion

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]

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

    - name: Set up Go 1.14
      uses: actions/setup-go@v2
      with:
        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

An image from Notion

Live example is here

https://github.com/evalphobia/aws-sdk-go-wrapper/blob/master/.github/workflows/test_mod_tidy.yml