mirror of
https://github.com/actions/setup-java.git
synced 2026-08-05 17:02:57 +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:
@@ -12,6 +12,9 @@ import fs from 'fs';
|
||||
|
||||
import path from 'path';
|
||||
import * as semver from 'semver';
|
||||
import os from 'os';
|
||||
|
||||
const realStatSync = fs.statSync;
|
||||
|
||||
// Mock @actions/core before importing source modules that depend on it
|
||||
jest.unstable_mockModule('@actions/core', () => ({
|
||||
@@ -54,6 +57,12 @@ jest.unstable_mockModule('@actions/tool-cache', () => ({
|
||||
evaluateVersions: jest.fn()
|
||||
}));
|
||||
|
||||
jest.unstable_mockModule('../../src/jdk-cache.js', () => ({
|
||||
getJdkVerificationIdentity: jest.fn(() => 'unverified'),
|
||||
registerJdk: jest.fn(),
|
||||
restoreJdk: jest.fn()
|
||||
}));
|
||||
|
||||
const real_util_module = await import('../../src/util.js');
|
||||
jest.unstable_mockModule('../../src/util.js', () => ({
|
||||
...real_util_module,
|
||||
@@ -70,6 +79,7 @@ jest.unstable_mockModule('../../src/util.js', () => ({
|
||||
const core = await import('@actions/core');
|
||||
const tc = await import('@actions/tool-cache');
|
||||
const util = await import('../../src/util.js');
|
||||
const jdkCache = await import('../../src/jdk-cache.js');
|
||||
const {LocalDistribution} =
|
||||
await import('../../src/distributions/local/installer.js');
|
||||
|
||||
@@ -95,6 +105,9 @@ describe('setupJava', () => {
|
||||
const expectedJdkFile = 'JavaLocalJdkFile';
|
||||
|
||||
beforeEach(() => {
|
||||
(jdkCache.getJdkVerificationIdentity as jest.Mock).mockReturnValue(
|
||||
'unverified'
|
||||
);
|
||||
spyGetToolcachePath = util.getToolcachePath as jest.Mock;
|
||||
spyGetToolcachePath.mockImplementation(
|
||||
(toolname: string, javaVersion: string, architecture: string) => {
|
||||
@@ -231,6 +244,72 @@ describe('setupJava', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[false, true, true],
|
||||
[true, false, true]
|
||||
])(
|
||||
'handles jdkfile caching with force-download=%s',
|
||||
async (forceDownload, restores, registers) => {
|
||||
const temporaryDirectory = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), 'setup-java-local-cache-')
|
||||
);
|
||||
const jdkFile = path.join(temporaryDirectory, 'java.tar.gz');
|
||||
fs.writeFileSync(jdkFile, 'jdk archive');
|
||||
spyGetToolcachePath.mockReturnValue('');
|
||||
spyFsStat.mockImplementation((file: string) => realStatSync(file));
|
||||
(jdkCache.restoreJdk as jest.Mock).mockResolvedValue(false);
|
||||
|
||||
try {
|
||||
mockJavaBase = new LocalDistribution(
|
||||
{
|
||||
version: actualJavaVersion,
|
||||
architecture: 'x86',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false,
|
||||
forceDownload,
|
||||
cacheJdk: true
|
||||
},
|
||||
jdkFile
|
||||
);
|
||||
|
||||
await mockJavaBase.setupJava();
|
||||
|
||||
expect(jdkCache.restoreJdk).toHaveBeenCalledTimes(restores ? 1 : 0);
|
||||
expect(jdkCache.registerJdk).toHaveBeenCalledTimes(registers ? 1 : 0);
|
||||
expect(
|
||||
(jdkCache.restoreJdk as jest.Mock).mock.calls[0]?.[0] ??
|
||||
(jdkCache.registerJdk as jest.Mock).mock.calls[0]?.[0]
|
||||
).toEqual(
|
||||
expect.objectContaining({
|
||||
distribution: 'jdkfile',
|
||||
version: actualJavaVersion,
|
||||
verification: 'unverified'
|
||||
})
|
||||
);
|
||||
} finally {
|
||||
fs.rmSync(temporaryDirectory, {recursive: true});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
it('rejects signature verification for jdkfile archives', async () => {
|
||||
mockJavaBase = new LocalDistribution(
|
||||
{
|
||||
version: actualJavaVersion,
|
||||
architecture: 'x86',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false,
|
||||
verifySignature: true
|
||||
},
|
||||
expectedJdkFile
|
||||
);
|
||||
|
||||
await expect(mockJavaBase.setupJava()).rejects.toThrow(
|
||||
"Input 'verify-signature' is not supported for distribution 'jdkfile'."
|
||||
);
|
||||
expect(spyGetToolcachePath).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("java is resolved from toolcache, jdkfile doesn't exist", async () => {
|
||||
const inputs = {
|
||||
version: actualJavaVersion,
|
||||
|
||||
Reference in New Issue
Block a user