Repository navigation
fix!: include runid in cache key, restore freshest lockfile match #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
67d91ee
include runid in cache key, restore freshest lockfile match
poulet42 641dc61
dedupe save keys across re-runs, extract restore/cache-hit logic for …
poulet42 e21921b
fix(cache): give each action invocation a unique save key
zkochan f10939f
style(cache): remove redundant behavior comments
zkochan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import assert from 'node:assert/strict' | ||
| import os from 'node:os' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { beforeEach, test } from 'node:test' | ||
| import { build } from 'esbuild' | ||
| import { getCacheKeyPrefix } from './keys.ts' | ||
|
|
||
| const mocks = { | ||
| '@actions/cache': ` | ||
| import { mock } from 'node:test' | ||
| export const restoreCache = mock.fn(async () => undefined) | ||
| export const saveCache = mock.fn(async () => 1) | ||
| `, | ||
| '@actions/core': ` | ||
| export const state = new Map() | ||
| export const outputs = new Map() | ||
| export const saveState = (key, value) => state.set(key, value) | ||
| export const getState = key => state.get(key) ?? '' | ||
| export const setOutput = (key, value) => outputs.set(key, value) | ||
| export const debug = () => {} | ||
| export const info = () => {} | ||
| `, | ||
| '@actions/exec': String.raw`export const getExecOutput = async () => ({ stdout: '/pnpm-store\n' })`, | ||
| '@actions/glob': `export const hashFiles = async () => 'lockfile-hash'`, | ||
| '../lockfile-verification-cache': `export const restoreVerificationCache = async () => {}`, | ||
| } | ||
| const bundle = await build({ | ||
| stdin: { | ||
| contents: ` | ||
| export { runRestoreCache, finalizeCache } from './run.ts' | ||
| export { runSaveCache } from '../cache-save/run.ts' | ||
| export * as cache from '@actions/cache' | ||
| export * as core from '@actions/core' | ||
| `, | ||
| resolveDir: fileURLToPath(new URL('.', import.meta.url)), | ||
| }, | ||
| bundle: true, | ||
| platform: 'node', | ||
| format: 'esm', | ||
| write: false, | ||
| plugins: [{ | ||
| name: 'fake-cache-services', | ||
| setup(builder) { | ||
| builder.onResolve({ filter: /.*/ }, args => { | ||
| if (Object.hasOwn(mocks, args.path)) return { path: args.path, namespace: 'mock' } | ||
| }) | ||
| builder.onLoad({ filter: /.*/, namespace: 'mock' }, args => ({ contents: mocks[args.path] })) | ||
| }, | ||
| }], | ||
| }) | ||
| const { runRestoreCache, finalizeCache, runSaveCache, cache, core } = await import( | ||
| `data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].text).toString('base64')}` | ||
| ) | ||
|
|
||
| const inputs = { cache: true, cacheDependencyPath: 'pnpm-lock.yaml' } | ||
| const runtimes = [{ name: 'node', version: '24.19.0' }] | ||
| const keyPrefix = getCacheKeyPrefix(process.env.RUNNER_OS, os.arch(), runtimes) | ||
| const lockfileKeyPrefix = `${keyPrefix}lockfile-hash-` | ||
|
|
||
| beforeEach(() => { | ||
| core.state.clear() | ||
| core.outputs.clear() | ||
| cache.restoreCache.mock.resetCalls() | ||
| cache.restoreCache.mock.mockImplementation(async () => undefined) | ||
| cache.saveCache.mock.resetCalls() | ||
| }) | ||
|
|
||
| test('restore asks for the current lockfile before the broader runtime fallback', async () => { | ||
| await runRestoreCache(inputs, runtimes) | ||
| assert.deepEqual(cache.restoreCache.mock.calls[0].arguments, [ | ||
| ['/pnpm-store'], lockfileKeyPrefix, [lockfileKeyPrefix, keyPrefix], | ||
| ]) | ||
| }) | ||
|
|
||
| for (const [label, restoredKey, expectedHit] of [ | ||
| ['same lockfile', `${lockfileKeyPrefix}previous-invocation`, true], | ||
| ['different lockfile', `${keyPrefix}other-lockfile-previous-invocation`, false], | ||
| ['cache miss', undefined, false], | ||
| ]) { | ||
| test(`${label}: reports cache-hit and publishes a fresh store`, async () => { | ||
| cache.restoreCache.mock.mockImplementation(async () => restoredKey) | ||
| const restored = await runRestoreCache(inputs, runtimes) | ||
| finalizeCache(restored, runtimes) | ||
| assert.equal(core.outputs.get('cache-hit'), expectedHit) | ||
|
|
||
| await runSaveCache() | ||
| const primaryKey = core.state.get('cache_primary_key') | ||
| assert.ok(primaryKey.startsWith(lockfileKeyPrefix)) | ||
| assert.notEqual(primaryKey, restoredKey) | ||
| assert.deepEqual(cache.saveCache.mock.calls[0].arguments, [['/pnpm-store'], primaryKey]) | ||
| }) | ||
| } | ||
|
|
||
| test('a failure before finalization does not save the restored store', async () => { | ||
| await runRestoreCache(inputs, runtimes) | ||
| await runSaveCache() | ||
| assert.equal(cache.saveCache.mock.callCount(), 0) | ||
| }) | ||
|
|
||
| test('equivalent invocations in one workflow attempt publish distinct save keys', async t => { | ||
| const previous = [process.env.GITHUB_RUN_ID, process.env.GITHUB_RUN_ATTEMPT] | ||
| t.after(() => { | ||
| for (const [index, name] of ['GITHUB_RUN_ID', 'GITHUB_RUN_ATTEMPT'].entries()) { | ||
| if (previous[index] === undefined) delete process.env[name] | ||
| else process.env[name] = previous[index] | ||
| } | ||
| }) | ||
| process.env.GITHUB_RUN_ID = '555' | ||
| process.env.GITHUB_RUN_ATTEMPT = '1' | ||
|
|
||
| for (let invocation = 0; invocation < 2; invocation++) { | ||
| const restored = await runRestoreCache(inputs, runtimes) | ||
| finalizeCache(restored, runtimes) | ||
| await runSaveCache() | ||
| } | ||
| const keys = cache.saveCache.mock.calls.map(call => call.arguments[1]) | ||
| assert.equal(keys.length, 2) | ||
| assert.notEqual(keys[0], keys[1]) | ||
| for (const key of keys) assert.match(key, /-555-1-[0-9a-f-]{36}$/) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.