Skip to content

Commit 117e7f5

Browse files
committed
feat(cache): change key to match actions/cache documentation
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 25316bb commit 117e7f5

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

__tests__/cache-restore.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ describe('cache-restore', () => {
135135
await restoreCache(packageManager);
136136
expect(hashFilesSpy).toHaveBeenCalled();
137137
expect(infoSpy).toHaveBeenCalledWith(
138-
`Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}`
138+
`Cache restored from key: ${platform}-setup-node-${packageManager}-${fileHash}`
139139
);
140140
expect(infoSpy).not.toHaveBeenCalledWith(
141-
`${packageManager} cache is not found`
141+
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
142142
);
143143
}
144144
);
@@ -165,7 +165,7 @@ describe('cache-restore', () => {
165165
await restoreCache(packageManager);
166166
expect(hashFilesSpy).toHaveBeenCalled();
167167
expect(infoSpy).toHaveBeenCalledWith(
168-
`${packageManager} cache is not found`
168+
`Cache not found for input keys: ${platform}-setup-node-${packageManager}-${fileHash}, ${platform}-setup-node-${packageManager}-, ${platform}-setup-node-`
169169
);
170170
}
171171
);

dist/setup/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44663,19 +44663,22 @@ exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0
4466344663
}
4466444664
const platform = process.env.RUNNER_OS;
4466544665
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo, packageManager);
44666+
const paths = [cachePath];
4466644667
const lockFilePath = cacheDependencyPath
4466744668
? cacheDependencyPath
4466844669
: findLockFile(packageManagerInfo);
4466944670
const fileHash = yield glob.hashFiles(lockFilePath);
4467044671
if (!fileHash) {
4467144672
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
4467244673
}
44673-
const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
44674+
const keyPrefix = `${platform}-setup-node-`;
44675+
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
44676+
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
4467444677
core.debug(`primary key is ${primaryKey}`);
4467544678
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
44676-
const cacheKey = yield cache.restoreCache([cachePath], primaryKey);
44679+
const cacheKey = yield cache.restoreCache(paths, primaryKey, restoreKeys);
4467744680
if (!cacheKey) {
44678-
core.info(`${packageManager} cache is not found`);
44681+
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(', ')}`);
4467944682
return;
4468044683
}
4468144684
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);

src/cache-restore.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as core from '@actions/core';
33
import * as glob from '@actions/glob';
44
import path from 'path';
55
import fs from 'fs';
6-
7-
import {State, Outputs} from './constants';
6+
import {State} from './constants';
87
import {
98
getCacheDirectoryPath,
109
getPackageManagerInfo,
@@ -25,6 +24,7 @@ export const restoreCache = async (
2524
packageManagerInfo,
2625
packageManager
2726
);
27+
const paths = [cachePath];
2828
const lockFilePath = cacheDependencyPath
2929
? cacheDependencyPath
3030
: findLockFile(packageManagerInfo);
@@ -35,19 +35,20 @@ export const restoreCache = async (
3535
'Some specified paths were not resolved, unable to cache dependencies.'
3636
);
3737
}
38-
39-
const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
38+
const keyPrefix = `${platform}-setup-node-`;
39+
const primaryKey = `${keyPrefix}${packageManager}-${fileHash}`;
40+
const restoreKeys = [`${keyPrefix}${packageManager}-`, keyPrefix];
4041
core.debug(`primary key is ${primaryKey}`);
41-
4242
core.saveState(State.CachePrimaryKey, primaryKey);
43-
44-
const cacheKey = await cache.restoreCache([cachePath], primaryKey);
45-
43+
const cacheKey = await cache.restoreCache(paths, primaryKey, restoreKeys);
4644
if (!cacheKey) {
47-
core.info(`${packageManager} cache is not found`);
45+
core.info(
46+
`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(
47+
', '
48+
)}`
49+
);
4850
return;
4951
}
50-
5152
core.saveState(State.CacheMatchedKey, cacheKey);
5253
core.info(`Cache restored from key: ${cacheKey}`);
5354
};

0 commit comments

Comments
 (0)