Skip to content

Commit 23dbd45

Browse files
authored
ci: Use nx cache from master by default (#6732)
Currently, we do not store any cache for the master branch. When we run a PR, it will try to restore the cache from itself, then from the current branch, then just the newest cache it finds at all. This means that on a fresh branch, it will restore the cache from the last action that ran - which could be any branch, which means the cache may incorporate changed we don't have, leading to us having to rebuild the cache. Instead, this makes sure we restore from the master branch first, if possible. This makes it necessary to ensure we actually also _store_ the cache when running on master - we only pass a restore key in that case that will never match anything.
1 parent 8f299b6 commit 23dbd45

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

.github/workflows/build.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ env:
4040
4141
BUILD_CACHE_KEY: ${{ github.event.inputs.commit || github.sha }}
4242

43+
# GH will use the first restore-key it finds that matches
44+
# So it will start by looking for one from the same branch, else take the newest one it can find elsewhere
45+
# We want to prefer the cache from the current master branch, if we don't find any on the current branch
46+
NX_CACHE_RESTORE_KEYS: |
47+
nx-Linux-${{ github.ref }}-${{ github.event.inputs.commit || github.sha }}
48+
nx-Linux-${{ github.ref }}
49+
nx-Linux-refs/heads/master
50+
nx-Linux
51+
4352
jobs:
4453
job_get_metadata:
4554
name: Get Metadata
@@ -194,18 +203,14 @@ jobs:
194203
# - on release branches
195204
# - when PR has `ci-skip-cache` label
196205
if: |
197-
needs.job_get_metadata.outputs.is_master == 'false' &&
198206
needs.job_get_metadata.outputs.is_release == 'false' &&
199207
needs.job_get_metadata.outputs.force_skip_cache == 'false'
200208
with:
201209
path: node_modules/.cache/nx
202-
key: nx-${{ runner.os }}-${{ github.ref }}-${{ env.HEAD_COMMIT }}
203-
# GH will use the first restore-key it finds that matches
204-
# So it will start by looking for one from the same branch, else take the newest one it can find elsewhere
205-
restore-keys: |
206-
nx-${{ runner.os }}-${{ github.ref }}-${{ env.HEAD_COMMIT }}
207-
nx-${{ runner.os }}-${{ github.ref }}
208-
nx-${{ runner.os }}
210+
key: nx-Linux-${{ github.ref }}-${{ env.HEAD_COMMIT }}
211+
# On master branch, we want to _store_ the cache (so it can be used by other branches), but never _restore_ from it
212+
restore-keys:
213+
${{needs.job_get_metadata.outputs.is_master == 'false' && env.NX_CACHE_RESTORE_KEYS || 'nx-never-restore'}}
209214

210215
- name: Build packages
211216
# Under normal circumstances, using the git SHA as a cache key, there shouldn't ever be a cache hit on the built

0 commit comments

Comments
 (0)