Skip to content

Commit a00d67d

Browse files
committed
[ci] Centralize cache
To avoid race conditions where multiple jobs try to write to the same cache, we now centralize saving the cache and then reusing it in every subsequent job.
1 parent 5bfc51b commit a00d67d

File tree

1 file changed

+79
-14
lines changed

1 file changed

+79
-14
lines changed

.github/workflows/runtime_build_and_test.yml

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,71 @@ env:
1717
SEGMENT_DOWNLOAD_TIMEOUT_MINS: 1
1818

1919
jobs:
20+
# ----- YARN CACHE -----
21+
# Centralize the yarn/node_modules cache so it is saved once and each subsequent job only needs to
22+
# restore the cache. Prevents race conditions where multiple workflows try to write to the cache.
23+
runtime_yarn_cache:
24+
name: Restore or create yarn/node_modules cache for runtime
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
with:
29+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
30+
- uses: actions/setup-node@v4
31+
with:
32+
node-version-file: '.nvmrc'
33+
cache: yarn
34+
cache-dependency-path: yarn.lock
35+
- name: Get yarn cache directory path
36+
id: yarn-cache-dir-path
37+
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
38+
- uses: actions/cache@v4
39+
id: yarn-cache
40+
with:
41+
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
42+
key: yarn-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
43+
restore-keys: |
44+
yarn-${{ runner.arch }}-${{ runner.os }}-
45+
- name: Restore cached node_modules
46+
uses: actions/cache@v4
47+
id: node_modules
48+
with:
49+
path: |
50+
**/node_modules
51+
key: runtime-node_modules-v6-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('yarn.lock') }}
52+
- run: yarn install --frozen-lockfile
53+
54+
runtime_compiler_yarn_cache:
55+
name: Restore or create yarn/node_modules cache for runtime and compiler
56+
runs-on: ubuntu-latest
57+
steps:
58+
- uses: actions/checkout@v4
59+
with:
60+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
61+
- uses: actions/setup-node@v4
62+
with:
63+
node-version-file: '.nvmrc'
64+
cache: yarn
65+
cache-dependency-path: yarn.lock
66+
- name: Get yarn cache directory path
67+
id: yarn-cache-dir-path
68+
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
69+
- uses: actions/cache@v4
70+
id: yarn-cache
71+
with:
72+
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
73+
key: yarn-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
74+
restore-keys: |
75+
yarn-${{ runner.arch }}-${{ runner.os }}-
76+
- name: Restore cached node_modules
77+
uses: actions/cache@v4
78+
id: node_modules
79+
with:
80+
path: |
81+
**/node_modules
82+
key: runtime-and-compiler-node_modules-v6-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('yarn.lock', 'compiler/yarn.lock') }}
83+
- run: yarn install --frozen-lockfile
84+
2085
# ----- FLOW -----
2186
discover_flow_inline_configs:
2287
name: Discover flow inline configs
@@ -52,7 +117,7 @@ jobs:
52117
cache: yarn
53118
cache-dependency-path: yarn.lock
54119
- name: Restore cached node_modules
55-
uses: actions/cache@v4
120+
uses: actions/cache/restore@v4
56121
id: node_modules
57122
with:
58123
path: |
@@ -77,7 +142,7 @@ jobs:
77142
cache: yarn
78143
cache-dependency-path: yarn.lock
79144
- name: Restore cached node_modules
80-
uses: actions/cache@v4
145+
uses: actions/cache/restore@v4
81146
id: node_modules
82147
with:
83148
path: |
@@ -104,7 +169,7 @@ jobs:
104169
cache: yarn
105170
cache-dependency-path: yarn.lock
106171
- name: Restore cached node_modules
107-
uses: actions/cache@v4
172+
uses: actions/cache/restore@v4
108173
id: node_modules
109174
with:
110175
path: |
@@ -160,7 +225,7 @@ jobs:
160225
yarn.lock
161226
compiler/yarn.lock
162227
- name: Restore cached node_modules
163-
uses: actions/cache@v4
228+
uses: actions/cache/restore@v4
164229
id: node_modules
165230
with:
166231
path: |
@@ -198,7 +263,7 @@ jobs:
198263
distribution: temurin
199264
java-version: 11.0.22
200265
- name: Restore cached node_modules
201-
uses: actions/cache@v4
266+
uses: actions/cache/restore@v4
202267
id: node_modules
203268
with:
204269
path: |
@@ -276,7 +341,7 @@ jobs:
276341
yarn.lock
277342
compiler/yarn.lock
278343
- name: Restore cached node_modules
279-
uses: actions/cache@v4
344+
uses: actions/cache/restore@v4
280345
id: node_modules
281346
with:
282347
path: |
@@ -310,7 +375,7 @@ jobs:
310375
cache: yarn
311376
cache-dependency-path: yarn.lock
312377
- name: Restore cached node_modules
313-
uses: actions/cache@v4
378+
uses: actions/cache/restore@v4
314379
id: node_modules
315380
with:
316381
path: |
@@ -358,7 +423,7 @@ jobs:
358423
cache: yarn
359424
cache-dependency-path: yarn.lock
360425
- name: Restore cached node_modules
361-
uses: actions/cache@v4
426+
uses: actions/cache/restore@v4
362427
id: node_modules
363428
with:
364429
path: |
@@ -394,7 +459,7 @@ jobs:
394459
cache: yarn
395460
cache-dependency-path: yarn.lock
396461
- name: Restore cached node_modules
397-
uses: actions/cache@v4
462+
uses: actions/cache/restore@v4
398463
id: node_modules
399464
with:
400465
path: |
@@ -427,7 +492,7 @@ jobs:
427492
cache: yarn
428493
cache-dependency-path: yarn.lock
429494
- name: Restore cached node_modules
430-
uses: actions/cache@v4
495+
uses: actions/cache@v4 # note: this does not reuse centralized cache since it has unique cache key
431496
id: node_modules
432497
with:
433498
path: |
@@ -470,7 +535,7 @@ jobs:
470535
# That means dependencies of the built packages are not installed.
471536
# We need to install dependencies of the workroot to fulfill all dependency constraints
472537
- name: Restore cached node_modules
473-
uses: actions/cache@v4
538+
uses: actions/cache@v4 # note: this does not reuse centralized cache since it has unique cache key
474539
id: node_modules
475540
with:
476541
path: |
@@ -533,7 +598,7 @@ jobs:
533598
cache: yarn
534599
cache-dependency-path: yarn.lock
535600
- name: Restore cached node_modules
536-
uses: actions/cache@v4
601+
uses: actions/cache/restore@v4
537602
id: node_modules
538603
with:
539604
path: |
@@ -585,7 +650,7 @@ jobs:
585650
cache: yarn
586651
cache-dependency-path: yarn.lock
587652
- name: Restore cached node_modules
588-
uses: actions/cache@v4
653+
uses: actions/cache/restore@v4
589654
id: node_modules
590655
with:
591656
path: |
@@ -623,7 +688,7 @@ jobs:
623688
cache: yarn
624689
cache-dependency-path: yarn.lock
625690
- name: Restore cached node_modules
626-
uses: actions/cache@v4
691+
uses: actions/cache/restore@v4
627692
id: node_modules
628693
with:
629694
path: |

0 commit comments

Comments
 (0)