mirror of
https://gitea.com/actions/setup-java.git
synced 2026-07-09 19:22:05 +08:00
* 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>
This commit is contained in:
@@ -145,6 +145,8 @@ When the option `cache-dependency-path` is specified, the hash is based on the m
|
|||||||
|
|
||||||
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 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 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).
|
||||||
|
|
||||||
The cache input is optional, and caching is turned off by default.
|
The cache input is optional, and caching is turned off by default.
|
||||||
|
|
||||||
**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.
|
**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.
|
||||||
|
|||||||
@@ -78,6 +78,10 @@ describe('dependency cache', () => {
|
|||||||
ReturnType<typeof glob.hashFiles>,
|
ReturnType<typeof glob.hashFiles>,
|
||||||
Parameters<typeof glob.hashFiles>
|
Parameters<typeof glob.hashFiles>
|
||||||
>;
|
>;
|
||||||
|
let spySetOutput: jest.SpyInstance<
|
||||||
|
ReturnType<typeof core.setOutput>,
|
||||||
|
Parameters<typeof core.setOutput>
|
||||||
|
>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
spyCacheRestore = jest
|
spyCacheRestore = jest
|
||||||
@@ -86,6 +90,7 @@ describe('dependency cache', () => {
|
|||||||
Promise.resolve(undefined)
|
Promise.resolve(undefined)
|
||||||
);
|
);
|
||||||
spyGlobHashFiles = jest.spyOn(glob, 'hashFiles');
|
spyGlobHashFiles = jest.spyOn(glob, 'hashFiles');
|
||||||
|
spySetOutput = jest.spyOn(core, 'setOutput').mockImplementation(() => {});
|
||||||
spyWarning.mockImplementation(() => null);
|
spyWarning.mockImplementation(() => null);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -120,6 +125,15 @@ describe('dependency cache', () => {
|
|||||||
expect(spyWarning).not.toHaveBeenCalled();
|
expect(spyWarning).not.toHaveBeenCalled();
|
||||||
expect(spyInfo).toHaveBeenCalledWith('maven cache is not found');
|
expect(spyInfo).toHaveBeenCalledWith('maven cache is not found');
|
||||||
});
|
});
|
||||||
|
it('sets the cache-primary-key output', async () => {
|
||||||
|
createFile(join(workspace, 'pom.xml'));
|
||||||
|
|
||||||
|
await restore('maven', '');
|
||||||
|
expect(spySetOutput).toHaveBeenCalledWith(
|
||||||
|
'cache-primary-key',
|
||||||
|
expect.stringContaining('setup-java-')
|
||||||
|
);
|
||||||
|
});
|
||||||
it('downloads cache based on maven-wrapper.properties', async () => {
|
it('downloads cache based on maven-wrapper.properties', async () => {
|
||||||
createDirectory(join(workspace, '.mvn'));
|
createDirectory(join(workspace, '.mvn'));
|
||||||
createDirectory(join(workspace, '.mvn', 'wrapper'));
|
createDirectory(join(workspace, '.mvn', 'wrapper'));
|
||||||
|
|||||||
@@ -103,6 +103,8 @@ outputs:
|
|||||||
description: 'Path to where the java environment has been installed (same as $JAVA_HOME)'
|
description: 'Path to where the java environment has been installed (same as $JAVA_HOME)'
|
||||||
cache-hit:
|
cache-hit:
|
||||||
description: 'A boolean value to indicate an exact match was found for the primary key'
|
description: 'A boolean value to indicate an exact match was found for the primary key'
|
||||||
|
cache-primary-key:
|
||||||
|
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.'
|
||||||
runs:
|
runs:
|
||||||
using: 'node24'
|
using: 'node24'
|
||||||
main: 'dist/setup/index.js'
|
main: 'dist/setup/index.js'
|
||||||
|
|||||||
1
dist/cleanup/index.js
vendored
1
dist/cleanup/index.js
vendored
@@ -52056,6 +52056,7 @@ function restore(id, cacheDependencyPath) {
|
|||||||
const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath);
|
const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath);
|
||||||
core.debug(`primary key is ${primaryKey}`);
|
core.debug(`primary key is ${primaryKey}`);
|
||||||
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||||
|
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||||
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
|
// 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);
|
const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey);
|
||||||
if (matchedKey) {
|
if (matchedKey) {
|
||||||
|
|||||||
1
dist/setup/index.js
vendored
1
dist/setup/index.js
vendored
@@ -77921,6 +77921,7 @@ function restore(id, cacheDependencyPath) {
|
|||||||
const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath);
|
const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath);
|
||||||
core.debug(`primary key is ${primaryKey}`);
|
core.debug(`primary key is ${primaryKey}`);
|
||||||
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||||
|
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||||
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
|
// 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);
|
const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey);
|
||||||
if (matchedKey) {
|
if (matchedKey) {
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ export async function restore(id: string, cacheDependencyPath: string) {
|
|||||||
const primaryKey = await computeCacheKey(packageManager, cacheDependencyPath);
|
const primaryKey = await computeCacheKey(packageManager, cacheDependencyPath);
|
||||||
core.debug(`primary key is ${primaryKey}`);
|
core.debug(`primary key is ${primaryKey}`);
|
||||||
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||||
|
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||||
|
|
||||||
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
|
// 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);
|
const matchedKey = await cache.restoreCache(packageManager.path, primaryKey);
|
||||||
|
|||||||
Reference in New Issue
Block a user