mirror of
https://gitea.com/actions/setup-java.git
synced 2026-07-11 19:41:57 +08:00
Support pinning java-version as "latest" (#1093)
* Support pinning java-version as "latest" Add a `latest` alias for the `java-version` input that floats to the newest available stable (GA) release. It is normalized to the SemVer wildcard at the base-installer layer and always resolves from remote (like `check-latest: true`). List-based distributions resolve it automatically via the existing newest-first matching. Corretto selects its newest available major; Oracle and GraalVM look up the newest GA major via the Adoptium API and request it, failing with an actionable error if that major isn't published yet. The jdkfile distribution rejects `latest`. Closes #832 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Clarify latest note for oracle/graalvm version resolution vs download Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * graalvm-community: float latest to its own newest GA release GraalVM Community publishes its releases on GitHub, so the `latest` alias now matches against that real release list using the SemVer wildcard instead of asking the Adoptium API for the newest GA major. This prevents `latest` from hard-failing when GraalVM lags behind a freshly released Java major (e.g. Adoptium reports 26 before GraalVM ships it). Oracle GraalVM has no listing endpoint, so it keeps deriving the newest major from Adoptium and errors clearly if that major is not yet published. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Give latest+qualifier inputs (e.g. latest-ea) a targeted error Inputs like 'latest-ea' had their '-ea' suffix stripped and fell through to the generic SemVer validation, failing with a confusing "'latest' is not valid SemVer" message even though 'latest' is supported. Add an explicit guard so any 'latest*' value other than exactly 'latest' throws a targeted error explaining that 'latest' resolves GA releases only and cannot be combined with '-ea' or other qualifiers. 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: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -23,6 +23,7 @@ export abstract class JavaBase {
|
||||
protected architecture: string;
|
||||
protected packageType: string;
|
||||
protected stable: boolean;
|
||||
protected latest: boolean;
|
||||
protected checkLatest: boolean;
|
||||
protected setDefault: boolean;
|
||||
protected verifySignature: boolean;
|
||||
@@ -37,9 +38,11 @@ export abstract class JavaBase {
|
||||
maxRetries: 3
|
||||
});
|
||||
|
||||
({version: this.version, stable: this.stable} = this.normalizeVersion(
|
||||
installerOptions.version
|
||||
));
|
||||
({
|
||||
version: this.version,
|
||||
stable: this.stable,
|
||||
latest: this.latest
|
||||
} = this.normalizeVersion(installerOptions.version));
|
||||
this.architecture = installerOptions.architecture || os.arch();
|
||||
this.packageType = installerOptions.packageType;
|
||||
this.checkLatest = installerOptions.checkLatest;
|
||||
@@ -66,7 +69,7 @@ export abstract class JavaBase {
|
||||
}
|
||||
|
||||
let foundJava = this.findInToolcache();
|
||||
if (foundJava && !this.checkLatest) {
|
||||
if (foundJava && !this.checkLatest && !this.latest) {
|
||||
core.info(`Resolved Java ${foundJava.version} from tool-cache`);
|
||||
} else {
|
||||
core.info('Trying to resolve the latest version from remote');
|
||||
@@ -267,6 +270,30 @@ export abstract class JavaBase {
|
||||
|
||||
protected normalizeVersion(version: string) {
|
||||
let stable = true;
|
||||
const latest = false;
|
||||
|
||||
// Support the `latest` alias (case-insensitive), which floats to the newest
|
||||
// available stable/GA release. It is translated to the SemVer wildcard `x`
|
||||
// so the existing "newest satisfying version wins" resolution applies.
|
||||
const normalized = version.trim().toLowerCase();
|
||||
if (normalized === 'latest') {
|
||||
return {
|
||||
version: 'x',
|
||||
stable: true,
|
||||
latest: true
|
||||
};
|
||||
}
|
||||
|
||||
// Reject `latest` combined with any qualifier (e.g. `latest-ea`). Such inputs
|
||||
// would otherwise have their `-ea` suffix stripped and fall through to the
|
||||
// generic SemVer check, which fails with a confusing "'latest' is not valid
|
||||
// SemVer" message even though `latest` is a supported value. Fail early with a
|
||||
// targeted explanation instead.
|
||||
if (normalized.startsWith('latest')) {
|
||||
throw new Error(
|
||||
`The 'latest' alias resolves stable (GA) releases only and cannot be combined with '-ea' or other qualifiers (received '${version}'). Use 'latest' on its own, or specify a concrete version.`
|
||||
);
|
||||
}
|
||||
|
||||
if (version.endsWith('-ea')) {
|
||||
version = version.replace(/-ea$/, '');
|
||||
@@ -294,7 +321,8 @@ export abstract class JavaBase {
|
||||
|
||||
return {
|
||||
version,
|
||||
stable
|
||||
stable,
|
||||
latest
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -59,10 +59,28 @@ export class CorrettoDistribution extends JavaBase {
|
||||
if (!this.stable) {
|
||||
throw new Error('Early access versions are not supported');
|
||||
}
|
||||
const availableVersions = await this.getAvailableVersions();
|
||||
|
||||
// The `latest` alias is normalized to the SemVer wildcard, but Corretto
|
||||
// matches on an exact major version, so resolve it to the newest available
|
||||
// major from Corretto's own list.
|
||||
if (this.latest) {
|
||||
const majors = availableVersions
|
||||
.map(item => parseInt(item.version, 10))
|
||||
.filter(major => Number.isFinite(major) && major > 0);
|
||||
|
||||
if (majors.length === 0) {
|
||||
throw new Error(
|
||||
'Could not determine the latest available Corretto major version from remote metadata'
|
||||
);
|
||||
}
|
||||
|
||||
version = Math.max(...majors).toString();
|
||||
}
|
||||
|
||||
if (version.includes('.')) {
|
||||
throw new Error('Only major versions are supported');
|
||||
}
|
||||
const availableVersions = await this.getAvailableVersions();
|
||||
const matchingVersions = availableVersions
|
||||
.filter(item => item.version == version)
|
||||
.map(item => {
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
extractJdkFile,
|
||||
getDownloadArchiveExtension,
|
||||
getGitHubHttpHeaders,
|
||||
getLatestMajorVersion,
|
||||
getNextPageUrlFromLinkHeader,
|
||||
isVersionSatisfies,
|
||||
MAX_PAGINATION_PAGES,
|
||||
@@ -119,6 +120,13 @@ export class GraalVMDistribution extends JavaBase {
|
||||
return this.findEABuildDownloadUrl(`${range}-ea`);
|
||||
}
|
||||
|
||||
// The `latest` alias is normalized to the SemVer wildcard. Oracle GraalVM
|
||||
// builds its download URLs from a concrete major and has no endpoint to list
|
||||
// releases, so resolve the newest available GA major from the Adoptium API.
|
||||
if (this.latest) {
|
||||
range = (await getLatestMajorVersion(this.http)).toString();
|
||||
}
|
||||
|
||||
const {platform, extension, major} = this.validateStableBuildRequest(range);
|
||||
|
||||
const fileUrl = this.constructFileUrl(
|
||||
@@ -203,6 +211,9 @@ export class GraalVMDistribution extends JavaBase {
|
||||
if (statusCode === HttpCodes.NotFound) {
|
||||
// Create the standard error with additional hint about checking the download URL
|
||||
const error = this.createVersionNotFoundError(range);
|
||||
if (this.latest) {
|
||||
error.message += `\nThe latest Java major version (${range}) is not yet available for the ${this.distribution} distribution. Please specify a concrete version instead of 'latest'.`;
|
||||
}
|
||||
error.message += `\nPlease check if this version is available at ${GRAALVM_DOWNLOAD_URL} . Pick a version from the list.`;
|
||||
throw error;
|
||||
}
|
||||
@@ -354,7 +365,27 @@ export class GraalVMCommunityDistribution extends GraalVMDistribution {
|
||||
}
|
||||
|
||||
const arch = this.getSupportedArchitecture();
|
||||
const {platform, extension} = this.validateStableBuildRequest(range);
|
||||
|
||||
// GraalVM Community publishes its releases on GitHub, so the `latest` alias
|
||||
// (normalized to the SemVer wildcard `x`) can float to the newest GA it
|
||||
// actually ships. Unlike Oracle GraalVM (which has no listing endpoint and
|
||||
// must derive the newest major from the Adoptium API), we match against the
|
||||
// real release list here, so `latest` never fails when GraalVM lags behind a
|
||||
// brand-new Java major.
|
||||
let platform: OsVersions;
|
||||
let extension: string;
|
||||
if (this.latest) {
|
||||
if (this.packageType !== 'jdk') {
|
||||
throw new Error(
|
||||
`${this.distribution} provides only the \`jdk\` package type`
|
||||
);
|
||||
}
|
||||
platform = this.getPlatform();
|
||||
extension = getDownloadArchiveExtension();
|
||||
} else {
|
||||
({platform, extension} = this.validateStableBuildRequest(range));
|
||||
}
|
||||
|
||||
// GraalVM Community asset names embed the platform, architecture and
|
||||
// archive type, e.g. `graalvm-community-jdk-21.0.2_linux-x64_bin.tar.gz`.
|
||||
const assetSuffix = `_${platform}-${arch}_bin.${extension}`;
|
||||
|
||||
@@ -22,6 +22,12 @@ export class LocalDistribution extends JavaBase {
|
||||
}
|
||||
|
||||
public async setupJava(): Promise<JavaInstallerResults> {
|
||||
if (this.latest) {
|
||||
throw new Error(
|
||||
"The 'latest' version alias is not supported for the 'jdkfile' distribution. Please specify a concrete version."
|
||||
);
|
||||
}
|
||||
|
||||
let foundJava = this.findInToolcache();
|
||||
|
||||
if (foundJava) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
import {
|
||||
extractJdkFile,
|
||||
getDownloadArchiveExtension,
|
||||
getLatestMajorVersion,
|
||||
renameWinArchive
|
||||
} from '../../util.js';
|
||||
import {HttpCodes} from '@actions/http-client';
|
||||
@@ -73,6 +74,14 @@ export class OracleDistribution extends JavaBase {
|
||||
const platform = this.getPlatform();
|
||||
const extension = getDownloadArchiveExtension();
|
||||
|
||||
// The `latest` alias is normalized to the SemVer wildcard. Oracle builds its
|
||||
// download URLs from a concrete major and has no endpoint to list releases,
|
||||
// so resolve the newest available GA major from the Adoptium API and use it.
|
||||
if (this.latest) {
|
||||
const latestMajor = await getLatestMajorVersion(this.http);
|
||||
range = latestMajor.toString();
|
||||
}
|
||||
|
||||
const isOnlyMajorProvided = !range.includes('.');
|
||||
const major = isOnlyMajorProvided ? range : range.split('.')[0];
|
||||
|
||||
@@ -113,6 +122,12 @@ export class OracleDistribution extends JavaBase {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.latest) {
|
||||
const error = this.createVersionNotFoundError(range);
|
||||
error.message += `\nThe latest Java major version (${range}) is not yet available for the Oracle JDK distribution. Please specify a concrete version instead of 'latest'.`;
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw this.createVersionNotFoundError(range);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user