mirror of
https://gitea.com/actions/setup-java.git
synced 2026-08-07 02:31:20 +00:00
Add conditional JDK caching (#1201)
* Add JDK caching Cache resolved JDK tool-cache entries by exact platform and release identity, with a default-on cache-jdk input and explicit opt-out. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Apply batched suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix JDK cache CI validation Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Update brace-expansion security fix Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Refresh brace-expansion license metadata Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Refine JDK cache semantics Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Refine JDK cache documentation Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Simplify JDK cache identity Use one normalized runner OS dimension, reset the internal cache key schema for the unreleased feature, and align documentation, tests, and bundles. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Align JDK cache OS identity Use the established RUNNER_OS value directly and retain process.platform only as a non-Actions fallback. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Harden JDK cache saves and document tool-cache reuse Bind each JDK cache key to the installation identity it was computed for, keep post-job saves best-effort per entry, and state the real reuse and verification guarantee in the documentation. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * docs: restructure README caching section Rename '## Caching dependencies' to '## Caching' and add a what-gets-cached overview table covering the dependency, wrapper, and JDK caches. Lead with the common 'cache: maven' example and the dependency-cache material, and demote JDK caching into its own subsection. Also corrects the IMPORTANT callout, which implied JDK caching required an explicit opt-in; it is enabled implicitly whenever 'cache' is set. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * docs: fix caching documentation defects - Remove pull-request framing that compared behavior to `main`; state the tool-cache and `jdkfile` behavior directly and unconditionally. - Clarify that the JDK cache is a separate cache *entry* from the dependency and wrapper caches, while its *enablement* is coupled to `cache`, so the opening paragraph agrees with the enablement matrix. - Cite the actions/setup-java-benchmarks repository instead of an open PR and a self-referential PR comment, keeping the measured figures and caveats. - Keep the `cache`/`cache-jdk` matrix only in docs/advanced-usage.md and summarize the rules in prose in README.md to avoid divergence. - Describe the guarantee that a cache key is only saved with the installation it was computed for, instead of documenting inode/size/timestamp internals. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * docs: add V6 what's new entry for JDK caching Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: e2755464-4e83-47b6-ba71-731bb481b418 --------- 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> Copilot-Session: e2755464-4e83-47b6-ba71-731bb481b418
This commit is contained in:
@@ -8,6 +8,9 @@ import {
|
||||
beforeAll,
|
||||
afterAll
|
||||
} from '@jest/globals';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
|
||||
// Mock @actions/cache before importing source modules
|
||||
const real_cache_module = await import('@actions/cache');
|
||||
@@ -60,6 +63,9 @@ const core = await import('@actions/core');
|
||||
const cache = await import('@actions/cache');
|
||||
const {run: cleanup} = await import('../src/cleanup-java.js');
|
||||
const util = await import('../src/util.js');
|
||||
const {registerJdk, buildJdkCacheKey} = await import('../src/jdk-cache.js');
|
||||
|
||||
const jdkTempRoots: string[] = [];
|
||||
|
||||
describe('cleanup', () => {
|
||||
let spyWarning: any;
|
||||
@@ -88,6 +94,9 @@ describe('cleanup', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
while (jdkTempRoots.length) {
|
||||
fs.rmSync(jdkTempRoots.pop()!, {recursive: true, force: true});
|
||||
}
|
||||
resetState();
|
||||
jest.resetAllMocks();
|
||||
jest.clearAllMocks();
|
||||
@@ -163,6 +172,103 @@ describe('cleanup', () => {
|
||||
|
||||
expect(spyCacheSave).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('saves the JDK cache without dependency caching', async () => {
|
||||
const {key, path: jdkPath, state} = createRegisteredJdk();
|
||||
(core.getInput as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'cache-jdk' ? 'true' : ''
|
||||
);
|
||||
(core.getState as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'jdk-caches' ? state : ''
|
||||
);
|
||||
spyCacheSave.mockResolvedValue(1);
|
||||
|
||||
await cleanup();
|
||||
|
||||
expect(spyCacheSave).toHaveBeenCalledWith([jdkPath], key);
|
||||
});
|
||||
|
||||
it('does not save a JDK cache when cache-jdk is disabled', async () => {
|
||||
(core.getInput as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'cache-jdk' ? 'false' : ''
|
||||
);
|
||||
|
||||
await cleanup();
|
||||
|
||||
expect(spyCacheSave).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
['', '', false],
|
||||
['', 'true', true],
|
||||
['', 'false', false],
|
||||
['maven', '', true],
|
||||
['maven', 'true', true],
|
||||
['maven', 'false', false]
|
||||
])(
|
||||
'uses effective JDK caching for cache=%j and cache-jdk=%j',
|
||||
async (cacheInput, cacheJdkInput, expectedJdkSave) => {
|
||||
const {key: jdkKey, path: jdkPath, state} = createRegisteredJdk();
|
||||
(core.getInput as jest.Mock<any>).mockImplementation((name: string) => {
|
||||
if (name === 'cache') return cacheInput;
|
||||
if (name === 'cache-jdk') return cacheJdkInput;
|
||||
return '';
|
||||
});
|
||||
(core.getState as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'jdk-caches' ? state : ''
|
||||
);
|
||||
spyCacheSave.mockResolvedValue(1);
|
||||
|
||||
await cleanup();
|
||||
|
||||
const jdkSaveCalls = spyCacheSave.mock.calls.filter(
|
||||
([, key]) => key === jdkKey
|
||||
);
|
||||
expect(jdkSaveCalls).toHaveLength(expectedJdkSave ? 1 : 0);
|
||||
if (expectedJdkSave) {
|
||||
expect(spyCacheSave).toHaveBeenCalledWith([jdkPath], jdkKey);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
it('keeps saving the remaining JDK caches when one save fails', async () => {
|
||||
const first = createRegisteredJdk();
|
||||
const second = createRegisteredJdk('17.0.19+9');
|
||||
(core.getInput as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'cache-jdk' ? 'true' : ''
|
||||
);
|
||||
(core.getState as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'jdk-caches' ? second.state : ''
|
||||
);
|
||||
spyCacheSave.mockImplementation(async (paths: string[]) => {
|
||||
if (paths[0] === first.path) {
|
||||
throw new Error('Unexpected save failure');
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
|
||||
await cleanup();
|
||||
|
||||
expect(spyCacheSave).toHaveBeenCalledWith([first.path], first.key);
|
||||
expect(spyCacheSave).toHaveBeenCalledWith([second.path], second.key);
|
||||
expect(spyCoreError).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not save a JDK installation that was replaced after registration', async () => {
|
||||
const {key, path: jdkPath, state, replace} = createRegisteredJdk();
|
||||
(core.getInput as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'cache-jdk' ? 'true' : ''
|
||||
);
|
||||
(core.getState as jest.Mock<any>).mockImplementation((name: string) =>
|
||||
name === 'jdk-caches' ? state : ''
|
||||
);
|
||||
spyCacheSave.mockResolvedValue(1);
|
||||
replace();
|
||||
|
||||
await cleanup();
|
||||
|
||||
expect(spyCacheSave).not.toHaveBeenCalledWith([jdkPath], key);
|
||||
});
|
||||
});
|
||||
|
||||
function resetState() {
|
||||
@@ -199,3 +305,49 @@ function createStateForSuccessfulRestoreWithWrapper(packageManager: string) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a real JDK installation in a temporary tool cache so the post-job
|
||||
* save sees the same installation identity that setup recorded.
|
||||
*/
|
||||
function createRegisteredJdk(version = '21.0.8+9') {
|
||||
const root = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), 'setup-java-cleanup-jdk-')
|
||||
);
|
||||
jdkTempRoots.push(root);
|
||||
const jdkPath = path.join(
|
||||
root,
|
||||
'Java_temurin_jdk',
|
||||
version.replace('+', '-')
|
||||
);
|
||||
const write = (marker: string) => {
|
||||
const architecturePath = path.join(jdkPath, 'x64');
|
||||
fs.rmSync(architecturePath, {recursive: true, force: true});
|
||||
fs.rmSync(`${architecturePath}.complete`, {force: true});
|
||||
fs.mkdirSync(architecturePath, {recursive: true});
|
||||
fs.writeFileSync(path.join(architecturePath, 'release'), marker);
|
||||
fs.writeFileSync(`${architecturePath}.complete`, marker);
|
||||
};
|
||||
write('installed');
|
||||
|
||||
const jdk = {
|
||||
distribution: 'temurin',
|
||||
packageType: 'jdk',
|
||||
architecture: 'x64',
|
||||
version,
|
||||
source: `sha256:${path.basename(root)}`,
|
||||
verification: 'unverified',
|
||||
path: jdkPath
|
||||
};
|
||||
registerJdk(jdk);
|
||||
const state = (
|
||||
(core.saveState as jest.Mock).mock.calls.at(-1) as string[]
|
||||
)[1];
|
||||
|
||||
return {
|
||||
key: buildJdkCacheKey(jdk),
|
||||
path: jdkPath,
|
||||
state,
|
||||
replace: () => write('replaced-by-a-later-step')
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user