fix: retry latest version lookup and add latest-fallback opt-out (#303)

* fix: retry latest version lookup and add latest-fallback opt-out

Resolving 'version: latest' made a single request to
https://get.helm.sh/helm-latest-version and, on any failure, silently
installed the hard-coded default version. Since 'latest' resolves to
Helm 4, that fallback moves a job back a whole major version while the
step stays green.

Retry the lookup a few times with a short backoff, treat non-2xx
responses as failures instead of installing the response body, and add
a 'latest-fallback' input so callers can make the step fail instead of
installing the default version. The default keeps the existing
behavior.

* fix: do not fall back across a major version by default

Make 'latest-fallback' default to 'false', so an exhausted lookup fails
the step with the underlying error instead of installing the built-in
default version, which can be a major behind what 'latest' resolves to.
Setting the input to 'true' restores the previous behavior.

* fix: keep latest-fallback on by default

Installing the built-in default version when 'latest' cannot be
determined is documented behavior for v5, so 'latest-fallback' now
defaults to 'true' and callers opt out with 'false'. The warning
emitted on fallback names the input, so the downgrade is easy to trace
and to turn off.

* fix: cancel the unread body of a failed latest version response

A non-2xx response from the latest version URL is turned into an error
without reading its body. Cancel the body first, so its connection is
released before the next attempt instead of waiting for garbage
collection.
This commit is contained in:
Thomas Seljen Tvedt
2026-11-04 10:50:29 -08:00
committed by GitHub
parent b7cb6a40fe
commit 531fb92ec6
4 changed files with 188 additions and 19 deletions
+57 -7
View File
@@ -34,7 +34,11 @@ export async function run() {
const downloadBaseURL = core.getInput('downloadBaseURL', {required: false})
if (version.toLocaleLowerCase() === 'latest') {
version = await getLatestHelmVersion()
// Only an explicit 'false' turns the fallback off, matching the
// input's default.
const fallbackToDefault =
core.getInput('latest-fallback').toLowerCase() !== 'false'
version = await getLatestHelmVersion(fallbackToDefault)
} else if (isMajorMinorShaped(version)) {
version = await resolveLatestPatchVersion(downloadBaseURL, version)
core.info(`Resolved latest patch Helm version to '${version}'`)
@@ -122,15 +126,61 @@ export function parseToolVersions(content: string): string {
return ''
}
// Gets the latest helm version or returns a default stable if getting latest fails
export async function getLatestHelmVersion(): Promise<string> {
const latestVersionURL = 'https://get.helm.sh/helm-latest-version'
// Number of attempts made to fetch the latest version before giving up, and
// the wait before the second attempt (each further wait doubles the previous).
const latestVersionAttempts = 3
const latestVersionRetryDelayMs = 1000
// Fetches the latest helm version. A failed request or a non-2xx response is
// retried with a short backoff, so a single transient failure does not decide
// the outcome. Throws the last error once every attempt has failed.
export async function fetchLatestHelmVersion(): Promise<string> {
let delayMs = latestVersionRetryDelayMs
for (let attempt = 1; ; attempt++) {
try {
const response = await fetch(latestVersionURL)
if (!response.ok) {
// The body is never read, so cancel it to release the connection
// before the next attempt.
await response.body?.cancel()
throw new Error(
`Unexpected HTTP ${response.status} from ${latestVersionURL}`
)
}
return (await response.text()).trim()
} catch (err) {
if (attempt >= latestVersionAttempts) {
throw err
}
core.info(
`Attempt ${attempt} of ${latestVersionAttempts} to fetch the latest Helm version failed: ${err instanceof Error ? err.message : String(err)}. Retrying in ${delayMs}ms`
)
await new Promise((resolve) => setTimeout(resolve, delayMs))
delayMs *= 2
}
}
}
// Gets the latest helm version. When it cannot be determined, returns the
// built-in default version with a warning, or throws when fallbackToDefault
// is off. The default version can be a major behind what 'latest' resolves
// to, so the warning names the input that turns the fallback off.
export async function getLatestHelmVersion(
fallbackToDefault = true
): Promise<string> {
try {
const response = await fetch('https://get.helm.sh/helm-latest-version')
const release = (await response.text()).trim()
return release
return await fetchLatestHelmVersion()
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
if (!fallbackToDefault) {
throw new Error(
`Unable to determine the latest Helm version: ${message}. Set 'latest-fallback' to 'true' to install the default version ${stableHelmVersion} instead, or request a specific version`
)
}
core.warning(
`Error while fetching latest Helm release: ${err instanceof Error ? err.message : String(err)}. Using default version ${stableHelmVersion}`
`Error while fetching latest Helm release: ${message}. Using default version ${stableHelmVersion}. Set 'latest-fallback' to 'false' to fail the step instead`
)
return stableHelmVersion
}