mirror of https://github.com/actions/setup-node
Compare commits
3 Commits
88e9c9a87b
...
b6a48a9949
Author | SHA1 | Date |
---|---|---|
Pelle Wessman | b6a48a9949 | |
John Wesley Walker III | abb238b131 | |
Pelle Wessman | f01118b9a5 |
|
@ -126,6 +126,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, '', '0');
|
||||||
expect(hashFilesSpy).toHaveBeenCalled();
|
expect(hashFilesSpy).toHaveBeenCalled();
|
||||||
expect(infoSpy).toHaveBeenCalledWith(
|
expect(infoSpy).toHaveBeenCalledWith(
|
||||||
`Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}`
|
`Cache restored from key: ${platform}-0-setup-node-${packageManager}-${fileHash}`
|
||||||
);
|
);
|
||||||
expect(infoSpy).not.toHaveBeenCalledWith(
|
expect(infoSpy).not.toHaveBeenCalledWith(
|
||||||
`${packageManager} cache is not found`
|
`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, '', '0');
|
||||||
expect(hashFilesSpy).toHaveBeenCalled();
|
expect(hashFilesSpy).toHaveBeenCalled();
|
||||||
expect(infoSpy).toHaveBeenCalledWith(
|
expect(infoSpy).toHaveBeenCalledWith(
|
||||||
`${packageManager} cache is not found`
|
`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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import {
|
||||||
PackageManagerInfo,
|
PackageManagerInfo,
|
||||||
isCacheFeatureAvailable,
|
isCacheFeatureAvailable,
|
||||||
supportedPackageManagers,
|
supportedPackageManagers,
|
||||||
getCommandOutput,
|
isGhes,
|
||||||
resetProjectDirectoriesMemoized
|
resetProjectDirectoriesMemoized
|
||||||
} from '../src/cache-utils';
|
} from '../src/cache-utils';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
@ -361,3 +361,41 @@ describe('cache-utils', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('isGhes', () => {
|
||||||
|
const pristineEnv = process.env;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetModules();
|
||||||
|
process.env = {...pristineEnv};
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
process.env = pristineEnv;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false when the GITHUB_SERVER_URL environment variable is not defined', () => {
|
||||||
|
delete process.env['GITHUB_SERVER_URL'];
|
||||||
|
expect(isGhes()).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', () => {
|
||||||
|
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
|
||||||
|
expect(isGhes()).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', () => {
|
||||||
|
process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com';
|
||||||
|
expect(isGhes()).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', () => {
|
||||||
|
process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost';
|
||||||
|
expect(isGhes()).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', () => {
|
||||||
|
process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com';
|
||||||
|
expect(isGhes()).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -83977,7 +83977,11 @@ const repoHasYarnBerryManagedDependencies = (packageManagerInfo, cacheDependency
|
||||||
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
|
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
|
||||||
function isGhes() {
|
function isGhes() {
|
||||||
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
|
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
|
||||||
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
|
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
|
||||||
|
const isGitHubHost = hostname === 'GITHUB.COM';
|
||||||
|
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
|
||||||
|
const isLocalHost = hostname.endsWith('.LOCALHOST');
|
||||||
|
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
|
||||||
}
|
}
|
||||||
exports.isGhes = isGhes;
|
exports.isGhes = isGhes;
|
||||||
function isCacheFeatureAvailable() {
|
function isCacheFeatureAvailable() {
|
||||||
|
|
|
@ -93305,7 +93305,7 @@ const path_1 = __importDefault(__nccwpck_require__(1017));
|
||||||
const fs_1 = __importDefault(__nccwpck_require__(7147));
|
const fs_1 = __importDefault(__nccwpck_require__(7147));
|
||||||
const constants_1 = __nccwpck_require__(9042);
|
const constants_1 = __nccwpck_require__(9042);
|
||||||
const cache_utils_1 = __nccwpck_require__(1678);
|
const cache_utils_1 = __nccwpck_require__(1678);
|
||||||
const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
|
const restoreCache = (packageManager, cacheDependencyPath, cacheInvalidateAfterDays) => __awaiter(void 0, void 0, void 0, function* () {
|
||||||
const packageManagerInfo = yield (0, cache_utils_1.getPackageManagerInfo)(packageManager);
|
const packageManagerInfo = yield (0, cache_utils_1.getPackageManagerInfo)(packageManager);
|
||||||
if (!packageManagerInfo) {
|
if (!packageManagerInfo) {
|
||||||
throw new Error(`Caching for '${packageManager}' is not supported`);
|
throw new Error(`Caching for '${packageManager}' is not supported`);
|
||||||
|
@ -93320,22 +93320,30 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
|
||||||
if (!fileHash) {
|
if (!fileHash) {
|
||||||
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
|
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
|
||||||
}
|
}
|
||||||
const keyPrefix = `node-cache-${platform}-${packageManager}`;
|
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 keyPrefixBase = `node-cache-${platform}-${packageManager}`;
|
||||||
|
const keyPrefix = `${keyPrefixBase}-${timedInvalidationPrefix}`;
|
||||||
const primaryKey = `${keyPrefix}-${fileHash}`;
|
const primaryKey = `${keyPrefix}-${fileHash}`;
|
||||||
|
const restoreKeys = [`${keyPrefix}-`];
|
||||||
core.debug(`primary key is ${primaryKey}`);
|
core.debug(`primary key is ${primaryKey}`);
|
||||||
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
|
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
|
||||||
const isManagedByYarnBerry = yield (0, cache_utils_1.repoHasYarnBerryManagedDependencies)(packageManagerInfo, cacheDependencyPath);
|
const isManagedByYarnBerry = yield (0, cache_utils_1.repoHasYarnBerryManagedDependencies)(packageManagerInfo, cacheDependencyPath);
|
||||||
let cacheKey;
|
let cacheKey;
|
||||||
if (isManagedByYarnBerry) {
|
if (isManagedByYarnBerry) {
|
||||||
core.info('All dependencies are managed locally by yarn3, the previous cache can be used');
|
core.info('All dependencies are managed locally by yarn3, the previous cache can be used');
|
||||||
cacheKey = yield cache.restoreCache(cachePaths, primaryKey, [keyPrefix]);
|
cacheKey = yield cache.restoreCache(cachePaths, primaryKey, [keyPrefixBase]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cacheKey = yield cache.restoreCache(cachePaths, primaryKey);
|
cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys);
|
||||||
}
|
}
|
||||||
core.setOutput('cache-hit', Boolean(cacheKey));
|
core.setOutput('cache-hit', Boolean(cacheKey));
|
||||||
if (!cacheKey) {
|
if (!cacheKey) {
|
||||||
core.info(`${packageManager} cache is not found`);
|
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(', ')}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
|
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
|
||||||
|
@ -93598,7 +93606,11 @@ const repoHasYarnBerryManagedDependencies = (packageManagerInfo, cacheDependency
|
||||||
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
|
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
|
||||||
function isGhes() {
|
function isGhes() {
|
||||||
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
|
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
|
||||||
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
|
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
|
||||||
|
const isGitHubHost = hostname === 'GITHUB.COM';
|
||||||
|
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
|
||||||
|
const isLocalHost = hostname.endsWith('.LOCALHOST');
|
||||||
|
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
|
||||||
}
|
}
|
||||||
exports.isGhes = isGhes;
|
exports.isGhes = isGhes;
|
||||||
function isCacheFeatureAvailable() {
|
function isCacheFeatureAvailable() {
|
||||||
|
@ -94466,7 +94478,8 @@ function run() {
|
||||||
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
|
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
|
||||||
core.saveState(constants_1.State.CachePackageManager, cache);
|
core.saveState(constants_1.State.CachePackageManager, cache);
|
||||||
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
||||||
yield (0, cache_restore_1.restoreCache)(cache, cacheDependencyPath);
|
const cacheInvalidateAfterDays = core.getInput('cache-invalidate-after-days');
|
||||||
|
yield (0, cache_restore_1.restoreCache)(cache, cacheDependencyPath, cacheInvalidateAfterDays);
|
||||||
}
|
}
|
||||||
const matchersPath = path.join(__dirname, '../..', '.github');
|
const matchersPath = path.join(__dirname, '../..', '.github');
|
||||||
core.info(`##[add-matcher]${path.join(matchersPath, 'tsc.json')}`);
|
core.info(`##[add-matcher]${path.join(matchersPath, 'tsc.json')}`);
|
||||||
|
|
|
@ -14,7 +14,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) {
|
||||||
|
@ -37,9 +38,17 @@ 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 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 = `node-cache-${platform}-${packageManager}`;
|
const keyPrefixBase = `node-cache-${platform}-${packageManager}`;
|
||||||
|
const keyPrefix = `${keyPrefixBase}-${timedInvalidationPrefix}`;
|
||||||
const primaryKey = `${keyPrefix}-${fileHash}`;
|
const primaryKey = `${keyPrefix}-${fileHash}`;
|
||||||
|
const restoreKeys = [`${keyPrefix}-`];
|
||||||
core.debug(`primary key is ${primaryKey}`);
|
core.debug(`primary key is ${primaryKey}`);
|
||||||
|
|
||||||
core.saveState(State.CachePrimaryKey, primaryKey);
|
core.saveState(State.CachePrimaryKey, primaryKey);
|
||||||
|
@ -53,15 +62,15 @@ export const restoreCache = async (
|
||||||
core.info(
|
core.info(
|
||||||
'All dependencies are managed locally by yarn3, the previous cache can be used'
|
'All dependencies are managed locally by yarn3, the previous cache can be used'
|
||||||
);
|
);
|
||||||
cacheKey = await cache.restoreCache(cachePaths, primaryKey, [keyPrefix]);
|
cacheKey = await cache.restoreCache(cachePaths, primaryKey, [keyPrefixBase]);
|
||||||
} else {
|
} else {
|
||||||
cacheKey = await cache.restoreCache(cachePaths, primaryKey);
|
cacheKey = await cache.restoreCache(cachePaths, primaryKey, restoreKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
core.setOutput('cache-hit', Boolean(cacheKey));
|
core.setOutput('cache-hit', Boolean(cacheKey));
|
||||||
|
|
||||||
if (!cacheKey) {
|
if (!cacheKey) {
|
||||||
core.info(`${packageManager} cache is not found`);
|
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(', ')}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -295,7 +295,13 @@ export function isGhes(): boolean {
|
||||||
const ghUrl = new URL(
|
const ghUrl = new URL(
|
||||||
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||||
);
|
);
|
||||||
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
|
|
||||||
|
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
|
||||||
|
const isGitHubHost = hostname === 'GITHUB.COM';
|
||||||
|
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
|
||||||
|
const isLocalHost = hostname.endsWith('.LOCALHOST');
|
||||||
|
|
||||||
|
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCacheFeatureAvailable(): boolean {
|
export function isCacheFeatureAvailable(): boolean {
|
||||||
|
|
|
@ -62,7 +62,8 @@ export async function run() {
|
||||||
if (cache && isCacheFeatureAvailable()) {
|
if (cache && isCacheFeatureAvailable()) {
|
||||||
core.saveState(State.CachePackageManager, cache);
|
core.saveState(State.CachePackageManager, cache);
|
||||||
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