mirror of
https://github.com/actions/setup-node.git
synced 2026-06-29 09:31:40 +00:00
Simplifies cache fallback + add timed invalidation
This commit is contained in:
parent
6a898d25f0
commit
cbe3ec3ea1
@ -63,6 +63,8 @@ The action defaults to search for the dependency file (`package-lock.json`, `npm
|
|||||||
|
|
||||||
**Note:** The action does not cache `node_modules`
|
**Note:** The action does not cache `node_modules`
|
||||||
|
|
||||||
|
Use `cache-invalidate-after-days` to change the default fallback cache invalidation of every 120 days. Set to 0 to deactivate.
|
||||||
|
|
||||||
See the examples of using cache for `yarn`/`pnpm` and `cache-dependency-path` input in the [Advanced usage](docs/advanced-usage.md#caching-packages-data) guide.
|
See the examples of using cache for `yarn`/`pnpm` and `cache-dependency-path` input in the [Advanced usage](docs/advanced-usage.md#caching-packages-data) guide.
|
||||||
|
|
||||||
**Caching npm dependencies:**
|
**Caching npm dependencies:**
|
||||||
|
|||||||
@ -132,13 +132,13 @@ describe('cache-restore', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await restoreCache(packageManager);
|
await restoreCache(packageManager, undefined, '0');
|
||||||
expect(hashFilesSpy).toHaveBeenCalled();
|
expect(hashFilesSpy).toHaveBeenCalled();
|
||||||
expect(infoSpy).toHaveBeenCalledWith(
|
expect(infoSpy).toHaveBeenCalledWith(
|
||||||
`Cache restored from key: ${platform}-setup-node-${packageManager}-${fileHash}`
|
`Cache restored from key: ${platform}-0-setup-node-${packageManager}-${fileHash}`
|
||||||
);
|
);
|
||||||
expect(infoSpy).not.toHaveBeenCalledWith(
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
||||||
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
|
`Cache not found for input keys: ${platform}-0-setup-node-${packageManager}-${fileHash}, ${platform}-0-setup-node-${packageManager}-`
|
||||||
);
|
);
|
||||||
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
|
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
|
||||||
}
|
}
|
||||||
@ -163,10 +163,10 @@ describe('cache-restore', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
restoreCacheSpy.mockImplementationOnce(() => undefined);
|
restoreCacheSpy.mockImplementationOnce(() => undefined);
|
||||||
await restoreCache(packageManager);
|
await restoreCache(packageManager, undefined, '0');
|
||||||
expect(hashFilesSpy).toHaveBeenCalled();
|
expect(hashFilesSpy).toHaveBeenCalled();
|
||||||
expect(infoSpy).toHaveBeenCalledWith(
|
expect(infoSpy).toHaveBeenCalledWith(
|
||||||
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
|
`Cache not found for input keys: ${platform}-0-setup-node-${packageManager}-${fileHash}, ${platform}-0-setup-node-${packageManager}-`
|
||||||
);
|
);
|
||||||
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false);
|
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,8 @@ inputs:
|
|||||||
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
|
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
|
||||||
cache-dependency-path:
|
cache-dependency-path:
|
||||||
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
|
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
|
||||||
|
cache-invalidate-after-days:
|
||||||
|
description: 'Used to control how often the fallback cache is invalidated automatically.'
|
||||||
# TODO: add input to control forcing to pull from cloud or dist.
|
# TODO: add input to control forcing to pull from cloud or dist.
|
||||||
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
|
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
|
||||||
outputs:
|
outputs:
|
||||||
|
|||||||
@ -13,7 +13,8 @@ import {
|
|||||||
|
|
||||||
export const restoreCache = async (
|
export const restoreCache = async (
|
||||||
packageManager: string,
|
packageManager: string,
|
||||||
cacheDependencyPath?: string
|
cacheDependencyPath?: string,
|
||||||
|
cacheInvalidateAfterDays?: string
|
||||||
) => {
|
) => {
|
||||||
const packageManagerInfo = await getPackageManagerInfo(packageManager);
|
const packageManagerInfo = await getPackageManagerInfo(packageManager);
|
||||||
if (!packageManagerInfo) {
|
if (!packageManagerInfo) {
|
||||||
@ -35,9 +36,15 @@ export const restoreCache = async (
|
|||||||
'Some specified paths were not resolved, unable to cache dependencies.'
|
'Some specified paths were not resolved, unable to cache dependencies.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const keyPrefix = `${platform}-setup-node-`;
|
const numericCacheInvalidateAfterDays = cacheInvalidateAfterDays && cacheInvalidateAfterDays === '0'
|
||||||
|
? 0
|
||||||
|
: (parseInt(cacheInvalidateAfterDays || '', 10) || 120)
|
||||||
|
const timedInvalidationPrefix = numericCacheInvalidateAfterDays
|
||||||
|
? Math.floor(Date.now() / (1000 * 60 * 60 * 24 * numericCacheInvalidateAfterDays)) % 1000 // % 1000 to get a rolling prefix between 0 and 999 rather than a possibly infinitely large
|
||||||
|
: 0;
|
||||||
|
const keyPrefix = `${platform}-${timedInvalidationPrefix}-setup-node-`;
|
||||||
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
|
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
|
||||||
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
|
const restoreKeys = [`${keyPrefix}${packageManager}-`];
|
||||||
core.debug(`primary key is ${primaryKey}`);
|
core.debug(`primary key is ${primaryKey}`);
|
||||||
core.saveState(State.CachePrimaryKey, primaryKey);
|
core.saveState(State.CachePrimaryKey, primaryKey);
|
||||||
|
|
||||||
|
|||||||
@ -61,7 +61,8 @@ export async function run() {
|
|||||||
|
|
||||||
if (cache && isCacheFeatureAvailable()) {
|
if (cache && isCacheFeatureAvailable()) {
|
||||||
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
||||||
await restoreCache(cache, cacheDependencyPath);
|
const cacheInvalidateAfterDays = core.getInput('cache-invalidate-after-days');
|
||||||
|
await restoreCache(cache, cacheDependencyPath, cacheInvalidateAfterDays);
|
||||||
}
|
}
|
||||||
|
|
||||||
const matchersPath = path.join(__dirname, '../..', '.github');
|
const matchersPath = path.join(__dirname, '../..', '.github');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user