5
0
mirror of https://gitea.com/actions/setup-java.git synced 2026-08-07 02:31:20 +00:00

Report concrete versions for floating Oracle JDK downloads (#1213)

* Fix floating Oracle JDK version resolution

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Update generated distribution bundles

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Harden floating artifact cache identity

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Regenerate setup bundle after cache hardening

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Temporarily enable hosted full validation

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Export hosted formatting results

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Apply repository formatting

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Run hosted validation after formatting

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Correct floating version regression tests

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Remove temporary validation wiring

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Cache checksum-less floating artifacts by their response fingerprint

Oracle and Oracle GraalVM do not always publish a `.sha256` sibling next
to a `/latest/` artifact. Those floating releases were excluded from both
the resolution cache and the JDK cache, so `cache-jdk` users lost caching
entirely for them.

A floating URL is a constant string, so it cannot serve as a cache
identity on its own — a stale entry would be reused forever. Instead,
derive a validator from the headers of the HEAD request that already
resolves the artifact: the ETag when present, otherwise `Last-Modified`
combined with `Content-Length`. Republishing changes the validator, which
changes the cache key, so a new build is downloaded rather than masked.

`getJdkReleaseIdentity` now falls back to that fingerprint before the
URL, and the floating cache gates ask whether the release has a stable
identity (checksum or fingerprint) rather than a checksum specifically. A
floating release with neither is still left uncached.

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: Bruno Borges <brborges@microsoft.com>
This commit is contained in:
Julien Dubois
2026-08-05 17:57:27 +02:00
committed by GitHub
parent ab597f914a
commit f4bfb3ddea
20 changed files with 1037 additions and 66 deletions
+232 -2
View File
@@ -148,6 +148,47 @@ class EmptyJavaBase extends JavaBase {
}
}
class FloatingJavaBase extends JavaBase {
static actualVersion = '21.0.8+9';
static checksum: string | undefined = 'artifact-one';
static fingerprint: string | undefined = undefined;
constructor(installerOptions: JavaInstallerOptions) {
super('Floating', installerOptions);
}
protected async downloadTool(): Promise<JavaInstallerResults> {
return {
version: FloatingJavaBase.actualVersion,
path: path.join(
'toolcache',
this.toolcacheFolderName,
FloatingJavaBase.actualVersion.replace('+', '-'),
this.architecture
)
};
}
protected async findPackageForDownload(): Promise<JavaDownloadRelease> {
return {
version: '21',
url: 'https://example.com/java/21/latest/jdk-21.tar.gz',
checksum: FloatingJavaBase.checksum
? {
algorithm: 'sha256',
value: FloatingJavaBase.checksum
}
: undefined,
floating: true,
fingerprint: FloatingJavaBase.fingerprint
};
}
protected requiresRemoteResolution(): boolean {
return true;
}
}
describe('findInToolcache', () => {
const actualJavaVersion = '11.0.8';
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x64');
@@ -397,6 +438,7 @@ describe('setupJava', () => {
spyCoreError.mockImplementation(() => undefined);
jest.spyOn(os, 'arch').mockReturnValue('x86' as ReturnType<typeof os.arch>);
FloatingJavaBase.fingerprint = undefined;
});
afterEach(() => {
@@ -476,6 +518,179 @@ describe('setupJava', () => {
);
});
it('uses the concrete versions of two different floating artifacts under the same major', async () => {
spyTcFindAllVersions.mockReturnValue(['21.0.8-9']);
spyGetToolcachePath.mockImplementation(
(_toolname: string, version: string, architecture: string) =>
path.join('toolcache', 'Java_Floating_jdk', version, architecture)
);
(jdkResolutionCache.restoreJdkResolution as jest.Mock).mockResolvedValue(
undefined
);
(jdkCache.restoreJdk as jest.Mock).mockResolvedValue(false);
FloatingJavaBase.actualVersion = '21.0.8+9';
FloatingJavaBase.checksum = 'artifact-one';
const first = new FloatingJavaBase({
version: '21',
architecture: 'x64',
packageType: 'jdk',
checkLatest: false,
cacheJdk: true
});
await expect(first.setupJava()).resolves.toEqual({
version: '21.0.8+9',
path: path.join('toolcache', 'Java_Floating_jdk', '21.0.8-9', 'x64')
});
FloatingJavaBase.actualVersion = '21.0.9+7';
FloatingJavaBase.checksum = 'artifact-two';
const second = new FloatingJavaBase({
version: '21',
architecture: 'x64',
packageType: 'jdk',
checkLatest: false,
cacheJdk: true
});
await expect(second.setupJava()).resolves.toEqual({
version: '21.0.9+7',
path: path.join('toolcache', 'Java_Floating_jdk', '21.0.9-7', 'x64')
});
expect(spyCoreSetOutput).toHaveBeenNthCalledWith(3, 'version', '21.0.8+9');
expect(spyCoreSetOutput).toHaveBeenNthCalledWith(6, 'version', '21.0.9+7');
expect(jdkCache.registerJdk).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
version: '21.0.8+9',
source: 'sha256:artifact-one'
})
);
expect(jdkCache.registerJdk).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
version: '21.0.9+7',
source: 'sha256:artifact-two'
})
);
expect(jdkResolutionCache.registerJdkResolution).toHaveBeenNthCalledWith(
2,
expect.objectContaining({source: 'sha256:artifact-two'}),
expect.objectContaining({version: '21.0.9+7', floating: true})
);
});
it('does not trust a matching tool-cache version for a floating artifact', async () => {
spyTcFindAllVersions.mockReturnValue(['21.0.8-9']);
spyGetToolcachePath.mockReturnValue(
path.join('toolcache', 'Java_Floating_jdk', '21.0.8-9', 'x64')
);
(jdkResolutionCache.restoreJdkResolution as jest.Mock).mockResolvedValue({
release: {
version: '21.0.8+9',
url: 'https://example.com/java/21/latest/jdk-21.tar.gz',
checksum: {algorithm: 'sha256', value: 'artifact-republished'},
floating: true
},
fresh: true
});
(jdkCache.restoreJdk as jest.Mock).mockResolvedValue(false);
FloatingJavaBase.actualVersion = '21.0.8+9';
FloatingJavaBase.checksum = 'artifact-republished';
const distribution = new FloatingJavaBase({
version: '21',
architecture: 'x64',
packageType: 'jdk',
checkLatest: false,
cacheJdk: true
});
const downloadTool = jest.spyOn(distribution as any, 'downloadTool');
await distribution.setupJava();
expect(jdkCache.restoreJdk).toHaveBeenCalled();
expect(downloadTool).toHaveBeenCalled();
});
it('does not cache a floating artifact with no way to identify its bytes', async () => {
spyTcFindAllVersions.mockReturnValue(['21.0.8-9']);
spyGetToolcachePath.mockReturnValue(
path.join('toolcache', 'Java_Floating_jdk', '21.0.8-9', 'x64')
);
FloatingJavaBase.actualVersion = '21.0.8+9';
FloatingJavaBase.checksum = undefined;
FloatingJavaBase.fingerprint = undefined;
const distribution = new FloatingJavaBase({
version: '21',
architecture: 'x64',
packageType: 'jdk',
checkLatest: false,
cacheJdk: true
});
await distribution.setupJava();
expect(jdkResolutionCache.restoreJdkResolution).not.toHaveBeenCalled();
expect(jdkResolutionCache.registerJdkResolution).not.toHaveBeenCalled();
expect(jdkCache.restoreJdk).not.toHaveBeenCalled();
expect(jdkCache.registerJdk).not.toHaveBeenCalled();
});
it('caches a checksum-less floating artifact identified by its response fingerprint', async () => {
spyTcFindAllVersions.mockReturnValue([]);
spyGetToolcachePath.mockReturnValue(
path.join('toolcache', 'Java_Floating_jdk', '21.0.8-9', 'x64')
);
FloatingJavaBase.actualVersion = '21.0.8+9';
FloatingJavaBase.checksum = undefined;
FloatingJavaBase.fingerprint = 'etag:"artifact-one"';
const distribution = new FloatingJavaBase({
version: '21',
architecture: 'x64',
packageType: 'jdk',
checkLatest: false,
cacheJdk: true
});
await distribution.setupJava();
// The fingerprint changes when the vendor republishes, so it is a safe
// identity even though no checksum is available.
expect(jdkResolutionCache.registerJdkResolution).toHaveBeenCalledWith(
expect.objectContaining({source: 'etag:"artifact-one"'}),
expect.objectContaining({version: '21.0.8+9'})
);
expect(jdkCache.registerJdk).toHaveBeenCalledWith(
expect.objectContaining({source: 'etag:"artifact-one"'})
);
});
it('separates the cache identities of two builds served by the same floating URL', async () => {
const sources: string[] = [];
(jdkCache.registerJdk as jest.Mock).mockImplementation((entry: any) => {
sources.push(entry.source);
});
spyTcFindAllVersions.mockReturnValue([]);
spyGetToolcachePath.mockReturnValue(
path.join('toolcache', 'Java_Floating_jdk', '21.0.8-9', 'x64')
);
FloatingJavaBase.actualVersion = '21.0.8+9';
FloatingJavaBase.checksum = undefined;
for (const fingerprint of ['etag:"before"', 'etag:"after"']) {
FloatingJavaBase.fingerprint = fingerprint;
await new FloatingJavaBase({
version: '21',
architecture: 'x64',
packageType: 'jdk',
checkLatest: false,
cacheJdk: true
}).setupJava();
}
expect(sources).toEqual(['etag:"before"', 'etag:"after"']);
});
it('should download java when force-download is enabled, even if the version is cached', async () => {
mockJavaBase = new EmptyJavaBase({
version: actualJavaVersion,
@@ -1074,7 +1289,7 @@ describe('setupJava', () => {
);
});
it('does not record a floating release', async () => {
it('records the concrete version for a checksum-bound floating release', async () => {
mockJavaBase = new EmptyJavaBase(options);
jest
.spyOn(mockJavaBase as any, 'findPackageForDownload')
@@ -1087,7 +1302,22 @@ describe('setupJava', () => {
await mockJavaBase.setupJava();
expect(jdkResolutionCache.registerJdkResolution).not.toHaveBeenCalled();
expect(jdkResolutionCache.registerJdkResolution).toHaveBeenCalledWith(
{
distribution: 'Empty',
packageType: 'jdk',
architecture: 'x86',
versionSpec: '11.0.9',
stable: true,
source: 'sha256:abc'
},
{
version: '11.0.9',
url: 'https://example.com/java/11/latest/jdk-11.tar.gz',
checksum: {algorithm: 'sha256', value: 'abc'},
floating: true
}
);
});
it.each([
@@ -72,6 +72,7 @@ jest.unstable_mockModule('../../src/util.js', () => ({
...realUtil,
extractJdkFile: jest.fn(),
getDownloadArchiveExtension: jest.fn(),
getJavaVersionFromReleaseFile: jest.fn(),
renameWinArchive: jest.fn(),
getGitHubHttpHeaders: jest.fn().mockReturnValue({Accept: 'application/json'})
}));
@@ -363,6 +364,30 @@ describe('GraalVMDistribution', () => {
path: '/cached/java/path'
});
});
it('caches Oracle GraalVM floating artifacts under their installed version', async () => {
(util.getJavaVersionFromReleaseFile as jest.Mock<any>).mockReturnValue(
'21.0.9+7'
);
const floatingRelease = {
version: '21',
url: 'https://example.com/graalvm/latest/graalvm-jdk-21.tar.gz',
floating: true
};
const result = await (distribution as any).downloadTool(floatingRelease);
expect(tc.cacheDir).toHaveBeenCalledWith(
path.join('/tmp/extracted', 'graalvm-jdk-17.0.5'),
'Java_GraalVM_jdk',
'21.0.9+7',
'x64'
);
expect(result).toEqual({
version: '21.0.9+7',
path: '/cached/java/path'
});
});
});
describe('findPackageForDownload', () => {
@@ -451,6 +476,33 @@ describe('GraalVMDistribution', () => {
});
});
it.each([
['21', 'etag:"graalvm-latest"'],
['17.0.5', undefined]
])(
'fingerprints only the floating artifact for version %s',
async (input, expected) => {
mockHttpClient.head.mockResolvedValue({
message: {statusCode: 200, headers: {etag: '"graalvm-latest"'}}
} as any);
const result = await (distribution as any).findPackageForDownload(
input
);
// Without a fingerprint the constant `/latest/` URL would key a cache
// entry that never invalidates when Oracle republishes the artifact.
expect(result.fingerprint).toBe(expected);
}
);
it('always resolves Oracle GraalVM major-only requests remotely', () => {
expect((distribution as any).requiresRemoteResolution()).toBe(true);
expect((communityDistribution as any).requiresRemoteResolution()).toBe(
false
);
});
it('should throw error for unsupported architecture', async () => {
distribution = new GraalVMDistribution({
...defaultOptions,
@@ -147,6 +147,27 @@ describe('findPackageForDownload', () => {
expect(result.floating).toBe(url.includes('/latest/'));
});
it.each([
['21', 'etag:"oracle-latest"'],
['21.0.1', undefined]
])(
'fingerprints only the floating artifact for version %s',
async (input, expected) => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'head');
spyHttpClient.mockResolvedValue({
message: {statusCode: 200, headers: {etag: '"oracle-latest"'}}
});
const result = await distribution['findPackageForDownload'](input);
jest.restoreAllMocks();
// Without a fingerprint the constant `/latest/` URL would key a cache
// entry that never invalidates when Oracle republishes the artifact.
expect(result.fingerprint).toBe(expected);
}
);
it('fetches the authoritative sha256 checksum for the resolved archive', async () => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'head');
spyHttpClient.mockResolvedValue({message: {statusCode: 200}});
@@ -164,6 +185,17 @@ describe('findPackageForDownload', () => {
expect(spyHttpClientGet).toHaveBeenCalledTimes(1);
});
it('always resolves major-only requests remotely', () => {
expect(distribution['requiresRemoteResolution']()).toBe(true);
const exactDistribution = new OracleDistribution({
version: '21.0.8',
architecture: 'x64',
packageType: 'jdk',
checkLatest: false
});
expect(exactDistribution['requiresRemoteResolution']()).toBe(false);
});
it.each([
['amd64', 'x64'],
['arm64', 'aarch64']
+15 -1
View File
@@ -248,7 +248,8 @@ describe('JDK resolution cache', () => {
algorithm: 'sha512',
value: 'def456',
source: 'https://example.com/a.sha512'
}
},
floating: true
};
restoreWith(JSON.stringify(full), 'setup-java-jdkres-v1-old');
@@ -312,6 +313,19 @@ describe('JDK resolution cache', () => {
state.length
);
});
it('uses different keys for different floating artifact identities', () => {
createRunnerTemp();
registerJdkResolution({...request, source: 'sha256:first'}, release);
registerJdkResolution({...request, source: 'sha256:second'}, release);
const state = JSON.parse(
jest.mocked(core.saveState).mock.calls.at(-1)![1] as string
);
expect(new Set(state.map((item: {key: string}) => item.key)).size).toBe(
state.length
);
});
});
describe('saveJdkResolutionCaches', () => {
+97 -1
View File
@@ -44,7 +44,12 @@ jest.unstable_mockModule('@actions/http-client', () => ({
const tc = await import('@actions/tool-cache');
const exec = await import('@actions/exec');
const io = await import('@actions/io');
const {cacheJdkDir, extractJdkFile} = await import('../src/util.js');
const {
cacheJdkDir,
extractJdkFile,
getArtifactFingerprint,
getJavaVersionFromReleaseFile
} = await import('../src/util.js');
const originalToolCache = process.env['RUNNER_TOOL_CACHE'];
const originalTemp = process.env['RUNNER_TEMP'];
@@ -298,6 +303,41 @@ describe('cacheJdkDir', () => {
});
});
describe('getJavaVersionFromReleaseFile', () => {
it.each([
['JAVA_RUNTIME_VERSION="21.0.9+7-LTS-123"', '21.0.9+7'],
['JAVA_RUNTIME_VERSION="17.0.12+8-jvmci-23.1-b52"', '17.0.12+8'],
['JAVA_RUNTIME_VERSION="25+36-LTS"', '25.0.0+36'],
['JAVA_VERSION="25.0.1"', '25.0.1'],
['JAVA_VERSION="25"', '25.0.0']
])('reads a concrete version from %s', (contents, expected) => {
const javaHome = createJdkDir();
fs.writeFileSync(path.join(javaHome, 'release'), contents);
expect(getJavaVersionFromReleaseFile(javaHome)).toBe(expected);
});
it('reads the macOS Contents/Home release file', () => {
const javaHome = path.join(workDir, 'macos-jdk');
fs.mkdirSync(path.join(javaHome, 'Contents', 'Home'), {recursive: true});
fs.writeFileSync(
path.join(javaHome, 'Contents', 'Home', 'release'),
'JAVA_RUNTIME_VERSION="21.0.9+7-LTS"'
);
expect(getJavaVersionFromReleaseFile(javaHome)).toBe('21.0.9+7');
});
it('fails when the JDK release metadata has no usable version', () => {
const javaHome = createJdkDir();
fs.writeFileSync(path.join(javaHome, 'release'), 'IMPLEMENTOR="Oracle"');
expect(() => getJavaVersionFromReleaseFile(javaHome)).toThrow(
/Unable to determine the installed Java version/
);
});
});
describe('extractJdkFile', () => {
it('uses pigz for tarballs when it is available', async () => {
(io.which as jest.Mock).mockResolvedValue('/usr/bin/pigz' as never);
@@ -410,3 +450,59 @@ describe('extractJdkFile', () => {
expect(exec.exec).not.toHaveBeenCalled();
});
});
describe('getArtifactFingerprint', () => {
it('prefers the ETag over the other validators', () => {
expect(
getArtifactFingerprint({
etag: '"abc123"',
'last-modified': 'Wed, 21 Oct 2026 07:28:00 GMT',
'content-length': '195000000'
})
).toBe('etag:"abc123"');
});
it('combines the last-modified date and the content length without an ETag', () => {
expect(
getArtifactFingerprint({
'last-modified': 'Wed, 21 Oct 2026 07:28:00 GMT',
'content-length': '195000000'
})
).toBe('mtime:Wed, 21 Oct 2026 07:28:00 GMT;length:195000000');
});
it.each([
['no validators', {}],
[
'only a last-modified date',
{'last-modified': 'Wed, 21 Oct 2026 07:28:00 GMT'}
],
['only a content length', {'content-length': '195000000'}],
[
'blank validators',
{etag: ' ', 'last-modified': '', 'content-length': ''}
],
['missing headers', undefined]
])('returns undefined for %s', (_label, headers) => {
expect(getArtifactFingerprint(headers)).toBeUndefined();
});
it('uses the first value of a repeated header', () => {
expect(getArtifactFingerprint({etag: ['"first"', '"second"'] as any})).toBe(
'etag:"first"'
);
});
it('distinguishes a republished artifact from the previous one', () => {
const before = getArtifactFingerprint({
'last-modified': 'Wed, 21 Oct 2026 07:28:00 GMT',
'content-length': '195000000'
});
const after = getArtifactFingerprint({
'last-modified': 'Thu, 22 Oct 2026 09:03:00 GMT',
'content-length': '195400000'
});
expect(before).not.toBe(after);
});
});
+17
View File
@@ -12,6 +12,8 @@ fi
EXPECTED_JAVA_VERSION=$1
EXPECTED_PATH=$2
SETUP_JAVA_VERSION=$3
REQUIRE_CONCRETE_VERSION=$4
EXPECTED_JAVA_VERSION=$(echo $EXPECTED_JAVA_VERSION | cut -d'+' -f1)
if [[ $EXPECTED_JAVA_VERSION == 8 ]] || [[ $EXPECTED_JAVA_VERSION == 8.* ]]; then
@@ -31,6 +33,21 @@ if [ -z "$GREP_RESULT" ]; then
exit 1
fi
if [ -n "$SETUP_JAVA_VERSION" ]; then
OUTPUT_JAVA_VERSION=$(echo "$SETUP_JAVA_VERSION" | cut -d'+' -f1)
OUTPUT_GREP_RESULT=$(echo "$ACTUAL_JAVA_VERSION" | grep -E "^(openjdk|java) version \"$OUTPUT_JAVA_VERSION")
if [ -z "$OUTPUT_GREP_RESULT" ]; then
echo "::error::The version output does not match the installed Java version"
echo "Version output: $SETUP_JAVA_VERSION"
exit 1
fi
if [ "$REQUIRE_CONCRETE_VERSION" = "true" ] && [ "$OUTPUT_JAVA_VERSION" = "$EXPECTED_JAVA_VERSION" ]; then
echo "::error::Expected a concrete version output for a floating JDK"
echo "Version output: $SETUP_JAVA_VERSION"
exit 1
fi
fi
if [ "$EXPECTED_PATH" != "$JAVA_HOME" ]; then
echo "::error::Unexpected path"
echo "Actual path: $JAVA_HOME"