Skip to content

Commit f188316

Browse files
authored
Use a single shared cache for all CI workflows (#1506)
1 parent fc4f203 commit f188316

File tree

4 files changed

+149
-32
lines changed

4 files changed

+149
-32
lines changed

.github/actions/setup-go/action.yml

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ inputs:
55
go-version:
66
description: Go version range to set up.
77
default: '>=1.24.0'
8-
cache-name:
9-
description: Name of scoped cache for this set up.
10-
default: 'cache'
8+
create:
9+
description: Create the cache
10+
default: 'false'
11+
lint-cache:
12+
description: Restore the golangci-lint cache
13+
default: 'false'
1114

1215
runs:
1316
using: composite
17+
1418
steps:
1519
- name: Install Go
1620
id: install-go
@@ -19,18 +23,101 @@ runs:
1923
go-version: ${{ inputs.go-version }}
2024
cache: false
2125

22-
# There is more code downloaded and built than is covered by '**/go.sum',
23-
# so give each job its own cache to try and not end up sharing the wrong
24-
# cache between jobs, and hash the Herebyfile and golancgi-lint version.
26+
# Avoid hardcoding the cache keys more than once.
27+
- name: Get cache info
28+
shell: bash
29+
id: cache-info
30+
env:
31+
MODULES_KEY: go-modules-${{ runner.os }}-${{ steps.install-go.outputs.go-version }}-${{ hashFiles('**/go.sum', '**/Herebyfile.mjs', '**/.custom-gcl.yml', '**/.dprint.jsonc') }}
32+
LINT_KEY: golangci-lint-${{ runner.os }}-${{ steps.install-go.outputs.go-version }}-${{ hashFiles('**/go.sum', '**/Herebyfile.mjs', '**/.custom-gcl.yml', '**/.dprint.jsonc') }}
33+
BUILD_KEY: go-build-cache-${{ runner.os }}-${{ steps.install-go.outputs.go-version }}
34+
run: |
35+
echo "modules-key=$MODULES_KEY" >> $GITHUB_OUTPUT
36+
echo "lint-key=$LINT_KEY" >> $GITHUB_OUTPUT
37+
echo "build-key=$BUILD_KEY" >> $GITHUB_OUTPUT
38+
echo "GOLANGCI_LINT_CACHE=$RUNNER_TEMP/golangci-lint-cache" >> $GITHUB_ENV
39+
40+
- if: ${{ inputs.create != 'true' }}
41+
name: Restore Go modules
42+
uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
43+
with:
44+
key: unused-key-${{ github.run_id }}
45+
restore-keys: ${{ steps.cache-info.outputs.modules-key }}-
46+
path: |
47+
~/go/pkg/mod
48+
49+
- if: ${{ inputs.create != 'true' }}
50+
name: Restore Go build cache
51+
uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
52+
with:
53+
key: unused-key-${{ github.run_id }}
54+
restore-keys: ${{ steps.cache-info.outputs.build-key }}-
55+
path: |
56+
~/.cache/go-build
57+
~/Library/Caches/go-build
58+
~/AppData/Local/go-build
59+
60+
- if: ${{ inputs.create != 'true' && inputs.lint-cache == 'true' }}
61+
name: Restore golangci-lint cache
62+
uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
63+
with:
64+
key: unused-key-${{ github.run_id }}
65+
restore-keys: ${{ steps.cache-info.outputs.lint-key }}-
66+
path: ${{ env.GOLANGCI_LINT_CACHE }}
67+
68+
- name: Set mtimes
69+
shell: bash
70+
run: |
71+
find . -type f ! -path ./.git/\*\* | go run github.com/slsyy/mtimehash/cmd/[email protected] || true
72+
find . -type d ! -path ./.git/\*\* -exec touch -d '1970-01-01T00:00:01Z' {} + || true
73+
74+
# All steps below are only run if the cache is being created.
75+
76+
- if: ${{ inputs.create == 'true' }}
77+
shell: bash
78+
run: npm ci
79+
80+
- if: ${{ inputs.create == 'true' }}
81+
shell: bash
82+
run: npx hereby build
83+
84+
- if: ${{ inputs.create == 'true' }}
85+
shell: bash
86+
run: npx hereby test
87+
88+
- if: ${{ inputs.create == 'true' }}
89+
shell: bash
90+
run: npx hereby lint
91+
92+
- if: ${{ inputs.create == 'true' }}
93+
shell: bash
94+
run: npx hereby lint --noembed
2595

26-
- name: Go cache
27-
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
96+
- if: ${{ inputs.create == 'true' }}
97+
shell: bash
98+
run: npx dprint check
99+
100+
- if: ${{ inputs.create == 'true' }}
101+
name: Save Go modules
102+
uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
28103
with:
29-
key: ts-setup-go-${{ runner.os }}-${{ steps.install-go.outputs.go-version }}-${{ hashFiles('**/go.sum', '**/Herebyfile.mjs', '**/.custom-gcl.yml') }}-${{ github.workflow }}-${{ inputs.cache-name }}
30-
restore-keys: |
31-
ts-setup-go-${{ runner.os }}-${{ steps.install-go.outputs.go-version }}-${{ hashFiles('**/go.sum', '**/Herebyfile.mjs', '**/.custom-gcl.yml') }}-${{ github.workflow }}-
104+
key: ${{ steps.cache-info.outputs.modules-key }}
32105
path: |
33106
~/go/pkg/mod
107+
108+
- if: ${{ inputs.create == 'true' }}
109+
name: Save Go build cache
110+
uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
111+
with:
112+
key: ${{ steps.cache-info.outputs.build-key }}-${{ github.run_id }}
113+
path: |
34114
~/.cache/go-build
35115
~/Library/Caches/go-build
36116
~/AppData/Local/go-build
117+
118+
- if: ${{ inputs.create == 'true' }}
119+
name: Save golangci-lint cache
120+
uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
121+
with:
122+
key: ${{ steps.cache-info.outputs.lint-key }}-${{ github.run_id }}
123+
path: ${{ env.GOLANGCI_LINT_CACHE }}

.github/workflows/ci.yml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ jobs:
2828
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
2929
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
3030
- uses: ./.github/actions/setup-go
31-
with:
32-
cache-name: build
3331

3432
# Avoid duplicate PR annotations.
3533
- name: Disable PR annotations
@@ -112,8 +110,6 @@ jobs:
112110
node-version: 'lts/*'
113111
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
114112
- uses: ./.github/actions/setup-go
115-
with:
116-
cache-name: test
117113

118114
# Avoid duplicate PR annotations.
119115
- if: ${{ ! matrix.config.main }}
@@ -200,7 +196,7 @@ jobs:
200196
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
201197
- uses: ./.github/actions/setup-go
202198
with:
203-
cache-name: lint${{ (matrix.config.noembed && '-noembed') || ''}}
199+
lint-cache: 'true'
204200

205201
# Avoid duplicate PR annotations.
206202
- if: ${{ ! matrix.config.main }}
@@ -222,8 +218,6 @@ jobs:
222218
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
223219
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
224220
- uses: ./.github/actions/setup-go
225-
with:
226-
cache-name: format
227221

228222
- run: npm ci
229223

@@ -240,8 +234,6 @@ jobs:
240234
node-version: '>=22.16.0'
241235
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
242236
- uses: ./.github/actions/setup-go
243-
with:
244-
cache-name: generate
245237

246238
- run: npm ci
247239

@@ -261,8 +253,6 @@ jobs:
261253
steps:
262254
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
263255
- uses: ./.github/actions/setup-go
264-
with:
265-
cache-name: tidy
266256

267257
- run: go mod tidy -diff
268258
- run: go -C ./_tools mod tidy -diff
@@ -276,8 +266,6 @@ jobs:
276266
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
277267
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
278268
- uses: ./.github/actions/setup-go
279-
with:
280-
cache-name: smoke
281269

282270
# Avoid duplicate PR annotations.
283271
- name: Disable PR annotations
@@ -302,8 +290,6 @@ jobs:
302290
steps:
303291
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
304292
- uses: ./.github/actions/setup-go
305-
with:
306-
cache-name: misc
307293

308294
- run: go -C ./_tools run ./cmd/checkmodpaths $PWD
309295

@@ -316,8 +302,6 @@ jobs:
316302
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
317303
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
318304
- uses: ./.github/actions/setup-go
319-
with:
320-
cache-name: baselines
321305

322306
- run: npm ci
323307

.github/workflows/copilot-setup-steps.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ jobs:
2525
with:
2626
# Updated to 1.25.0-rc.1 to improve compilation time
2727
go-version: '>=1.25.0-rc.1'
28-
cache-name: copilot-setup-steps
28+
lint-cache: 'true'
2929
- run: npm i -g @playwright/[email protected]
3030
- run: npm ci
3131
# pull dprint caches before network access is blocked
3232
- run: npx hereby check:format || true
33-
# cache build and lint operations
34-
- run: npx hereby build || true
35-
- run: npx hereby lint || true

.github/workflows/create-cache.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Create CI cache
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
schedule:
9+
# Run every day at 10:00 UTC / 03:00 PST
10+
- cron: '0 10 * * *'
11+
12+
permissions:
13+
contents: read
14+
15+
# Ensure scripts are run with pipefail. See:
16+
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#exit-codes-and-error-action-preference
17+
defaults:
18+
run:
19+
shell: bash
20+
21+
jobs:
22+
cache:
23+
strategy:
24+
fail-fast: false
25+
matrix:
26+
os:
27+
- ubuntu-latest
28+
- windows-latest
29+
- macos-latest
30+
go-version:
31+
- '>=1.24.0'
32+
33+
include:
34+
# Temporary for the Copilot setup steps
35+
- os: ubuntu-latest
36+
go-version: '>=1.25.0-rc.1'
37+
38+
runs-on: ${{ matrix.os }}
39+
40+
steps:
41+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
42+
with:
43+
submodules: true
44+
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
45+
- uses: dtolnay/rust-toolchain@fcf085fcb4b4b8f63f96906cd713eb52181b5ea4 # stable
46+
- uses: ./.github/actions/setup-go
47+
with:
48+
go-version: ${{ matrix.go-version }}
49+
create: 'true'

0 commit comments

Comments
 (0)