mirror of
https://github.com/opentofu/setup-opentofu.git
synced 2026-11-29 11:34:36 +01:00
Update Node.js runtime to 24 (#91)
Signed-off-by: Dawid Jóźwiak <5508474+Operativ@users.noreply.github.com> Signed-off-by: James Humphries <james@james-humphries.co.uk> Signed-off-by: Diogenes Fernandes <diofeher@gmail.com> Co-authored-by: Dawid Jóźwiak <5508474+Operativ@users.noreply.github.com> Co-authored-by: Diogenes Fernandes <diofeher@gmail.com>
This commit is contained in:
19 files changed
+44738
-43700
No files matched your search
+87
-66
@@ -5,15 +5,26 @@
|
||||
*/
|
||||
|
||||
// Node.js core
|
||||
const fs = require('fs').promises;
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
import { promises as fs } from 'fs';
|
||||
import { platform, arch } from 'os';
|
||||
import { sep, resolve, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
// External
|
||||
const core = require('@actions/core');
|
||||
const tc = require('@actions/tool-cache');
|
||||
const io = require('@actions/io');
|
||||
const releases = require('./releases');
|
||||
import {
|
||||
debug,
|
||||
error as logError,
|
||||
exportVariable,
|
||||
getInput,
|
||||
warning,
|
||||
addPath
|
||||
} from '@actions/core';
|
||||
import { downloadTool, extractZip, find, cacheDir } from '@actions/tool-cache';
|
||||
import { mv, cp, mkdirP } from '@actions/io';
|
||||
import { getRelease } from './releases.js';
|
||||
|
||||
// __dirname is not available in ES modules, so we need to construct it ourselves
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
|
||||
// return value in [amd64, 386, arm]
|
||||
@@ -35,8 +46,8 @@ function mapOS (os) {
|
||||
}
|
||||
|
||||
async function downloadAndExtractCLI (url) {
|
||||
core.debug(`Downloading OpenTofu CLI from ${url}`);
|
||||
const pathToCLIZip = await tc.downloadTool(url);
|
||||
debug(`Downloading OpenTofu CLI from ${url}`);
|
||||
const pathToCLIZip = await downloadTool(url);
|
||||
|
||||
if (!pathToCLIZip) {
|
||||
throw new Error(`Unable to download OpenTofu from ${url}`);
|
||||
@@ -44,18 +55,18 @@ async function downloadAndExtractCLI (url) {
|
||||
|
||||
let pathToCLI;
|
||||
|
||||
core.debug('Extracting OpenTofu CLI zip file');
|
||||
if (os.platform().startsWith('win')) {
|
||||
core.debug(`OpenTofu CLI Download Path is ${pathToCLIZip}`);
|
||||
debug('Extracting OpenTofu CLI zip file');
|
||||
if (platform().startsWith('win')) {
|
||||
debug(`OpenTofu CLI Download Path is ${pathToCLIZip}`);
|
||||
const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
|
||||
await io.mv(pathToCLIZip, fixedPathToCLIZip);
|
||||
core.debug(`Moved download to ${fixedPathToCLIZip}`);
|
||||
pathToCLI = await tc.extractZip(fixedPathToCLIZip);
|
||||
await mv(pathToCLIZip, fixedPathToCLIZip);
|
||||
debug(`Moved download to ${fixedPathToCLIZip}`);
|
||||
pathToCLI = await extractZip(fixedPathToCLIZip);
|
||||
} else {
|
||||
pathToCLI = await tc.extractZip(pathToCLIZip);
|
||||
pathToCLI = await extractZip(pathToCLIZip);
|
||||
}
|
||||
|
||||
core.debug(`OpenTofu CLI path is ${pathToCLI}.`);
|
||||
debug(`OpenTofu CLI path is ${pathToCLI}.`);
|
||||
|
||||
if (!pathToCLI) {
|
||||
throw new Error('Unable to unzip OpenTofu');
|
||||
@@ -68,32 +79,34 @@ async function installWrapper (pathToCLI) {
|
||||
let source, target;
|
||||
|
||||
// If we're on Windows, then the executable ends with .exe
|
||||
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
|
||||
const exeSuffix = platform().startsWith('win') ? '.exe' : '';
|
||||
|
||||
// Rename tofu(.exe) to tofu-bin(.exe)
|
||||
try {
|
||||
source = [pathToCLI, `tofu${exeSuffix}`].join(path.sep);
|
||||
target = [pathToCLI, `tofu-bin${exeSuffix}`].join(path.sep);
|
||||
core.debug(`Moving ${source} to ${target}.`);
|
||||
await io.mv(source, target);
|
||||
source = [pathToCLI, `tofu${exeSuffix}`].join(sep);
|
||||
target = [pathToCLI, `tofu-bin${exeSuffix}`].join(sep);
|
||||
debug(`Moving ${source} to ${target}.`);
|
||||
await mv(source, target);
|
||||
} catch (e) {
|
||||
core.error(`Unable to move ${source} to ${target}.`);
|
||||
logError(`Unable to move ${source} to ${target}.`);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Install our wrapper as tofu
|
||||
try {
|
||||
source = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep));
|
||||
target = [pathToCLI, 'tofu'].join(path.sep);
|
||||
core.debug(`Copying ${source} to ${target}.`);
|
||||
await io.cp(source, target);
|
||||
source = resolve(
|
||||
[__dirname, '..', 'wrapper', 'dist', 'index.js'].join(sep)
|
||||
);
|
||||
target = [pathToCLI, 'tofu'].join(sep);
|
||||
debug(`Copying ${source} to ${target}.`);
|
||||
await cp(source, target);
|
||||
} catch (e) {
|
||||
core.error(`Unable to copy ${source} to ${target}.`);
|
||||
logError(`Unable to copy ${source} to ${target}.`);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Export a new environment variable, so our wrapper can locate the binary
|
||||
core.exportVariable('TOFU_CLI_PATH', pathToCLI);
|
||||
exportVariable('TOFU_CLI_PATH', pathToCLI);
|
||||
}
|
||||
|
||||
// Add credentials to CLI Configuration File
|
||||
@@ -105,37 +118,43 @@ async function addCredentials (credentialsHostname, credentialsToken, osPlat) {
|
||||
credentials "${credentialsHostname}" {
|
||||
token = "${credentialsToken}"
|
||||
}`.trim();
|
||||
// eslint-enable
|
||||
// eslint-enable
|
||||
|
||||
// default to OS-specific path
|
||||
let credsFile = osPlat === 'win32'
|
||||
? `${process.env.APPDATA}/tofu.rc`
|
||||
: `${process.env.HOME}/.tofurc`;
|
||||
let credsFile =
|
||||
osPlat === 'win32'
|
||||
? `${process.env.APPDATA}/tofu.rc`
|
||||
: `${process.env.HOME}/.tofurc`;
|
||||
|
||||
// override with TF_CLI_CONFIG_FILE environment variable
|
||||
credsFile = process.env.TF_CLI_CONFIG_FILE ? process.env.TF_CLI_CONFIG_FILE : credsFile;
|
||||
credsFile = process.env.TF_CLI_CONFIG_FILE
|
||||
? process.env.TF_CLI_CONFIG_FILE
|
||||
: credsFile;
|
||||
|
||||
// get containing folder
|
||||
const credsFolder = path.dirname(credsFile);
|
||||
const credsFolder = dirname(credsFile);
|
||||
|
||||
core.debug(`Creating ${credsFolder}`);
|
||||
await io.mkdirP(credsFolder);
|
||||
debug(`Creating ${credsFolder}`);
|
||||
await mkdirP(credsFolder);
|
||||
|
||||
core.debug(`Adding credentials to ${credsFile}`);
|
||||
debug(`Adding credentials to ${credsFile}`);
|
||||
await fs.writeFile(credsFile, creds);
|
||||
}
|
||||
|
||||
async function run () {
|
||||
try {
|
||||
// Gather GitHub Actions inputs
|
||||
let version = core.getInput('tofu_version');
|
||||
const versionFile = core.getInput('tofu_version_file');
|
||||
const credentialsHostname = core.getInput('cli_config_credentials_hostname');
|
||||
const credentialsToken = core.getInput('cli_config_credentials_token');
|
||||
const wrapper = core.getInput('tofu_wrapper') === 'true';
|
||||
const useCache = core.getInput('cache') === 'true';
|
||||
let githubToken = core.getInput('github_token');
|
||||
if (githubToken === '' && !(process.env.FORGEJO_ACTIONS || process.env.GITEA_ACTIONS)) {
|
||||
let version = getInput('tofu_version');
|
||||
const versionFile = getInput('tofu_version_file');
|
||||
const credentialsHostname = getInput('cli_config_credentials_hostname');
|
||||
const credentialsToken = getInput('cli_config_credentials_token');
|
||||
const wrapper = getInput('tofu_wrapper') === 'true';
|
||||
const useCache = getInput('cache') === 'true';
|
||||
let githubToken = getInput('github_token');
|
||||
if (
|
||||
githubToken === '' &&
|
||||
!(process.env.FORGEJO_ACTIONS || process.env.GITEA_ACTIONS)
|
||||
) {
|
||||
// Only default to the environment variable when running in GitHub Actions. Don't do this for other CI systems
|
||||
// that may set the GITHUB_TOKEN environment variable.
|
||||
githubToken = process.env.GITHUB_TOKEN;
|
||||
@@ -144,49 +163,51 @@ async function run () {
|
||||
// If tofu_version_file is provided, read the version from the file
|
||||
if (versionFile) {
|
||||
try {
|
||||
core.debug(`Reading OpenTofu version from file: ${versionFile}`);
|
||||
debug(`Reading OpenTofu version from file: ${versionFile}`);
|
||||
const fileVersion = await fs.readFile(versionFile, 'utf8');
|
||||
const trimmedVersion = fileVersion.trim();
|
||||
if (trimmedVersion) {
|
||||
version = trimmedVersion;
|
||||
core.debug(`Using version from file: ${version}`);
|
||||
debug(`Using version from file: ${version}`);
|
||||
} else {
|
||||
core.warning(
|
||||
warning(
|
||||
`Version file ${versionFile} is empty, using tofu_version input: ${version}`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
core.warning(
|
||||
warning(
|
||||
`Failed to read version from file ${versionFile}: ${error.message}. Using tofu_version input: ${version}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Gather OS details
|
||||
const osPlatform = os.platform();
|
||||
const osArch = os.arch();
|
||||
const osPlatform = platform();
|
||||
const osArch = arch();
|
||||
|
||||
core.debug(`Finding releases for OpenTofu version ${version}`);
|
||||
const release = await releases.getRelease(version, githubToken);
|
||||
const platform = mapOS(osPlatform);
|
||||
const arch = mapArch(osArch);
|
||||
const build = release.getBuild(platform, arch);
|
||||
debug(`Finding releases for OpenTofu version ${version}`);
|
||||
const release = await getRelease(version, githubToken);
|
||||
const buildPlatform = mapOS(osPlatform);
|
||||
const buildArch = mapArch(osArch);
|
||||
const build = release.getBuild(buildPlatform, buildArch);
|
||||
if (!build) {
|
||||
throw new Error(`OpenTofu version ${version} not available for ${platform} and ${arch}`);
|
||||
throw new Error(
|
||||
`OpenTofu version ${version} not available for ${buildPlatform} and ${buildArch}`
|
||||
);
|
||||
}
|
||||
|
||||
// Download requested version if not cached
|
||||
let pathToCLI;
|
||||
if (useCache) {
|
||||
const cachedPath = tc.find('tofu', release.version, arch);
|
||||
const cachedPath = find('tofu', release.version, buildArch);
|
||||
if (cachedPath) {
|
||||
core.debug(`Using cached OpenTofu version ${release.version} from ${cachedPath}`);
|
||||
debug(`Using cached OpenTofu version ${release.version} from ${cachedPath}`);
|
||||
pathToCLI = cachedPath;
|
||||
} else {
|
||||
core.debug(`OpenTofu version ${release.version} not found in cache, downloading...`);
|
||||
debug(`OpenTofu version ${release.version} not found in cache, downloading...`);
|
||||
const extractedPath = await downloadAndExtractCLI(build.url);
|
||||
core.debug(`Caching OpenTofu version ${release.version} to tool cache`);
|
||||
pathToCLI = await tc.cacheDir(extractedPath, 'tofu', release.version, arch);
|
||||
debug(`Caching OpenTofu version ${release.version} to tool cache`);
|
||||
pathToCLI = await cacheDir(extractedPath, 'tofu', release.version, buildArch);
|
||||
}
|
||||
} else {
|
||||
pathToCLI = await downloadAndExtractCLI(build.url);
|
||||
@@ -198,7 +219,7 @@ async function run () {
|
||||
}
|
||||
|
||||
// Add to path
|
||||
core.addPath(pathToCLI);
|
||||
addPath(pathToCLI);
|
||||
|
||||
// Add credentials to file if they are provided
|
||||
if (credentialsHostname && credentialsToken) {
|
||||
@@ -206,9 +227,9 @@ async function run () {
|
||||
}
|
||||
return release;
|
||||
} catch (error) {
|
||||
core.error(error);
|
||||
logError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = run;
|
||||
export default run;
|
||||
Reference in new issue
Block a user