Skip to content

Commit

Permalink
feat(cache): change key to match actions/cache documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Sep 3, 2021
1 parent 25316bb commit 117e7f5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
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

0 comments on commit 117e7f5

Please sign in to comment.