2020-04-29 20:57:02 +03:00
import * as path from 'path' ;
import * as core from '@actions/core' ;
import * as tc from '@actions/tool-cache' ;
import * as exec from '@actions/exec' ;
2026-07-15 22:10:44 +05:30
import { ExecOptions } from '@actions/exec' ;
2024-03-26 14:56:00 +01:00
import * as httpm from '@actions/http-client' ;
2026-06-19 05:17:28 +05:30
import * as fs from 'fs' ;
import * as semver from 'semver' ;
2026-07-15 22:10:44 +05:30
import { IS_WINDOWS , IS_LINUX , getDownloadFileName } from './utils.js' ;
2025-04-17 19:25:44 +05:30
import { IToolRelease } from '@actions/tool-cache' ;
2020-04-29 20:57:02 +03:00
2026-09-09 00:31:48 +02:00
const DEFAULT_REPO_OWNER = 'actions' ;
const DEFAULT_REPO_NAME = 'python-versions' ;
const DEFAULT_REPO_BRANCH = 'main' ;
const DEFAULT_MIRROR = `https://raw.githubusercontent.com/ ${ DEFAULT_REPO_OWNER } / ${ DEFAULT_REPO_NAME } / ${ DEFAULT_REPO_BRANCH } ` ;
// Matches https://raw.githubusercontent.com/{owner}/{repo}/{branch} and the
// equivalent https://raw.githubusercontent.com/{owner}/{repo}/refs/heads/{branch}
// form, capturing the bare branch in both. The GitHub tree API wants the bare
// branch, so the optional refs/heads/ prefix is consumed, not captured.
const REPO_COORDS_RE =
/^https:\/\/raw\.githubusercontent\.com\/([^/]+)\/([^/]+)\/(?:refs\/heads\/)?([^/]+)\/?$/ ;
function getToken () : string {
return core . getInput ( 'token' );
}
function getMirrorToken () : string {
return core . getInput ( 'mirror-token' );
}
// Memoized per raw input value so the mirror is validated once per run rather
// than on every call. `getManifestUrl()` is also used to build the "version not
// found" message in find-python.ts, where re-validating would replace the real
// cause with an invalid-mirror error.
const mirrorCache = new Map < string , { url : string } | { error : Error }>();
function getMirror () : string {
const input = core . getInput ( 'mirror' ) || DEFAULT_MIRROR ;
let resolved = mirrorCache . get ( input );
if ( ! resolved ) {
const url = input . trim (). replace ( /\/+$/ , '' );
try {
new URL ( url );
resolved = { url };
} catch {
resolved = { error : new Error ( `Invalid 'mirror' URL: " ${ url } "` )};
}
mirrorCache . set ( input , resolved );
}
if ( 'error' in resolved ) throw resolved . error ;
return resolved . url ;
}
export function getManifestUrl () : string {
return ` ${ getMirror () } /versions-manifest.json` ;
}
// Whether the user set `mirror` to something other than the built-in default.
// action.yml gives `mirror` a default, so core.getInput('mirror') is never
// empty; callers that want "did the user opt into a custom mirror" must compare
// against DEFAULT_MIRROR rather than test for a falsy input. Normalizes the
// same way getMirror() does but never throws, so a warning path can call it
// even when the mirror is malformed.
export function isMirrorCustomized () : boolean {
const input = core . getInput ( 'mirror' );
if ( ! input ) return false ;
return input . trim (). replace ( /\/+$/ , '' ) !== DEFAULT_MIRROR ;
}
// Origin (scheme + host + port) of the mirror, so `mirror-token` is matched
// against the exact origin the user nominated. Comparing origin rather than
// host alone means a manifest served over https that points a download_url at
// http://same-host/... does NOT get the token — the scheme must match too.
// Deliberately not wrapped in try/catch: an invalid mirror throws here just as
// it does in getManifestUrl(), so both agree a bad mirror is fatal.
function getMirrorOrigin () : string {
return new URL ( getMirror ()). origin ;
}
function isGitHubHost ( host : string ) : boolean {
return (
host === 'github.com' ||
host . endsWith ( '.github.com' ) ||
host . endsWith ( '.githubusercontent.com' )
);
}
// Warned at most once per distinct mirror; resolveRepoCoords() is called from
// several paths within a single run.
const warnedMirrors = new Set < string >();
export function resolveRepoCoords () : {
owner : string ;
repo : string ;
branch : string ;
} | null {
const mirror = getMirror ();
const m = REPO_COORDS_RE . exec ( mirror );
if ( m ) return { owner : m [ 1 ], repo : m [ 2 ], branch : m [ 3 ]};
// A raw.githubusercontent.com URL that doesn't parse is a branch name
// containing a slash (e.g. .../{owner}/{repo}/feature/riscv), which is
// indistinguishable from a deeper path. getMirror() succeeded above, so the
// URL is well-formed and this parse cannot throw.
const isRawGitHub = new URL ( mirror ). host === 'raw.githubusercontent.com' ;
if ( ! warnedMirrors . has ( mirror ) && isRawGitHub ) {
warnedMirrors . add ( mirror );
core . warning (
`Could not parse owner/repo/branch out of mirror " ${ mirror } ", so the manifest will be fetched directly from the raw URL instead of through the GitHub REST API. ` +
`The request is still authenticated with your token, because raw.githubusercontent.com is a GitHub host. ` +
`Branch names containing '/' are not supported for the REST API path; use a branch without a slash if you want the manifest fetched through the API.`
);
}
return null ;
}
// Mirror origin with `mirror-token` set gets the token verbatim, so internal
// mirrors can choose their own scheme (Bearer, Basic, ...). GitHub hosts get
// `token ${token}`. Anything else is anonymous — neither credential is sent to
// a host the user didn't nominate.
function authForUrl ( url : string ) : string | undefined {
let parsed : URL ;
try {
parsed = new URL ( url );
} catch {
return undefined ;
}
const mirrorToken = getMirrorToken ();
if ( mirrorToken && parsed . origin === getMirrorOrigin ()) return mirrorToken ;
const token = getToken ();
if ( token && isGitHubHost ( parsed . host )) return `token ${ token } ` ;
return undefined ;
}
2020-04-29 20:57:02 +03:00
2026-06-19 05:17:28 +05:30
interface LinuxOsRelease {
id : string ;
versionId : string ;
}
function getLinuxOsRelease () : LinuxOsRelease | null {
try {
const content = fs . readFileSync ( '/etc/os-release' , 'utf8' );
const lines = content . split ( '\n' );
let id = '' ;
let versionId = '' ;
for ( const line of lines ) {
const parts = line . split ( '=' );
if ( parts . length === 2 ) {
const key = parts [ 0 ]. trim ();
const value = parts [ 1 ]. trim (). replace ( /^"/ , '' ). replace ( /"$/ , '' );
if ( key === 'ID' ) id = value ;
if ( key === 'VERSION_ID' ) versionId = value ;
}
}
if ( id && versionId ) {
return { id , versionId };
}
return null ;
} catch {
return null ;
}
}
function findRhelRelease (
semanticVersionSpec : string ,
architecture : string ,
manifest : tc.IToolRelease [],
osVersion : string
) : tc . IToolRelease | undefined {
for ( const candidate of manifest ) {
const version = candidate . version ;
core . debug ( `check ${ version } satisfies ${ semanticVersionSpec } ` );
if ( ! semver . satisfies ( version , semanticVersionSpec )) continue ;
const file = candidate . files . find ( item => {
core . debug (
` ${ item . arch } === ${ architecture } && ${ item . platform } ===rhel && ${ item . platform_version } === ${ osVersion } `
);
const archMatch = item . arch === architecture ;
const platformMatch = item . platform === 'rhel' ;
const versionMatch =
! item . platform_version ||
item . platform_version === osVersion ||
osVersion . startsWith ( item . platform_version );
return archMatch && platformMatch && versionMatch ;
});
if ( file ) {
core . debug ( `matched ${ candidate . version } ` );
const result = Object . assign ({}, candidate );
result . files = [ file ];
return result ;
}
}
return undefined ;
}
2026-07-15 04:32:48 +05:30
const MANIFEST_FETCH_MAX_ATTEMPTS = 3 ;
const MANIFEST_FETCH_RETRY_BASE_DELAY_MS = 1000 ;
2020-04-29 20:57:02 +03:00
export async function findReleaseFromManifest (
semanticVersionSpec : string ,
2022-07-25 16:54:04 +02:00
architecture : string ,
manifest : tc.IToolRelease [] | null
2020-04-29 20:57:02 +03:00
) : Promise < tc.IToolRelease | undefined > {
2022-07-25 16:54:04 +02:00
if ( ! manifest ) {
manifest = await getManifest ();
}
2026-06-19 05:17:28 +05:30
// On RHEL, tc.findFromManifest() won't match because os.platform() returns 'linux'
// but manifest entries use platform 'rhel'. Use custom filtering for RHEL.
if ( IS_LINUX ) {
const osRelease = getLinuxOsRelease ();
if ( osRelease && osRelease . id === 'rhel' ) {
core . debug (
`Detected RHEL ${ osRelease . versionId } , using custom manifest filtering`
);
return findRhelRelease (
semanticVersionSpec ,
architecture ,
manifest ,
osRelease . versionId
);
}
}
2022-07-25 16:54:04 +02:00
const foundRelease = await tc . findFromManifest (
2020-04-29 20:57:02 +03:00
semanticVersionSpec ,
2020-07-17 12:58:03 +03:00
false ,
2020-04-29 20:57:02 +03:00
manifest ,
architecture
);
2022-07-25 16:54:04 +02:00
return foundRelease ;
}
2026-06-19 05:17:28 +05:30
2025-04-17 19:25:44 +05:30
function isIToolRelease ( obj : any ) : obj is IToolRelease {
return (
typeof obj === 'object' &&
obj !== null &&
typeof obj . version === 'string' &&
typeof obj . stable === 'boolean' &&
Array . isArray ( obj . files ) &&
obj . files . every (
( file : any ) =>
typeof file . filename === 'string' &&
typeof file . platform === 'string' &&
typeof file . arch === 'string' &&
typeof file . download_url === 'string'
)
);
}
2026-06-19 05:17:28 +05:30
2026-07-15 04:32:48 +05:30
// Rejects empty or truncated manifest responses.
function isValidManifest ( manifest : unknown ) : manifest is tc . IToolRelease [] {
return (
Array . isArray ( manifest ) &&
manifest . length > 0 &&
manifest . every ( isIToolRelease )
);
}
function sleep ( ms : number ) : Promise < void > {
return new Promise ( resolve => setTimeout ( resolve , ms ));
}
// HTTP 403/429 from http-client (`statusCode`) or tool-cache (`httpStatusCode`).
function isRateLimitError ( err : unknown ) : boolean {
const e = err as
| { httpStatusCode? : number ; statusCode? : number }
| null
| undefined ;
const status = e ? . httpStatusCode ?? e ? . statusCode ;
return status === 403 || status === 429 ;
}
// Fetches and validates a manifest, retrying transient failures with backoff.
async function fetchValidManifest (
source : string ,
fetcher : () => Promise < tc.IToolRelease [] >
) : Promise < tc.IToolRelease [] > {
let lastError : Error | undefined ;
let attempts = 0 ;
for ( let attempt = 1 ; attempt <= MANIFEST_FETCH_MAX_ATTEMPTS ; attempt ++ ) {
attempts = attempt ;
try {
const manifest = await fetcher ();
if ( isValidManifest ( manifest )) {
return manifest ;
}
throw new Error (
`The manifest fetched from ${ source } is empty, truncated, or does not contain any valid tool release entries.`
);
} catch ( err ) {
lastError = err instanceof Error ? err : new Error ( String ( err ));
core . debug (
`Attempt ${ attempt } / ${ MANIFEST_FETCH_MAX_ATTEMPTS } to fetch the manifest from ${ source } failed: ${ lastError . message } `
);
// Rate limits won't clear within the backoff window; fall back instead.
if ( isRateLimitError ( err )) {
core . debug (
` ${ source } is rate-limited; skipping retries for this source.`
);
break ;
}
if ( attempt < MANIFEST_FETCH_MAX_ATTEMPTS ) {
const delay = MANIFEST_FETCH_RETRY_BASE_DELAY_MS * 2 ** ( attempt - 1 );
core . debug ( `Retrying in ${ delay } ms...` );
await sleep ( delay );
}
}
}
throw new Error (
`Failed to fetch a valid manifest from ${ source } after ${ attempts } attempt(s): ${ lastError ? . message } `
);
}
2024-03-26 14:56:00 +01:00
export async function getManifest () : Promise < tc.IToolRelease [] > {
2026-09-09 00:31:48 +02:00
// Only GitHub repo mirrors can be fetched via the API. Checking up front
// avoids burning MANIFEST_FETCH_MAX_ATTEMPTS with backoff on a throw that
// could never succeed.
if ( resolveRepoCoords ()) {
try {
return await fetchValidManifest ( 'the GitHub API' , getManifestFromRepo );
} catch ( err ) {
core . debug ( 'Fetching the manifest via the API failed.' );
if ( err instanceof Error ) {
core . debug ( err . message );
} else {
core . debug ( 'An unexpected error occurred while fetching the manifest.' );
}
2024-03-26 14:56:00 +01:00
}
2026-09-09 00:31:48 +02:00
} else {
core . debug (
`Mirror " ${ getMirror () } " is not a GitHub repo URL; fetching the manifest by URL.`
);
2024-03-26 14:56:00 +01:00
}
2026-07-15 04:32:48 +05:30
try {
return await fetchValidManifest ( 'the raw URL' , getManifestFromURL );
} catch ( err ) {
const message = err instanceof Error ? err.message : String ( err );
// Fail loudly so the action doesn't exit 0 without installing Python.
throw new Error (
2026-07-15 22:10:44 +05:30
`Failed to fetch the Python versions manifest. The response was empty, truncated, or invalid, and all retries were exhausted. ${ message } ` ,
{ cause : err }
2026-07-15 04:32:48 +05:30
);
}
2024-03-26 14:56:00 +01:00
}
export function getManifestFromRepo () : Promise < tc.IToolRelease [] > {
2026-09-09 00:31:48 +02:00
const coords = resolveRepoCoords ();
if ( ! coords ) {
throw new Error (
`Mirror " ${ getMirror () } " is not a GitHub repo URL; falling back to raw URL fetch.`
);
}
2022-07-25 16:54:04 +02:00
core . debug (
2026-09-09 00:31:48 +02:00
`Getting manifest from ${ coords . owner } / ${ coords . repo } @ ${ coords . branch } `
2022-07-25 16:54:04 +02:00
);
2026-09-09 00:31:48 +02:00
// This only runs for GitHub repo mirrors, where `mirror-token` is the user's
// explicit intent for that repo. The target is always api.github.com, which
// requires the `token ` prefix, so the host rule in authForUrl() doesn't
// apply here.
const token = getToken ();
const mirrorToken = getMirrorToken ();
const auth = mirrorToken
? `token ${ mirrorToken } `
: token
? `token ${ token } `
: undefined ;
return tc . getManifestFromRepo ( coords . owner , coords . repo , auth , coords . branch );
2020-04-29 20:57:02 +03:00
}
2024-03-26 14:56:00 +01:00
export async function getManifestFromURL () : Promise < tc.IToolRelease [] > {
core . debug ( 'Falling back to fetching the manifest using raw URL.' );
2026-09-09 00:31:48 +02:00
const manifestUrl = getManifestUrl ();
2024-03-26 14:56:00 +01:00
const http : httpm.HttpClient = new httpm . HttpClient ( 'tool-cache' );
2026-09-09 00:31:48 +02:00
const auth = authForUrl ( manifestUrl );
const response = await http . getJson < tc.IToolRelease [] >(
manifestUrl ,
auth ? { authorization : auth } : undefined
);
2024-03-26 14:56:00 +01:00
if ( ! response . result ) {
2026-09-09 00:31:48 +02:00
throw new Error ( `Unable to get manifest from ${ manifestUrl } ` );
2024-03-26 14:56:00 +01:00
}
return response . result ;
}
2020-04-29 20:57:02 +03:00
async function installPython ( workingDirectory : string ) {
const options : ExecOptions = {
cwd : workingDirectory ,
2020-10-05 23:51:21 +11:00
env : {
... process . env ,
...( IS_LINUX && { LD_LIBRARY_PATH : path.join ( workingDirectory , 'lib' )})
},
2020-04-29 20:57:02 +03:00
silent : true ,
listeners : {
stdout : ( data : Buffer ) => {
2020-07-15 19:13:43 +02:00
core . info ( data . toString (). trim ());
},
stderr : ( data : Buffer ) => {
2026-07-13 23:58:55 +05:30
const msg = data . toString (). trim ();
if ( /^WARNING:/im . test ( msg )) {
core . warning ( msg );
} else {
core . error ( msg );
}
2020-04-29 20:57:02 +03:00
}
}
};
if ( IS_WINDOWS ) {
await exec . exec ( 'powershell' , [ './setup.ps1' ], options );
} else {
await exec . exec ( 'bash' , [ './setup.sh' ], options );
}
}
export async function installCpythonFromRelease ( release : tc.IToolRelease ) {
2025-04-17 19:25:44 +05:30
if ( ! release . files || release . files . length === 0 ) {
throw new Error ( 'No files found in the release to download.' );
}
2020-04-29 20:57:02 +03:00
const downloadUrl = release . files [ 0 ]. download_url ;
core . info ( `Download from " ${ downloadUrl } "` );
2022-10-24 11:10:18 +02:00
let pythonPath = '' ;
try {
2024-08-05 22:53:34 +05:30
const fileName = getDownloadFileName ( downloadUrl );
2026-09-09 00:31:48 +02:00
pythonPath = await tc . downloadTool (
downloadUrl ,
fileName ,
authForUrl ( downloadUrl )
);
2022-10-24 11:10:18 +02:00
core . info ( 'Extract downloaded archive' );
let pythonExtractedFolder ;
if ( IS_WINDOWS ) {
pythonExtractedFolder = await tc . extractZip ( pythonPath );
} else {
pythonExtractedFolder = await tc . extractTar ( pythonPath );
}
2020-04-29 20:57:02 +03:00
2022-10-24 11:10:18 +02:00
core . info ( 'Execute installation script' );
await installPython ( pythonExtractedFolder );
} catch ( err ) {
if ( err instanceof tc . HTTPError ) {
// Rate limit?
2025-04-17 19:25:44 +05:30
if ( err . httpStatusCode === 403 ) {
core . error (
`Received HTTP status code 403. This indicates a permission issue or restricted access.`
);
} else if ( err . httpStatusCode === 429 ) {
2022-10-24 11:10:18 +02:00
core . info (
2025-04-17 19:25:44 +05:30
`Received HTTP status code 429. This usually indicates the rate limit has been exceeded`
2022-10-24 11:10:18 +02:00
);
} else {
core . info ( err . message );
}
if ( err . stack ) {
core . debug ( err . stack );
}
}
throw err ;
}
2020-04-29 20:57:02 +03:00
}