Skip to content
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

feat(cache): change key to match actions/cache documentation #323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ describe('cache-restore', () => {
await restoreCache(packageManager);
expect(hashFilesSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}`
`Cache restored from key: ${platform}-setup-node-${packageManager}-${fileHash}`
);
expect(infoSpy).not.toHaveBeenCalledWith(
`${packageManager} cache is not found`
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
);
}
);
Expand All @@ -165,7 +165,7 @@ describe('cache-restore', () => {
await restoreCache(packageManager);
expect(hashFilesSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith(
`${packageManager} cache is not found`
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
);
}
);
Expand Down
9 changes: 6 additions & 3 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44663,19 +44663,22 @@ exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0
}
const platform = process.env.RUNNER_OS;
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo, packageManager);
const paths = [cachePath];
const lockFilePath = cacheDependencyPath
? cacheDependencyPath
: findLockFile(packageManagerInfo);
const fileHash = yield glob.hashFiles(lockFilePath);
if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
}
const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
const keyPrefix = `${platform}-setup-node-`;
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache([cachePath], primaryKey);
const cacheKey = yield cache.restoreCache(paths, primaryKey, restoreKeys);
if (!cacheKey) {
core.info(`${packageManager} cache is not found`);
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(', ')}`);
return;
}
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
Expand Down
21 changes: 11 additions & 10 deletions src/cache-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as core from '@actions/core';
import * as glob from '@actions/glob';
import path from 'path';
import fs from 'fs';

import {State, Outputs} from './constants';
import {State} from './constants';
import {
getCacheDirectoryPath,
getPackageManagerInfo,
Expand All @@ -25,6 +24,7 @@ export const restoreCache = async (
packageManagerInfo,
packageManager
);
const paths = [cachePath];
const lockFilePath = cacheDependencyPath
? cacheDependencyPath
: findLockFile(packageManagerInfo);
Expand All @@ -35,19 +35,20 @@ export const restoreCache = async (
'Some specified paths were not resolved, unable to cache dependencies.'
);
}

const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
const keyPrefix = `${platform}-setup-node-`;
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
core.debug(`primary key is ${primaryKey}`);

core.saveState(State.CachePrimaryKey, primaryKey);

const cacheKey = await cache.restoreCache([cachePath], primaryKey);

const cacheKey = await cache.restoreCache(paths, primaryKey, restoreKeys);
if (!cacheKey) {
core.info(`${packageManager} cache is not found`);
core.info(
`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(
', '
)}`
);
return;
}

core.saveState(State.CacheMatchedKey, cacheKey);
core.info(`Cache restored from key: ${cacheKey}`);
};
Expand Down