Expose the ability to select different architectures

This commit is contained in:
Tyler Ang-Wanek 2019-08-22 13:21:46 -07:00
parent e565252a9d
commit 3d0361d465
No known key found for this signature in database
GPG key ID: DED1387F8E0A5146
7 changed files with 84 additions and 35 deletions

View file

@ -100,6 +100,18 @@ steps:
- run: npm rebuild && npm run prepare --if-present - run: npm rebuild && npm run prepare --if-present
``` ```
Specifying a different architecture than the system architecture:
```yaml
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: '10.x'
node-arch: 'x86'
- run: npm install
- run: npm test
```
# License # License

View file

@ -120,4 +120,30 @@ describe('installer tests', () => {
await installer.getNode('252'); await installer.getNode('252');
await installer.getNode('252.0'); await installer.getNode('252.0');
}); });
it('Acquires specified x86 version of node if no matching version is installed', async () => {
const arch = 'x86';
await installer.getNode('8.8.1', arch);
const nodeDir = path.join(toolDir, 'node', '8.8.1', arch);
expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(nodeDir, 'bin', 'node'))).toBe(true);
}
}, 100000);
it('Acquires specified x64 version of node if no matching version is installed', async () => {
const arch = 'x64';
await installer.getNode('8.8.1', arch);
const nodeDir = path.join(toolDir, 'node', '8.8.1', arch);
expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(nodeDir, 'bin', 'node'))).toBe(true);
}
}, 100000);
}); });

View file

@ -8,6 +8,8 @@ inputs:
node-version: node-version:
description: 'Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0' description: 'Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0'
default: '10.x' default: '10.x'
node-arch:
description: 'Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.'
registry-url: registry-url:
description: 'Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN' description: 'Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN'
scope: scope:

View file

@ -25,7 +25,6 @@ const os = __importStar(require("os"));
const path = __importStar(require("path")); const path = __importStar(require("path"));
const semver = __importStar(require("semver")); const semver = __importStar(require("semver"));
let osPlat = os.platform(); let osPlat = os.platform();
let osArch = os.arch();
if (!tempDirectory) { if (!tempDirectory) {
let baseLocation; let baseLocation;
if (process.platform === 'win32') { if (process.platform === 'win32') {
@ -42,11 +41,11 @@ if (!tempDirectory) {
} }
tempDirectory = path.join(baseLocation, 'actions', 'temp'); tempDirectory = path.join(baseLocation, 'actions', 'temp');
} }
function getNode(versionSpec) { function getNode(versionSpec, osArch = os.arch()) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// check cache // check cache
let toolPath; let toolPath;
toolPath = tc.find('node', versionSpec); toolPath = tc.find('node', versionSpec, osArch);
// If not found in cache, download // If not found in cache, download
if (!toolPath) { if (!toolPath) {
let version; let version;
@ -58,16 +57,16 @@ function getNode(versionSpec) {
} }
else { else {
// query nodejs.org for a matching version // query nodejs.org for a matching version
version = yield queryLatestMatch(versionSpec); version = yield queryLatestMatch(versionSpec, osArch);
if (!version) { if (!version) {
throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`); throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
} }
// check cache // check cache
toolPath = tc.find('node', version); toolPath = tc.find('node', version, osArch);
} }
if (!toolPath) { if (!toolPath) {
// download, extract, cache // download, extract, cache
toolPath = yield acquireNode(version); toolPath = yield acquireNode(version, osArch);
} }
} }
// //
@ -84,7 +83,7 @@ function getNode(versionSpec) {
}); });
} }
exports.getNode = getNode; exports.getNode = getNode;
function queryLatestMatch(versionSpec) { function queryLatestMatch(versionSpec, osArch) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// node offers a json list of versions // node offers a json list of versions
let dataFileName; let dataFileName;
@ -142,15 +141,15 @@ function evaluateVersions(versions, versionSpec) {
} }
return version; return version;
} }
function acquireNode(version) { function acquireNode(version, osArch) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// //
// Download - a tool installer intimately knows how to get the tool (and construct urls) // Download - a tool installer intimately knows how to get the tool (and construct urls)
// //
version = semver.clean(version) || ''; version = semver.clean(version) || '';
let fileName = osPlat == 'win32' let fileName = osPlat == 'win32'
? 'node-v' + version + '-win-' + os.arch() ? 'node-v' + version + '-win-' + osArch
: 'node-v' + version + '-' + osPlat + '-' + os.arch(); : 'node-v' + version + '-' + osPlat + '-' + osArch;
let urlFileName = osPlat == 'win32' ? fileName + '.7z' : fileName + '.tar.gz'; let urlFileName = osPlat == 'win32' ? fileName + '.7z' : fileName + '.tar.gz';
let downloadUrl = 'https://nodejs.org/dist/v' + version + '/' + urlFileName; let downloadUrl = 'https://nodejs.org/dist/v' + version + '/' + urlFileName;
let downloadPath; let downloadPath;
@ -159,7 +158,7 @@ function acquireNode(version) {
} }
catch (err) { catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) { if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
return yield acquireNodeFromFallbackLocation(version); return yield acquireNodeFromFallbackLocation(version, osArch);
} }
throw err; throw err;
} }
@ -178,7 +177,7 @@ function acquireNode(version) {
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
// //
let toolRoot = path.join(extPath, fileName); let toolRoot = path.join(extPath, fileName);
return yield tc.cacheDir(toolRoot, 'node', version); return yield tc.cacheDir(toolRoot, 'node', version, osArch);
}); });
} }
// For non LTS versions of Node, the files we need (for Windows) are sometimes located // For non LTS versions of Node, the files we need (for Windows) are sometimes located
@ -193,7 +192,7 @@ function acquireNode(version) {
// This method attempts to download and cache the resources from these alternative locations. // This method attempts to download and cache the resources from these alternative locations.
// Note also that the files are normally zipped but in this case they are just an exe // Note also that the files are normally zipped but in this case they are just an exe
// and lib file in a folder, not zipped. // and lib file in a folder, not zipped.
function acquireNodeFromFallbackLocation(version) { function acquireNodeFromFallbackLocation(version, osArch) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// Create temporary folder to download in to // Create temporary folder to download in to
let tempDownloadFolder = 'temp_' + Math.floor(Math.random() * 2000000000); let tempDownloadFolder = 'temp_' + Math.floor(Math.random() * 2000000000);
@ -202,8 +201,8 @@ function acquireNodeFromFallbackLocation(version) {
let exeUrl; let exeUrl;
let libUrl; let libUrl;
try { try {
exeUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.exe`; exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`;
libUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.lib`; libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`;
const exePath = yield tc.downloadTool(exeUrl); const exePath = yield tc.downloadTool(exeUrl);
yield io.cp(exePath, path.join(tempDir, 'node.exe')); yield io.cp(exePath, path.join(tempDir, 'node.exe'));
const libPath = yield tc.downloadTool(libUrl); const libPath = yield tc.downloadTool(libUrl);
@ -222,6 +221,6 @@ function acquireNodeFromFallbackLocation(version) {
throw err; throw err;
} }
} }
return yield tc.cacheDir(tempDir, 'node', version); return yield tc.cacheDir(tempDir, 'node', version, osArch);
}); });
} }

View file

@ -18,6 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const installer = __importStar(require("./installer")); const installer = __importStar(require("./installer"));
const auth = __importStar(require("./authutil")); const auth = __importStar(require("./authutil"));
const os = __importStar(require("os"));
const path = __importStar(require("path")); const path = __importStar(require("path"));
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@ -30,9 +31,10 @@ function run() {
if (!version) { if (!version) {
version = core.getInput('node-version'); version = core.getInput('node-version');
} }
const osArch = core.getInput('node-arch') || os.arch();
if (version) { if (version) {
// TODO: installer doesn't support proxy // TODO: installer doesn't support proxy
yield installer.getNode(version); yield installer.getNode(version, osArch);
} }
const registryUrl = core.getInput('registry-url'); const registryUrl = core.getInput('registry-url');
const alwaysAuth = core.getInput('always-auth'); const alwaysAuth = core.getInput('always-auth');

View file

@ -9,7 +9,6 @@ import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
let osPlat: string = os.platform(); let osPlat: string = os.platform();
let osArch: string = os.arch();
if (!tempDirectory) { if (!tempDirectory) {
let baseLocation; let baseLocation;
@ -35,10 +34,13 @@ interface INodeVersion {
files: string[]; files: string[];
} }
export async function getNode(versionSpec: string) { export async function getNode(
versionSpec: string,
osArch: string | undefined = os.arch()
) {
// check cache // check cache
let toolPath: string; let toolPath: string;
toolPath = tc.find('node', versionSpec); toolPath = tc.find('node', versionSpec, osArch);
// If not found in cache, download // If not found in cache, download
if (!toolPath) { if (!toolPath) {
@ -50,7 +52,7 @@ export async function getNode(versionSpec: string) {
version = versionSpec; version = versionSpec;
} else { } else {
// query nodejs.org for a matching version // query nodejs.org for a matching version
version = await queryLatestMatch(versionSpec); version = await queryLatestMatch(versionSpec, osArch);
if (!version) { if (!version) {
throw new Error( throw new Error(
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.` `Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
@ -58,12 +60,12 @@ export async function getNode(versionSpec: string) {
} }
// check cache // check cache
toolPath = tc.find('node', version); toolPath = tc.find('node', version, osArch);
} }
if (!toolPath) { if (!toolPath) {
// download, extract, cache // download, extract, cache
toolPath = await acquireNode(version); toolPath = await acquireNode(version, osArch);
} }
} }
@ -81,7 +83,10 @@ export async function getNode(versionSpec: string) {
core.addPath(toolPath); core.addPath(toolPath);
} }
async function queryLatestMatch(versionSpec: string): Promise<string> { async function queryLatestMatch(
versionSpec: string,
osArch: string
): Promise<string> {
// node offers a json list of versions // node offers a json list of versions
let dataFileName: string; let dataFileName: string;
switch (osPlat) { switch (osPlat) {
@ -143,15 +148,15 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
return version; return version;
} }
async function acquireNode(version: string): Promise<string> { async function acquireNode(version: string, osArch: string): Promise<string> {
// //
// Download - a tool installer intimately knows how to get the tool (and construct urls) // Download - a tool installer intimately knows how to get the tool (and construct urls)
// //
version = semver.clean(version) || ''; version = semver.clean(version) || '';
let fileName: string = let fileName: string =
osPlat == 'win32' osPlat == 'win32'
? 'node-v' + version + '-win-' + os.arch() ? 'node-v' + version + '-win-' + osArch
: 'node-v' + version + '-' + osPlat + '-' + os.arch(); : 'node-v' + version + '-' + osPlat + '-' + osArch;
let urlFileName: string = let urlFileName: string =
osPlat == 'win32' ? fileName + '.7z' : fileName + '.tar.gz'; osPlat == 'win32' ? fileName + '.7z' : fileName + '.tar.gz';
@ -163,7 +168,7 @@ async function acquireNode(version: string): Promise<string> {
downloadPath = await tc.downloadTool(downloadUrl); downloadPath = await tc.downloadTool(downloadUrl);
} catch (err) { } catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) { if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
return await acquireNodeFromFallbackLocation(version); return await acquireNodeFromFallbackLocation(version, osArch);
} }
throw err; throw err;
@ -184,7 +189,7 @@ async function acquireNode(version: string): Promise<string> {
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
// //
let toolRoot = path.join(extPath, fileName); let toolRoot = path.join(extPath, fileName);
return await tc.cacheDir(toolRoot, 'node', version); return await tc.cacheDir(toolRoot, 'node', version, osArch);
} }
// For non LTS versions of Node, the files we need (for Windows) are sometimes located // For non LTS versions of Node, the files we need (for Windows) are sometimes located
@ -200,7 +205,8 @@ async function acquireNode(version: string): Promise<string> {
// Note also that the files are normally zipped but in this case they are just an exe // Note also that the files are normally zipped but in this case they are just an exe
// and lib file in a folder, not zipped. // and lib file in a folder, not zipped.
async function acquireNodeFromFallbackLocation( async function acquireNodeFromFallbackLocation(
version: string version: string,
osArch: string
): Promise<string> { ): Promise<string> {
// Create temporary folder to download in to // Create temporary folder to download in to
let tempDownloadFolder: string = let tempDownloadFolder: string =
@ -210,8 +216,8 @@ async function acquireNodeFromFallbackLocation(
let exeUrl: string; let exeUrl: string;
let libUrl: string; let libUrl: string;
try { try {
exeUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.exe`; exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`;
libUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.lib`; libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`;
const exePath = await tc.downloadTool(exeUrl); const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe')); await io.cp(exePath, path.join(tempDir, 'node.exe'));
@ -230,5 +236,5 @@ async function acquireNodeFromFallbackLocation(
throw err; throw err;
} }
} }
return await tc.cacheDir(tempDir, 'node', version); return await tc.cacheDir(tempDir, 'node', version, osArch);
} }

View file

@ -1,6 +1,7 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as installer from './installer'; import * as installer from './installer';
import * as auth from './authutil'; import * as auth from './authutil';
import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
async function run() { async function run() {
@ -13,9 +14,10 @@ async function run() {
if (!version) { if (!version) {
version = core.getInput('node-version'); version = core.getInput('node-version');
} }
const osArch = core.getInput('node-arch') || os.arch();
if (version) { if (version) {
// TODO: installer doesn't support proxy // TODO: installer doesn't support proxy
await installer.getNode(version); await installer.getNode(version, osArch);
} }
const registryUrl: string = core.getInput('registry-url'); const registryUrl: string = core.getInput('registry-url');