11name : Set up Go
22description : >-
3- Installs the Go version from go.mod and caches the module and build caches
4- under a key specific to the calling job.
3+ Installs the Go version from go.mod and restores the module and build caches
4+ under a key specific to the calling job. Only trunk saves the caches.
55
66inputs :
77 cache-key-prefix :
@@ -23,19 +23,39 @@ runs:
2323 go-version-file : go.mod
2424 cache : false
2525
26- - name : Determine Go cache paths
26+ - name : Determine Go cache paths and key
2727 id : go-cache
2828 shell : bash
29+ env :
30+ PREFIX : ${{ inputs.cache-key-prefix }}-${{ runner.os }}-${{ runner.arch }}
31+ GO_MOD_HASH : ${{ hashFiles('go.mod') }}
2932 run : |
33+ restore_prefix="${PREFIX}-${ImageOS}-$(go env GOVERSION)-"
3034 echo "modcache=$(go env GOMODCACHE)" >> "$GITHUB_OUTPUT"
3135 echo "buildcache=$(go env GOCACHE)" >> "$GITHUB_OUTPUT"
32- echo "version=$(go env GOVERSION) " >> "$GITHUB_OUTPUT"
33- echo "image =${ImageOS }" >> "$GITHUB_OUTPUT"
36+ echo "restore-prefix=${restore_prefix} " >> "$GITHUB_OUTPUT"
37+ echo "key =${restore_prefix}${GO_MOD_HASH }" >> "$GITHUB_OUTPUT"
3438
39+ # Only trunk saves, so pull requests don't fill the repository's cache
40+ # storage with entries no other branch can restore. Each cache is roughly
41+ # 300 MB, or 520 MB for -race builds.
3542 - name : Cache Go modules and build artifacts
43+ if : github.ref == 'refs/heads/trunk'
3644 uses : actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5.1.0
3745 with :
3846 path : |
3947 ${{ steps.go-cache.outputs.modcache }}
4048 ${{ steps.go-cache.outputs.buildcache }}
41- key : ${{ inputs.cache-key-prefix }}-${{ runner.os }}-${{ runner.arch }}-${{ steps.go-cache.outputs.image }}-${{ steps.go-cache.outputs.version }}-${{ hashFiles('go.mod') }}
49+ key : ${{ steps.go-cache.outputs.key }}
50+
51+ # Pull requests that change go.mod fall back to the most recent trunk
52+ # cache, which still holds most of the compiled packages they need.
53+ - name : Restore Go modules and build artifacts
54+ if : github.ref != 'refs/heads/trunk'
55+ uses : actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5.1.0
56+ with :
57+ path : |
58+ ${{ steps.go-cache.outputs.modcache }}
59+ ${{ steps.go-cache.outputs.buildcache }}
60+ key : ${{ steps.go-cache.outputs.key }}
61+ restore-keys : ${{ steps.go-cache.outputs.restore-prefix }}
0 commit comments