Skip to content

Commit 513edc4

Browse files
brunoborgesCopilot
andauthored
feat: expose cache-primary-key output (#597) [v5 backport] (#1089)
* feat: expose cache-primary-key output (#597) Backport of the cache-primary-key output to the v5 release line. Expose the primary cache key computed by the caching logic as a new `cache-primary-key` action output, so workflows can compose the built-in setup-java cache key with actions/cache or actions/cache/restore across steps and dependent jobs. - src/cache.ts: set the `cache-primary-key` output in restore() - action.yml: declare the new output - README.md: document the new output - __tests__/cache.test.ts: assert the output is set - dist: rebuild (CommonJS) Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Apply PR #1088 review suggestions to v5 backport Port the review feedback from the main-line PR (#1088) into the v5 backport: - src/cache.ts: use the STATE_CACHE_PRIMARY_KEY constant for the output name instead of a duplicated string literal - action.yml / README.md: clarify that the output is also empty when caching is skipped (e.g. the cache service is unavailable) - dist: rebuild Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 62df799 commit 513edc4

6 files changed

Lines changed: 21 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ When the option `cache-dependency-path` is specified, the hash is based on the m
145145

146146
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).
147147

148+
The workflow output `cache-primary-key` exposes the primary cache key computed by the action for the configured build tool. It is useful for composing with [`actions/cache`](https://github.com/actions/cache) or [`actions/cache/restore`](https://github.com/actions/cache/tree/main/restore) in later steps or dependent jobs that need to reuse the exact same key. It is empty when caching is not enabled or when caching is skipped (for example, when the cache service is unavailable).
149+
148150
The cache input is optional, and caching is turned off by default.
149151

150152
**Maven Wrapper:** when `cache: 'maven'` is enabled, the action also caches and restores the Maven Wrapper distribution downloaded to `~/.m2/wrapper/dists` (in addition to the local repository), so wrapper-based (`./mvnw`) builds don't re-download the wrapper on every run. This is keyed on `**/.mvn/wrapper/maven-wrapper.properties` as shown above.

__tests__/cache.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ describe('dependency cache', () => {
7878
ReturnType<typeof glob.hashFiles>,
7979
Parameters<typeof glob.hashFiles>
8080
>;
81+
let spySetOutput: jest.SpyInstance<
82+
ReturnType<typeof core.setOutput>,
83+
Parameters<typeof core.setOutput>
84+
>;
8185

8286
beforeEach(() => {
8387
spyCacheRestore = jest
@@ -86,6 +90,7 @@ describe('dependency cache', () => {
8690
Promise.resolve(undefined)
8791
);
8892
spyGlobHashFiles = jest.spyOn(glob, 'hashFiles');
93+
spySetOutput = jest.spyOn(core, 'setOutput').mockImplementation(() => {});
8994
spyWarning.mockImplementation(() => null);
9095
});
9196

@@ -120,6 +125,15 @@ describe('dependency cache', () => {
120125
expect(spyWarning).not.toHaveBeenCalled();
121126
expect(spyInfo).toHaveBeenCalledWith('maven cache is not found');
122127
});
128+
it('sets the cache-primary-key output', async () => {
129+
createFile(join(workspace, 'pom.xml'));
130+
131+
await restore('maven', '');
132+
expect(spySetOutput).toHaveBeenCalledWith(
133+
'cache-primary-key',
134+
expect.stringContaining('setup-java-')
135+
);
136+
});
123137
it('downloads cache based on maven-wrapper.properties', async () => {
124138
createDirectory(join(workspace, '.mvn'));
125139
createDirectory(join(workspace, '.mvn', 'wrapper'));

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ outputs:
103103
description: 'Path to where the java environment has been installed (same as $JAVA_HOME)'
104104
cache-hit:
105105
description: 'A boolean value to indicate an exact match was found for the primary key'
106+
cache-primary-key:
107+
description: 'The primary cache key computed by the action for the configured build tool. Empty when caching is not enabled or when caching is skipped (e.g. cache service unavailable). Useful for composing with actions/cache or actions/cache/restore across jobs.'
106108
runs:
107109
using: 'node24'
108110
main: 'dist/setup/index.js'

dist/cleanup/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52056,6 +52056,7 @@ function restore(id, cacheDependencyPath) {
5205652056
const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath);
5205752057
core.debug(`primary key is ${primaryKey}`);
5205852058
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
52059+
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
5205952060
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
5206052061
const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey);
5206152062
if (matchedKey) {

dist/setup/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77921,6 +77921,7 @@ function restore(id, cacheDependencyPath) {
7792177921
const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath);
7792277922
core.debug(`primary key is ${primaryKey}`);
7792377923
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
77924+
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
7792477925
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
7792577926
const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey);
7792677927
if (matchedKey) {

src/cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export async function restore(id: string, cacheDependencyPath: string) {
118118
const primaryKey = await computeCacheKey(packageManager, cacheDependencyPath);
119119
core.debug(`primary key is ${primaryKey}`);
120120
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
121+
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
121122

122123
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
123124
const matchedKey = await cache.restoreCache(packageManager.path, primaryKey);

0 commit comments

Comments
 (0)