From 01f97385c374abf6ce112b7a69db74f10d7e2423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sch=C3=BCnadel?= Date: Thu, 13 Jan 2022 15:08:51 +0100 Subject: [PATCH 1/4] Set output cache-hit to indicate if cache was hit --- README.md | 2 ++ dist/cleanup/index.js | 2 ++ dist/setup/index.js | 2 ++ src/cache.ts | 2 ++ 4 files changed, 8 insertions(+) diff --git a/README.md b/README.md index c02eb6c92..af8ea7863 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ The action has a built-in functionality for caching and restoring dependencies. - gradle: `**/*.gradle*`, `**/gradle-wrapper.properties` - maven: `**/pom.xml` +The workflow output `cache-hit` is set to indicate if an exact match was found for the key [as actions/cache does](https://github.com/actions/cache/tree/main#outputs). + The cache input is optional, and caching is turned off by default. #### Caching gradle dependencies diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 0a2047269..6507c846f 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -61873,9 +61873,11 @@ function restore(id) { ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); + core.setOutput('cache-hit', true); core.info(`Cache restored from key: ${matchedKey}`); } else { + core.setOutput('cache-hit', false); core.info(`${packageManager.id} cache is not found`); } }); diff --git a/dist/setup/index.js b/dist/setup/index.js index 34b866d88..468bd8518 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -18664,9 +18664,11 @@ function restore(id) { ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); + core.setOutput('cache-hit', true); core.info(`Cache restored from key: ${matchedKey}`); } else { + core.setOutput('cache-hit', false); core.info(`${packageManager.id} cache is not found`); } }); diff --git a/src/cache.ts b/src/cache.ts index fb97fb0b1..d79155fbe 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -77,8 +77,10 @@ export async function restore(id: string) { ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); + core.setOutput('cache-hit', true); core.info(`Cache restored from key: ${matchedKey}`); } else { + core.setOutput('cache-hit', false); core.info(`${packageManager.id} cache is not found`); } } From cb023f444780c37147fb45baf919b5d27ba9d6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sch=C3=BCnadel?= Date: Thu, 13 Jan 2022 16:39:37 +0100 Subject: [PATCH 2/4] remove "restoreKeys" to clear cache after update --- dist/cleanup/index.js | 5 ++--- dist/setup/index.js | 5 ++--- src/cache.ts | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 4392ec676..d38de346c 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -61868,9 +61868,8 @@ function restore(id) { if (primaryKey.endsWith('-')) { throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); } - const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` - ]); + // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) + const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); core.setOutput('cache-hit', true); diff --git a/dist/setup/index.js b/dist/setup/index.js index 587a76c77..2b400cde3 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -18659,9 +18659,8 @@ function restore(id) { if (primaryKey.endsWith('-')) { throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); } - const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` - ]); + // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) + const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); core.setOutput('cache-hit', true); diff --git a/src/cache.ts b/src/cache.ts index 72ae2ef01..f5dc94afe 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -72,9 +72,8 @@ export async function restore(id: string) { ); } - const matchedKey = await cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` - ]); + // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) + const matchedKey = await cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); core.setOutput('cache-hit', true); From edfea5c367da9d10a274d4508ceba5c76e640bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sch=C3=BCnadel?= Date: Fri, 14 Jan 2022 11:22:56 +0100 Subject: [PATCH 3/4] Only set cache-hit on exact match of cache key --- dist/cleanup/index.js | 2 +- dist/setup/index.js | 2 +- src/cache.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 6507c846f..460651aa4 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -61873,7 +61873,7 @@ function restore(id) { ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); - core.setOutput('cache-hit', true); + core.setOutput('cache-hit', matchedKey === primaryKey); core.info(`Cache restored from key: ${matchedKey}`); } else { diff --git a/dist/setup/index.js b/dist/setup/index.js index 468bd8518..9be31b4a5 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -18664,7 +18664,7 @@ function restore(id) { ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); - core.setOutput('cache-hit', true); + core.setOutput('cache-hit', matchedKey === primaryKey); core.info(`Cache restored from key: ${matchedKey}`); } else { diff --git a/src/cache.ts b/src/cache.ts index d79155fbe..3948a5a41 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -77,7 +77,7 @@ export async function restore(id: string) { ]); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); - core.setOutput('cache-hit', true); + core.setOutput('cache-hit', matchedKey === primaryKey); core.info(`Cache restored from key: ${matchedKey}`); } else { core.setOutput('cache-hit', false); From efcb607a6b220889eda36da9c60f75624bdfbff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sch=C3=BCnadel?= Date: Thu, 13 Jan 2022 16:39:37 +0100 Subject: [PATCH 4/4] remove "restoreKeys" to clear cache after update --- dist/cleanup/index.js | 5 ++--- dist/setup/index.js | 5 ++--- src/cache.ts | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 0a2047269..9ca9f5305 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -61868,9 +61868,8 @@ function restore(id) { if (primaryKey.endsWith('-')) { throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); } - const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` - ]); + // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) + const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); core.info(`Cache restored from key: ${matchedKey}`); diff --git a/dist/setup/index.js b/dist/setup/index.js index 34b866d88..e4d5dca9e 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -18659,9 +18659,8 @@ function restore(id) { if (primaryKey.endsWith('-')) { throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); } - const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` - ]); + // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) + const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); core.info(`Cache restored from key: ${matchedKey}`); diff --git a/src/cache.ts b/src/cache.ts index fb97fb0b1..4e1c7ad41 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -72,9 +72,8 @@ export async function restore(id: string) { ); } - const matchedKey = await cache.restoreCache(packageManager.path, primaryKey, [ - `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}` - ]); + // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) + const matchedKey = await cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { core.saveState(CACHE_MATCHED_KEY, matchedKey); core.info(`Cache restored from key: ${matchedKey}`);