2020-04-30 01:57:02 +08: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';
|
2024-03-26 21:56:00 +08:00
|
|
|
import * as httpm from '@actions/http-client';
|
2020-04-30 01:57:02 +08:00
|
|
|
import {ExecOptions} from '@actions/exec/lib/interfaces';
|
2024-08-06 01:23:34 +08:00
|
|
|
import {IS_WINDOWS, IS_LINUX, getDownloadFileName} from './utils';
|
2020-04-30 01:57:02 +08:00
|
|
|
|
2020-05-20 21:35:09 +08:00
|
|
|
const TOKEN = core.getInput('token');
|
2022-08-31 03:02:20 +08:00
|
|
|
const AUTH = !TOKEN ? undefined : `token ${TOKEN}`;
|
2020-04-30 01:57:02 +08:00
|
|
|
const MANIFEST_REPO_OWNER = 'actions';
|
|
|
|
const MANIFEST_REPO_NAME = 'python-versions';
|
2020-07-15 17:26:28 +08:00
|
|
|
const MANIFEST_REPO_BRANCH = 'main';
|
|
|
|
export const MANIFEST_URL = `https://raw.githubusercontent.com/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/${MANIFEST_REPO_BRANCH}/versions-manifest.json`;
|
2020-04-30 01:57:02 +08:00
|
|
|
|
|
|
|
export async function findReleaseFromManifest(
|
|
|
|
semanticVersionSpec: string,
|
2022-07-25 22:54:04 +08:00
|
|
|
architecture: string,
|
|
|
|
manifest: tc.IToolRelease[] | null
|
2020-04-30 01:57:02 +08:00
|
|
|
): Promise<tc.IToolRelease | undefined> {
|
2022-07-25 22:54:04 +08:00
|
|
|
if (!manifest) {
|
|
|
|
manifest = await getManifest();
|
2024-09-23 18:50:29 +08:00
|
|
|
core.info('manifest :>> ' + JSON.stringify(manifest));
|
2022-07-25 22:54:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const foundRelease = await tc.findFromManifest(
|
2020-04-30 01:57:02 +08:00
|
|
|
semanticVersionSpec,
|
2020-07-17 17:58:03 +08:00
|
|
|
false,
|
2020-04-30 01:57:02 +08:00
|
|
|
manifest,
|
|
|
|
architecture
|
|
|
|
);
|
2024-09-23 18:50:29 +08:00
|
|
|
core.info(`Found release: ${JSON.stringify(foundRelease)}`);
|
2022-07-25 22:54:04 +08:00
|
|
|
return foundRelease;
|
|
|
|
}
|
|
|
|
|
2024-03-26 21:56:00 +08:00
|
|
|
export async function getManifest(): Promise<tc.IToolRelease[]> {
|
|
|
|
try {
|
2024-09-23 18:39:10 +08:00
|
|
|
const manifestFromRepo = await getManifestFromRepo();
|
2024-09-23 18:50:29 +08:00
|
|
|
core.info('Successfully fetched the manifest from the repo.');
|
|
|
|
core.info(`Manifest from repo: ${JSON.stringify(manifestFromRepo)}`);
|
2024-09-23 18:39:10 +08:00
|
|
|
return manifestFromRepo;
|
2024-03-26 21:56:00 +08:00
|
|
|
} catch (err) {
|
2024-09-23 18:50:29 +08:00
|
|
|
core.info('Fetching the manifest via the API failed.');
|
2024-03-26 21:56:00 +08:00
|
|
|
if (err instanceof Error) {
|
2024-09-23 18:50:29 +08:00
|
|
|
core.info(err.message);
|
2024-03-26 21:56:00 +08:00
|
|
|
}
|
|
|
|
}
|
2024-09-23 18:39:10 +08:00
|
|
|
const manifestFromURL = await getManifestFromURL();
|
2024-09-23 18:50:29 +08:00
|
|
|
core.info('Successfully fetched the manifest from the URL.');
|
|
|
|
core.info(`Manifest from URL: ${JSON.stringify(manifestFromURL)}`);
|
2024-09-23 18:39:10 +08:00
|
|
|
return manifestFromURL;
|
2024-03-26 21:56:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getManifestFromRepo(): Promise<tc.IToolRelease[]> {
|
2024-09-23 18:50:29 +08:00
|
|
|
core.info(
|
2022-07-25 22:54:04 +08:00
|
|
|
`Getting manifest from ${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`
|
|
|
|
);
|
|
|
|
return tc.getManifestFromRepo(
|
|
|
|
MANIFEST_REPO_OWNER,
|
|
|
|
MANIFEST_REPO_NAME,
|
|
|
|
AUTH,
|
|
|
|
MANIFEST_REPO_BRANCH
|
|
|
|
);
|
2020-04-30 01:57:02 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 21:56:00 +08:00
|
|
|
export async function getManifestFromURL(): Promise<tc.IToolRelease[]> {
|
|
|
|
core.debug('Falling back to fetching the manifest using raw URL.');
|
|
|
|
|
|
|
|
const http: httpm.HttpClient = new httpm.HttpClient('tool-cache');
|
|
|
|
const response = await http.getJson<tc.IToolRelease[]>(MANIFEST_URL);
|
|
|
|
if (!response.result) {
|
|
|
|
throw new Error(`Unable to get manifest from ${MANIFEST_URL}`);
|
|
|
|
}
|
|
|
|
return response.result;
|
|
|
|
}
|
|
|
|
|
2020-04-30 01:57:02 +08:00
|
|
|
async function installPython(workingDirectory: string) {
|
|
|
|
const options: ExecOptions = {
|
|
|
|
cwd: workingDirectory,
|
2020-10-05 20:51:21 +08:00
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
...(IS_LINUX && {LD_LIBRARY_PATH: path.join(workingDirectory, 'lib')})
|
|
|
|
},
|
2020-04-30 01:57:02 +08:00
|
|
|
silent: true,
|
|
|
|
listeners: {
|
|
|
|
stdout: (data: Buffer) => {
|
2020-07-16 01:13:43 +08:00
|
|
|
core.info(data.toString().trim());
|
|
|
|
},
|
|
|
|
stderr: (data: Buffer) => {
|
|
|
|
core.error(data.toString().trim());
|
2020-04-30 01:57:02 +08: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) {
|
|
|
|
const downloadUrl = release.files[0].download_url;
|
|
|
|
|
|
|
|
core.info(`Download from "${downloadUrl}"`);
|
2022-10-24 17:10:18 +08:00
|
|
|
let pythonPath = '';
|
|
|
|
try {
|
2024-08-06 01:23:34 +08:00
|
|
|
const fileName = getDownloadFileName(downloadUrl);
|
|
|
|
pythonPath = await tc.downloadTool(downloadUrl, fileName, AUTH);
|
2022-10-24 17:10:18 +08:00
|
|
|
core.info('Extract downloaded archive');
|
|
|
|
let pythonExtractedFolder;
|
|
|
|
if (IS_WINDOWS) {
|
|
|
|
pythonExtractedFolder = await tc.extractZip(pythonPath);
|
|
|
|
} else {
|
|
|
|
pythonExtractedFolder = await tc.extractTar(pythonPath);
|
|
|
|
}
|
2020-04-30 01:57:02 +08:00
|
|
|
|
2022-10-24 17:10:18 +08:00
|
|
|
core.info('Execute installation script');
|
|
|
|
await installPython(pythonExtractedFolder);
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof tc.HTTPError) {
|
|
|
|
// Rate limit?
|
|
|
|
if (err.httpStatusCode === 403 || err.httpStatusCode === 429) {
|
|
|
|
core.info(
|
|
|
|
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
core.info(err.message);
|
|
|
|
}
|
|
|
|
if (err.stack) {
|
|
|
|
core.debug(err.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2020-04-30 01:57:02 +08:00
|
|
|
}
|