Switch to @hashicorp/js-releases for finding correct package to install (#70)

This commit is contained in:
Audrey Eschright 2020-12-01 10:49:15 -08:00 committed by GitHub
parent 4c5048fbaf
commit 32c4f59108
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48794 additions and 4682 deletions

View file

@ -21,6 +21,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - ${{ matrix['terraform-versions'] }} - name: Setup Terraform - ${{ matrix['terraform-versions'] }}
uses: ./ uses: ./
@ -45,6 +47,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform (no wrapper) - ${{ matrix['terraform-versions'] }} - name: Setup Terraform (no wrapper) - ${{ matrix['terraform-versions'] }}
uses: ./ uses: ./
@ -70,6 +74,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - ${{ matrix['terraform-versions'] }} - name: Setup Terraform - ${{ matrix['terraform-versions'] }}
uses: ./ uses: ./
@ -89,6 +95,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform (no wrapper) - ${{ matrix['terraform-versions'] }} - name: Setup Terraform (no wrapper) - ${{ matrix['terraform-versions'] }}
uses: ./ uses: ./
@ -110,6 +118,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - name: Setup Terraform
uses: ./ uses: ./
@ -139,6 +149,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - name: Setup Terraform
uses: ./ uses: ./
@ -167,6 +179,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - name: Setup Terraform
uses: ./ uses: ./
@ -190,6 +204,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - name: Setup Terraform
uses: ./ uses: ./
@ -221,6 +237,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - name: Setup Terraform
uses: ./ uses: ./
@ -257,6 +275,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - name: Setup Terraform
uses: ./ uses: ./
@ -290,6 +310,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Setup Terraform - name: Setup Terraform
uses: ./ uses: ./

47710
dist/index.js vendored

File diff suppressed because one or more lines are too long

View file

@ -7,60 +7,7 @@ const path = require('path');
const core = require('@actions/core'); const core = require('@actions/core');
const tc = require('@actions/tool-cache'); const tc = require('@actions/tool-cache');
const io = require('@actions/io'); const io = require('@actions/io');
const fetch = require('node-fetch'); const releases = require('@hashicorp/js-releases');
const semver = require('semver');
// Find latest version given list of all available
function findLatest (allVersions) {
core.debug('Parsing version list for latest version');
let latest = '0.0.0';
for (const version in allVersions.versions) {
// Ignore pre-release
if (semver.prerelease(version) !== null) {
continue;
}
// is "version" greater than "latest"
latest = semver.gt(version, latest) ? version : latest;
}
core.info(`Latest version is ${latest}`);
return allVersions.versions[latest];
}
// Find specific version given list of all available
function findSpecific (allVersions, version) {
core.debug(`Parsing version list for version ${version}`);
return allVersions.versions[version];
}
// Find specific version given list of all available
function findLatestMatchingSpecification (allVersions, version) {
core.debug(`Parsing version list for latest matching specification ${version}`);
const versionList = [];
for (const _version in allVersions.versions) {
versionList.push(_version);
}
const bestMatchVersion = semver.maxSatisfying(versionList, version);
if (!bestMatchVersion) {
throw new Error(`Could not find Terraform version matching ${version} in version list`);
}
core.info(`Latest version satisfying ${version} is ${bestMatchVersion}`);
return allVersions.versions[bestMatchVersion];
}
async function downloadMetadata () {
core.debug('Downloading version metadata');
return fetch('https://releases.hashicorp.com/terraform/index.json')
.then(res => res.json())
.catch(err => {
core.setFailed(`Failed to fetch version metadata. ${err}`);
});
}
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch) // arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
// return value in [amd64, 386, arm] // return value in [amd64, 386, arm]
@ -81,23 +28,6 @@ function mapOS (os) {
return mappings[os] || os; return mappings[os] || os;
} }
// Get build for an operating system and architecture
function getBuild (versionObj, os, arch) {
core.debug(`Getting build for Terraform version ${versionObj.version}, os ${os}, and arch ${arch}`);
const buildObj = versionObj.builds.length &&
versionObj.builds.find(build =>
build.arch === mapArch(arch) &&
build.os === mapOS(os)
);
if (!buildObj) {
throw new Error(`Terraform version ${versionObj.version} not available for ${os} and ${arch}`);
}
return buildObj;
}
async function downloadCLI (url) { async function downloadCLI (url) {
core.debug(`Downloading Terraform CLI from ${url}`); core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url); const pathToCLIZip = await tc.downloadTool(url);
@ -183,40 +113,35 @@ async function run () {
const wrapper = core.getInput('terraform_wrapper') === 'true'; const wrapper = core.getInput('terraform_wrapper') === 'true';
// Gather OS details // Gather OS details
const osPlat = os.platform(); const osPlatform = os.platform();
const osArch = os.arch(); const osArch = os.arch();
// Download metadata about all versions of Terraform CLI core.debug(`Finding releases for Terraform version ${version}`);
const versionMetadata = await downloadMetadata(); const release = await releases.getRelease('terraform', version);
const platform = mapOS(osPlatform);
const specificMatch = findSpecific(versionMetadata, version); const arch = mapArch(osArch);
// Find latest or a specific version like 0.1.0 core.debug(`Getting build for Terraform version ${release.version}: ${platform} ${arch}`);
const versionObj = version.toLowerCase() === 'latest' const build = release.getBuild(platform, arch);
? findLatest(versionMetadata) : specificMatch || findLatestMatchingSpecification(versionMetadata, version); if (!build) {
throw new Error(`Terraform version ${version} not available for ${platform} and ${arch}`);
if (versionObj) {
// Get the build available for this runner's OS and a 64 bit architecture
const buildObj = getBuild(versionObj, osPlat, osArch);
// Download requested version
const pathToCLI = await downloadCLI(buildObj.url);
// Install our wrapper
if (wrapper) {
await installWrapper(pathToCLI);
}
// Add to path
core.addPath(pathToCLI);
// Add credentials to file if they are provided
if (credentialsHostname && credentialsToken) {
await addCredentials(credentialsHostname, credentialsToken, osPlat);
}
return versionObj;
} else {
core.setFailed(`Could not find Terraform version ${version} in version list`);
} }
// Download requested version
const pathToCLI = await downloadCLI(build.url);
// Install our wrapper
if (wrapper) {
await installWrapper(pathToCLI);
}
// Add to path
core.addPath(pathToCLI);
// Add credentials to file if they are provided
if (credentialsHostname && credentialsToken) {
await addCredentials(credentialsHostname, credentialsToken, osPlatform);
}
return release;
} catch (error) { } catch (error) {
core.error(error); core.error(error);
throw error; throw error;

5602
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -15,18 +15,17 @@
"author": "", "author": "",
"dependencies": { "dependencies": {
"@actions/core": "^1.2.6", "@actions/core": "^1.2.6",
"@actions/github": "^2.1.1", "@actions/github": "^4.0.0",
"@actions/io": "^1.0.2", "@actions/io": "^1.0.2",
"@actions/tool-cache": "^1.6.0", "@actions/tool-cache": "^1.6.1",
"node-fetch": "^2.6.1", "@hashicorp/js-releases": "^1.1.0"
"semver": "^7.3.2"
}, },
"devDependencies": { "devDependencies": {
"@zeit/ncc": "0.22.1", "@zeit/ncc": "0.22.1",
"husky": "^4.3.0", "husky": "^4.3.0",
"jest": "^25.2.4", "jest": "^26.6.3",
"nock": "^12.0.3", "nock": "^13.0.5",
"semistandard": "^14.2.0" "semistandard": "^16.0.0"
}, },
"jest": { "jest": {
"testPathIgnorePatterns": [ "testPathIgnorePatterns": [