Repository navigation
Conversation
saveCacheV2 created the archive before reserving the cache key. When another job had already saved the key, the archive was built and then discarded, and the save failed with ReserveCacheError. Look up the exact key and version with GetCacheEntryDownloadURL before creating the archive, and return -1 if an entry already exists. The lookup is best-effort and makes a single attempt; on failure the save proceeds as before. A save still in progress in another job is not detected and fails at reservation as it does today.
There was a problem hiding this comment.
🟡 Changes recommended
The preflight can suppress valid branch-local saves and attempts reads in write-only mode.
2 open issues
What changed in this PR
This PR addresses #2550 by adding a best-effort lookup before saveCacheV2 creates an archive, aiming to avoid redundant compression when a cache already exists.
Changes:
- Skip archiving and reservation on an exact key/version lookup hit.
- Use a single lookup attempt and continue saving if it fails.
- Add tests for existing entries, different-key matches, and lookup failures; leave
saveCacheV1unchanged.
| File | Description |
|---|---|
packages/cache/src/cache.ts |
Adds the pre-archive lookup and early return. |
packages/cache/__tests__/saveCacheV2.test.ts |
Tests skip behavior and fallback save paths. |
Review effort: Balanced
| if (existing.ok && existing.matchedKey === key) { | ||
| core.info(`Cache entry with key ${key} already exists, not saving cache.`) | ||
| return cacheId |
There was a problem hiding this comment.
Correct that the lookup searches all readable scopes, and the response doesn't say which one matched. I think skipping is still right: restore on this branch would already return that default-branch entry for the same key and version, so a branch copy adds nothing for reads. It's the same rule actions/cache applies when restore hits the primary key ("Cache hit occurred on the primary key, not saving cache"), which can also come from the default branch. This applies it at save time. A cross-branch test can't exercise scope client-side beyond mocking matchedKey, which the existing test does. If maintainers want branch-local copies kept, I can make this opt-in via UploadOptions.
The preflight lookup in saveCacheV2 is a read, but ACTIONS_CACHE_MODE write-only does not permit reads, and restoreCache already skips them in that mode. Only run the lookup when isCacheReadable(getCacheMode()) is true, so write-only saves go straight to archiving and reservation.

Fixes: #2550
saveCacheV2created the archive before reserving the cache key. When another job had already saved the key (common with matrix jobs sharing a key), every later job compressed the full cache, then failed withUnable to reserve cache with key <key>, another job may be creating this cache.Changes
GetCacheEntryDownloadURL. If an entry exists, logCache entry with key <key> already exists, not saving cache.and return-1without archiving or reserving.maxAttempts: 1). On failure the save proceeds as before.saveCacheV1is unchanged.