mirror of
https://github.com/actions/setup-java.git
synced 2026-08-05 17:02:57 +00:00
Cache resolved JDK releases to remove the vendor API from warm jobs (#1208)
* Cache resolved JDK releases to remove the vendor API from warm jobs Only Temurin is preinstalled in the runner tool cache, so for every other distribution `findInToolcache()` misses on essentially every job. That forces a call to the distribution's metadata API before the JDK cache key can even be computed, which makes the vendor a hard per-job dependency even when the JDK bytes are already cached, and turns a vendor 403, 429, or outage into a job failure. Store the resolved release in a small companion cache entry keyed only on inputs known before any network call: runner OS, architecture, distribution, package type, requested version, and stability. A job that finds a current entry installs the JDK without contacting the metadata API at all. `@actions/cache` derives a cache version by hashing the requested paths, so save and restore paths must match. The entry therefore uses a path that excludes the date bucket while the key includes it, which lets restore keys fall back to an older bucket. An entry older than the current day is not used directly: the metadata API is still queried so floating requests such as `java-version: 21` keep picking up new releases, and the older entry is used only when that query fails. Because the entry also carries the download URL and checksum, that fallback works even when the JDK itself is not cached. Releases whose URL is not content-addressed are never stored. Oracle JDK and Oracle GraalVM build a `/latest/` URL for a major-only version, and its bytes change when a new build is published, so the URL and checksum are only consistent at the moment they are resolved. Mark those releases floating and skip recording them. Restored payloads are validated as untrusted input, and the post-job save rewrites the payload the key was computed for rather than uploading whatever is on disk, since a restore in a later step targets the same path. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: b76d8cb0-f629-46e1-bf9a-ffde06948644 * Widen the resolution freshness window from a day to a week A daily window gives no benefit to the repositories that need it most. A repository whose workflows run once a day would re-resolve on every job, and one running weekly would never see a current entry at all, yet those are exactly the repositories with nothing warm in the tool cache. Seven days is also the ceiling. GitHub removes cache entries that have not been accessed for seven days, so a longer window would leave the previous entry evicted by the time the window rolls over, removing the stale fallback at the moment it is most likely to be needed. It comfortably covers JDK release cadence, which is monthly at its fastest and usually quarterly. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: b76d8cb0-f629-46e1-bf9a-ffde06948644 * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Rebuild dist to match the linted source The pre-commit hook runs `eslint --fix` after `npm run check` has already built `dist/`, so the fix it applied to the resolution fallback warning in `base-installer.ts` never reached the bundle. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: b76d8cb0-f629-46e1-bf9a-ffde06948644 * Rebuild dist to match the linted source The autofix accepted on the pull request edited the resolution fallback warning in `base-installer.ts` through the GitHub UI, which does not run `npm run build`, so `dist/` still carried the pre-fix bundle. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: b76d8cb0-f629-46e1-bf9a-ffde06948644 --------- 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: b76d8cb0-f629-46e1-bf9a-ffde06948644
This commit is contained in:
@@ -78,6 +78,11 @@ jest.unstable_mockModule('../../src/jdk-cache.js', () => ({
|
||||
restoreJdk: jest.fn()
|
||||
}));
|
||||
|
||||
jest.unstable_mockModule('../../src/jdk-resolution-cache.js', () => ({
|
||||
registerJdkResolution: jest.fn(),
|
||||
restoreJdkResolution: jest.fn()
|
||||
}));
|
||||
|
||||
const real_util_module = await import('../../src/util.js');
|
||||
jest.unstable_mockModule('../../src/util.js', () => ({
|
||||
...real_util_module,
|
||||
@@ -95,6 +100,7 @@ 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 jdkResolutionCache = await import('../../src/jdk-resolution-cache.js');
|
||||
const {JavaBase} = await import('../../src/distributions/base-installer.js');
|
||||
|
||||
class EmptyJavaBase extends JavaBase {
|
||||
@@ -949,6 +955,155 @@ describe('setupJava', () => {
|
||||
'Installing Java 11.0.9 (not setting as default)'
|
||||
);
|
||||
});
|
||||
|
||||
describe('resolution cache', () => {
|
||||
// 11.0.9 is not in the mocked tool-cache, so the tool-cache short-circuit
|
||||
// misses and the release has to be resolved, exactly as it does for every
|
||||
// distribution that is not preinstalled on hosted runners.
|
||||
const options: JavaInstallerOptions = {
|
||||
version: '11.0.9',
|
||||
architecture: 'x86',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false,
|
||||
cacheJdk: true
|
||||
};
|
||||
const cachedRelease = {
|
||||
version: '11.0.9',
|
||||
url: 'https://example.com/java/11.0.9'
|
||||
};
|
||||
|
||||
const expectedRequest = {
|
||||
distribution: 'Empty',
|
||||
packageType: 'jdk',
|
||||
architecture: 'x86',
|
||||
versionSpec: '11.0.9',
|
||||
stable: true
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
(jdkCache.restoreJdk as jest.Mock).mockResolvedValue(false);
|
||||
(jdkResolutionCache.restoreJdkResolution as jest.Mock).mockResolvedValue(
|
||||
undefined
|
||||
);
|
||||
});
|
||||
|
||||
it('skips the metadata API on a fresh cached resolution', async () => {
|
||||
mockJavaBase = new EmptyJavaBase(options);
|
||||
const findPackageForDownload = jest.spyOn(
|
||||
mockJavaBase as any,
|
||||
'findPackageForDownload'
|
||||
);
|
||||
(jdkResolutionCache.restoreJdkResolution as jest.Mock).mockResolvedValue({
|
||||
release: cachedRelease,
|
||||
fresh: true
|
||||
});
|
||||
|
||||
await mockJavaBase.setupJava();
|
||||
|
||||
expect(jdkResolutionCache.restoreJdkResolution).toHaveBeenCalledWith(
|
||||
expectedRequest
|
||||
);
|
||||
expect(findPackageForDownload).not.toHaveBeenCalled();
|
||||
expect(jdkResolutionCache.registerJdkResolution).not.toHaveBeenCalled();
|
||||
expect(spyCoreInfo).toHaveBeenCalledWith(
|
||||
'Resolved Empty 11.0.9 from the resolution cache'
|
||||
);
|
||||
});
|
||||
|
||||
it('re-resolves and records the release on a miss', async () => {
|
||||
mockJavaBase = new EmptyJavaBase(options);
|
||||
|
||||
await mockJavaBase.setupJava();
|
||||
|
||||
expect(jdkResolutionCache.registerJdkResolution).toHaveBeenCalledWith(
|
||||
expectedRequest,
|
||||
{version: '11.0.9', url: 'some/random_url/java/11.0.9'}
|
||||
);
|
||||
});
|
||||
|
||||
it('re-resolves when the cached resolution is stale', async () => {
|
||||
mockJavaBase = new EmptyJavaBase(options);
|
||||
const findPackageForDownload = jest.spyOn(
|
||||
mockJavaBase as any,
|
||||
'findPackageForDownload'
|
||||
);
|
||||
(jdkResolutionCache.restoreJdkResolution as jest.Mock).mockResolvedValue({
|
||||
release: cachedRelease,
|
||||
fresh: false
|
||||
});
|
||||
|
||||
await mockJavaBase.setupJava();
|
||||
|
||||
expect(findPackageForDownload).toHaveBeenCalled();
|
||||
expect(jdkResolutionCache.registerJdkResolution).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('falls back to a stale resolution when the metadata API fails', async () => {
|
||||
mockJavaBase = new EmptyJavaBase(options);
|
||||
const downloadTool = jest
|
||||
.spyOn(mockJavaBase as any, 'downloadTool')
|
||||
.mockResolvedValue({version: '11.0.9', path: javaPathInstalled});
|
||||
jest
|
||||
.spyOn(mockJavaBase as any, 'findPackageForDownload')
|
||||
.mockRejectedValue(new Error('503 Service Unavailable'));
|
||||
(jdkResolutionCache.restoreJdkResolution as jest.Mock).mockResolvedValue({
|
||||
release: cachedRelease,
|
||||
fresh: false
|
||||
});
|
||||
|
||||
await expect(mockJavaBase.setupJava()).resolves.toEqual({
|
||||
version: '11.0.9',
|
||||
path: javaPathInstalled
|
||||
});
|
||||
|
||||
expect(downloadTool).toHaveBeenCalledWith(cachedRelease);
|
||||
expect(jdkResolutionCache.registerJdkResolution).not.toHaveBeenCalled();
|
||||
expect(core.warning).toHaveBeenCalledWith(
|
||||
expect.stringContaining('falling back to the cached resolution')
|
||||
);
|
||||
});
|
||||
|
||||
it('fails when the metadata API fails and nothing was cached', async () => {
|
||||
mockJavaBase = new EmptyJavaBase(options);
|
||||
jest
|
||||
.spyOn(mockJavaBase as any, 'findPackageForDownload')
|
||||
.mockRejectedValue(new Error('503 Service Unavailable'));
|
||||
|
||||
await expect(mockJavaBase.setupJava()).rejects.toThrow(
|
||||
'503 Service Unavailable'
|
||||
);
|
||||
});
|
||||
|
||||
it('does not record a floating release', async () => {
|
||||
mockJavaBase = new EmptyJavaBase(options);
|
||||
jest
|
||||
.spyOn(mockJavaBase as any, 'findPackageForDownload')
|
||||
.mockResolvedValue({
|
||||
version: '11.0.9',
|
||||
url: 'https://example.com/java/11/latest/jdk-11.tar.gz',
|
||||
checksum: {algorithm: 'sha256', value: 'abc'},
|
||||
floating: true
|
||||
});
|
||||
|
||||
await mockJavaBase.setupJava();
|
||||
|
||||
expect(jdkResolutionCache.registerJdkResolution).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
['cache-jdk is disabled', {cacheJdk: false}],
|
||||
['check-latest is enabled', {checkLatest: true}],
|
||||
['force-download is enabled', {forceDownload: true}],
|
||||
['java-version is "latest"', {version: 'latest'}]
|
||||
])('is bypassed when %s', async (_name, overrides) => {
|
||||
mockJavaBase = new EmptyJavaBase({...options, ...overrides});
|
||||
|
||||
await mockJavaBase.setupJava();
|
||||
|
||||
expect(jdkResolutionCache.restoreJdkResolution).not.toHaveBeenCalled();
|
||||
expect(jdkResolutionCache.registerJdkResolution).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('downloadAndVerify', () => {
|
||||
|
||||
Reference in New Issue
Block a user