feat: expose cache-primary-key output (#597) (#1088) · actions/setup-java@f848fd1 · GitHub
Skip to content

Commit f848fd1

Browse files
brunoborgesCopilotCopilot
authored
feat: expose cache-primary-key output (#597) (#1088)
* feat: expose cache-primary-key output (#597) 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 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * chore: rebuild dist Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent fe9b749 commit f848fd1

6 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions

‎__tests__/cache.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,16 @@ describe('dependency cache', () => {
133133
describe('restore', () => {
134134
let spyCacheRestore: any;
135135
let spyGlobHashFiles: any;
136+
let spySetOutput: any;
136137

137138
beforeEach(() => {
138139
spyCacheRestore = (cache.restoreCache as any).mockImplementation(
139140
(paths: string[], primaryKey: string) => Promise.resolve(undefined)
140141
);
141142
spyGlobHashFiles = glob.hashFiles as jest.Mock;
142143
spyGlobHashFiles.mockResolvedValue('hash-stub');
144+
spySetOutput = core.setOutput as jest.Mock;
145+
spySetOutput.mockImplementation(() => null);
143146
spyWarning.mockImplementation(() => null);
144147
});
145148

@@ -175,6 +178,15 @@ describe('dependency cache', () => {
175178
expect(spyWarning).not.toHaveBeenCalled();
176179
expect(spyInfo).toHaveBeenCalledWith('maven cache is not found');
177180
});
181+
it('sets the cache-primary-key output', async () => {
182+
createFile(join(workspace, 'pom.xml'));
183+
184+
await restore('maven', '');
185+
expect(spySetOutput).toHaveBeenCalledWith(
186+
'cache-primary-key',
187+
expect.stringContaining('setup-java-')
188+
);
189+
});
178190
it('downloads cache based on maven-wrapper.properties', async () => {
179191
createDirectory(join(workspace, '.mvn'));
180192
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
@@ -99735,6 +99735,7 @@ async function restore(id, cacheDependencyPath) {
9973599735
const primaryKey = await computeCacheKey(packageManager, cacheDependencyPath);
9973699736
core.debug(`primary key is ${primaryKey}`);
9973799737
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
99738+
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
9973899739
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
9973999740
const matchedKey = await cache.restoreCache(packageManager.path, primaryKey);
9974099741
if (matchedKey) {

‎dist/setup/index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130957,6 +130957,7 @@ async function restore(id, cacheDependencyPath) {
130957130957
const primaryKey = await computeCacheKey(packageManager, cacheDependencyPath);
130958130958
core_debug(`primary key is ${primaryKey}`);
130959130959
saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
130960+
setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
130960130961
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
130961130962
const matchedKey = await restoreCache(packageManager.path, primaryKey);
130962130963
if (matchedKey) {

‎src/cache.ts‎

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)