Skip to content

Commit 04be95c

Browse files
gowridurgadgowridurgad
andauthored
Add cache-primary-key and cache-matched-key as outputs (#1577)
* Add cache-primary-key and cache-matched-key as outputs * Refine cache output descriptions --------- Co-authored-by: gowridurgad <gowridurgad@gmail.com>
1 parent 7c2c68d commit 04be95c

4 files changed

Lines changed: 29 additions & 1 deletion

File tree

__tests__/cache-restore.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ describe('cache-restore', () => {
137137
] as const)(
138138
'restored dependencies for %s',
139139
async (packageManager, toolVersion, fileHash) => {
140+
const expectedCacheKey = `node-cache-${platform}-${arch}-${packageManager}-${fileHash}`;
140141
// Set workspace to the appropriate fixture folder
141142
setWorkspaceFor(packageManager);
142143
getCommandOutputSpy.mockImplementation((command: string) => {
@@ -150,12 +151,20 @@ describe('cache-restore', () => {
150151
await restoreCache(packageManager, '');
151152
expect(hashFilesSpy).toHaveBeenCalled();
152153
expect(infoSpy).toHaveBeenCalledWith(
153-
`Cache restored from key: node-cache-${platform}-${arch}-${packageManager}-${fileHash}`
154+
`Cache restored from key: ${expectedCacheKey}`
154155
);
155156
expect(infoSpy).not.toHaveBeenCalledWith(
156157
`${packageManager} cache is not found`
157158
);
158159
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
160+
expect(setOutputSpy).toHaveBeenCalledWith(
161+
'cache-primary-key',
162+
expectedCacheKey
163+
);
164+
expect(setOutputSpy).toHaveBeenCalledWith(
165+
'cache-matched-key',
166+
expectedCacheKey
167+
);
159168
}
160169
);
161170
});
@@ -169,6 +178,7 @@ describe('cache-restore', () => {
169178
] as const)(
170179
'dependencies are changed %s',
171180
async (packageManager, toolVersion, fileHash) => {
181+
const expectedCacheKey = `node-cache-${platform}-${arch}-${packageManager}-${fileHash}`;
172182
// Set workspace to the appropriate fixture folder
173183
setWorkspaceFor(packageManager);
174184
getCommandOutputSpy.mockImplementation((command: string) => {
@@ -186,6 +196,14 @@ describe('cache-restore', () => {
186196
`${packageManager} cache is not found`
187197
);
188198
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false);
199+
expect(setOutputSpy).toHaveBeenCalledWith(
200+
'cache-primary-key',
201+
expectedCacheKey
202+
);
203+
expect(setOutputSpy).toHaveBeenCalledWith(
204+
'cache-matched-key',
205+
undefined
206+
);
189207
}
190208
);
191209
});

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ inputs:
3434
outputs:
3535
cache-hit:
3636
description: 'A boolean value to indicate if a cache was hit.'
37+
cache-primary-key:
38+
description: 'The key used to restore and save the cache.'
39+
cache-matched-key:
40+
description: 'The key of the cache that was restored. Empty when no cache is found.'
3741
node-version:
3842
description: 'The installed node version.'
3943
runs:

dist/setup/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57021,6 +57021,7 @@ const restoreCache = async (packageManager, cacheDependencyPath) => {
5702157021
const primaryKey = `${keyPrefix}-${fileHash}`;
5702257022
core.debug(`primary key is ${primaryKey}`);
5702357023
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
57024+
core.setOutput('cache-primary-key', primaryKey);
5702457025
const isManagedByYarnBerry = await (0, cache_utils_1.repoHasYarnBerryManagedDependencies)(packageManagerInfo, cacheDependencyPath);
5702557026
let cacheKey;
5702657027
if (isManagedByYarnBerry) {
@@ -57031,6 +57032,8 @@ const restoreCache = async (packageManager, cacheDependencyPath) => {
5703157032
cacheKey = await cache.restoreCache(cachePaths, primaryKey);
5703257033
}
5703357034
core.setOutput('cache-hit', Boolean(cacheKey));
57035+
core.setOutput('cache-matched-key', cacheKey);
57036+
core.debug(`cache-matched-key is ${cacheKey}`);
5703457037
if (!cacheKey) {
5703557038
core.info(`${packageManager} cache is not found`);
5703657039
return;

src/cache-restore.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const restoreCache = async (
4545
core.debug(`primary key is ${primaryKey}`);
4646

4747
core.saveState(State.CachePrimaryKey, primaryKey);
48+
core.setOutput('cache-primary-key', primaryKey);
4849

4950
const isManagedByYarnBerry = await repoHasYarnBerryManagedDependencies(
5051
packageManagerInfo,
@@ -61,6 +62,8 @@ export const restoreCache = async (
6162
}
6263

6364
core.setOutput('cache-hit', Boolean(cacheKey));
65+
core.setOutput('cache-matched-key', cacheKey);
66+
core.debug(`cache-matched-key is ${cacheKey}`);
6467

6568
if (!cacheKey) {
6669
core.info(`${packageManager} cache is not found`);

0 commit comments

Comments
 (0)