Skip to content

Commit abf542f

Browse files
committed
ci: keep the Go build cache current
actions/setup-go keys its cache on the Go version and go.sum only. The first job to finish after a new Go release or a go.sum change saves the entry, and every later run hits that key and skips saving. Master CI is still restoring the entries saved on 2026-09-11 (Go 1.27.1), so everything that changed since then recompiles in every run. Replace it with actions/cache restore/save, keyed per OS, architecture, Go version and job. Each run restores the newest cache saved for its key prefix, and runs other than pull requests save a new entry for the commit, so the cache tracks master. Pull requests only restore, which keeps the repository's cache storage bounded. A full race build cache compresses to about 80 MB. Claude-Session: https://claude.ai/code/session_01QKDYQr53zNKkR7nif2CAAq
1 parent af07b69 commit abf542f

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎.github/workflows/checks.yml‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ env:
2121
# https://github.com/actions/setup-go/commit/924ae3a1cded613372ab5595356fb5720e22ba16
2222
SETUP_GO_ACTION: &setup_go_action actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6
2323

24+
# https://github.com/actions/cache/commit/55cc8345863c7cc4c66a329aec7e433d2d1c52a9
25+
CACHE_RESTORE_ACTION: &cache_restore_action actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
26+
CACHE_SAVE_ACTION: &cache_save_action actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
27+
2428
jobs:
2529
check:
2630
runs-on: ubuntu-latest
@@ -33,6 +37,30 @@ jobs:
3337
with:
3438
go-version: ${{ env.LATEST_GO_VERSION }}
3539
check-latest: true
40+
cache: false
41+
42+
# setup-go's built-in cache is keyed on go.sum only, so its entry is never refreshed and
43+
# soon holds little of the current build. Restore the latest cache saved for this job
44+
# instead, and save a new one after runs other than pull requests, which only restore.
45+
- name: Find Go Cache
46+
id: go-cache
47+
shell: bash
48+
run: |
49+
{
50+
echo "build=$(go env GOCACHE)"
51+
echo "mod=$(go env GOMODCACHE)"
52+
echo "key=go-${{ runner.os }}-${{ runner.arch }}-$(go env GOVERSION)-${{ github.job }}"
53+
} >> "$GITHUB_OUTPUT"
54+
55+
- name: Restore Go Cache
56+
id: go-cache-restore
57+
uses: *cache_restore_action
58+
with:
59+
path: |
60+
${{ steps.go-cache.outputs.build }}
61+
${{ steps.go-cache.outputs.mod }}
62+
key: ${{ steps.go-cache.outputs.key }}-${{ github.sha }}
63+
restore-keys: ${{ steps.go-cache.outputs.key }}-
3664

3765
- name: Run golint
3866
run: |
@@ -49,3 +77,12 @@ jobs:
4977
go version
5078
go install golang.org/x/vuln/cmd/govulncheck@latest
5179
govulncheck ./...
80+
81+
- name: Save Go Cache
82+
if: github.event_name != 'pull_request' && steps.go-cache-restore.outputs.cache-hit != 'true'
83+
uses: *cache_save_action
84+
with:
85+
path: |
86+
${{ steps.go-cache.outputs.build }}
87+
${{ steps.go-cache.outputs.mod }}
88+
key: ${{ steps.go-cache.outputs.key }}-${{ github.sha }}

‎.github/workflows/echo.yml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ env:
2121
# https://github.com/actions/setup-go/commit/924ae3a1cded613372ab5595356fb5720e22ba16
2222
SETUP_GO_ACTION: &setup_go_action actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6
2323

24+
# https://github.com/actions/cache/commit/55cc8345863c7cc4c66a329aec7e433d2d1c52a9
25+
CACHE_RESTORE_ACTION: &cache_restore_action actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
26+
CACHE_SAVE_ACTION: &cache_save_action actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
27+
2428
# https://github.com/codecov/codecov-action/commit/fb8b3582c8e4def4969c97caa2f19720cb33a72f
2529
CODECOV_ACTION: &codecov_action codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7
2630

@@ -44,10 +48,46 @@ jobs:
4448
uses: *setup_go_action
4549
with:
4650
go-version: ${{ matrix.go }}
51+
cache: false
52+
53+
# setup-go's built-in cache is keyed on go.sum only, so its entry is never refreshed and
54+
# soon holds little of the current build. Restore the latest cache saved for this job
55+
# instead, and save a new one after runs other than pull requests, which only restore.
56+
- &find_go_cache
57+
name: Find Go Cache
58+
id: go-cache
59+
shell: bash
60+
run: |
61+
{
62+
echo "build=$(go env GOCACHE)"
63+
echo "mod=$(go env GOMODCACHE)"
64+
echo "key=go-${{ runner.os }}-${{ runner.arch }}-$(go env GOVERSION)-${{ github.job }}"
65+
} >> "$GITHUB_OUTPUT"
66+
67+
- &restore_go_cache
68+
name: Restore Go Cache
69+
id: go-cache-restore
70+
uses: *cache_restore_action
71+
with:
72+
path: |
73+
${{ steps.go-cache.outputs.build }}
74+
${{ steps.go-cache.outputs.mod }}
75+
key: ${{ steps.go-cache.outputs.key }}-${{ github.sha }}
76+
restore-keys: ${{ steps.go-cache.outputs.key }}-
4777

4878
- name: Run Tests
4979
run: go test -race --coverprofile=coverage.coverprofile --covermode=atomic ./...
5080

81+
- &save_go_cache
82+
name: Save Go Cache
83+
if: github.event_name != 'pull_request' && steps.go-cache-restore.outputs.cache-hit != 'true'
84+
uses: *cache_save_action
85+
with:
86+
path: |
87+
${{ steps.go-cache.outputs.build }}
88+
${{ steps.go-cache.outputs.mod }}
89+
key: ${{ steps.go-cache.outputs.key }}-${{ github.sha }}
90+
5191
- name: Upload coverage to Codecov
5292
if: success() && matrix.go == env.LATEST_GO_VERSION && matrix.os == 'ubuntu-latest'
5393
uses: *codecov_action
@@ -75,6 +115,11 @@ jobs:
75115
uses: *setup_go_action
76116
with:
77117
go-version: ${{ env.LATEST_GO_VERSION }}
118+
cache: false
119+
120+
- *find_go_cache
121+
122+
- *restore_go_cache
78123

79124
- name: Install Dependencies
80125
run: go install golang.org/x/perf/cmd/benchstat@latest
@@ -92,3 +137,5 @@ jobs:
92137
- name: Run Benchstat
93138
run: |
94139
benchstat previous/benchmark.txt new/benchmark.txt
140+
141+
- *save_go_cache

0 commit comments

Comments
 (0)