Skip to content

Commit

Permalink
Uses tool cache - skips wrapper create on hit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcuffe committed Mar 6, 2021
1 parent 3d8debd commit 8c7b377
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
44 changes: 36 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const tc = __webpack_require__(7784);
const io = __webpack_require__(7436);
const releases = __webpack_require__(9947);

// Constants
const CACHE_KEY = 'terraform';

// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
// return value in [amd64, 386, arm]
function mapArch (arch) {
Expand All @@ -53,7 +56,8 @@ function mapOS (os) {
return mappings[os] || os;
}

async function downloadCLI (url) {
async function downloadCLI (url, version) {
// Look for CLI in the cache first
core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url);

Expand All @@ -65,7 +69,24 @@ async function downloadCLI (url) {
throw new Error(`Unable to download Terraform from ${url}`);
}

return pathToCLI;
// Cache for later
const cachedPath = await tc.cacheDir(pathToCLI, CACHE_KEY, version);
return cachedPath;
}

async function checkWrapper (pathToCLI) {
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
const target = [pathToCLI, `terraform-bin${exeSuffix}`].join(path.sep);

core.debug('Checking for existing wrapper');

const hasWrapper = io.which(target);

if (hasWrapper) {
core.debug('Wrapper found, skipping creation.');
}

return hasWrapper;
}

async function installWrapper (pathToCLI) {
Expand Down Expand Up @@ -95,9 +116,6 @@ async function installWrapper (pathToCLI) {
core.error(`Unable to copy ${source} to ${target}.`);
throw e;
}

// Export a new environment variable, so our wrapper can locate the binary
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);
}

// Add credentials to CLI Configuration File
Expand Down Expand Up @@ -151,14 +169,24 @@ async function run () {
throw new Error(`Terraform version ${version} not available for ${platform} and ${arch}`);
}

// Download requested version
const pathToCLI = await downloadCLI(build.url);
// Check cache for requested version, then download if not present
let pathToCLI = tc.find(CACHE_KEY, release.version, os.arch());

// Check to see if wrapper has been installed in a previous run
const hasWrapper = pathToCLI && checkWrapper(pathToCLI);

if (!pathToCLI) {
pathToCLI = await downloadCLI(build.url, release.version);
}

// Install our wrapper
if (wrapper) {
if (wrapper && !hasWrapper) {
await installWrapper(pathToCLI);
}

// Export a new environment variable, so our wrapper can locate the binary
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);

// Add to path
core.addPath(pathToCLI);

Expand Down
44 changes: 36 additions & 8 deletions lib/setup-terraform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const tc = require('@actions/tool-cache');
const io = require('@actions/io');
const releases = require('@hashicorp/js-releases');

// Constants
const CACHE_KEY = 'terraform';

// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
// return value in [amd64, 386, arm]
function mapArch (arch) {
Expand All @@ -28,7 +31,8 @@ function mapOS (os) {
return mappings[os] || os;
}

async function downloadCLI (url) {
async function downloadCLI (url, version) {
// Look for CLI in the cache first
core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url);

Expand All @@ -40,7 +44,24 @@ async function downloadCLI (url) {
throw new Error(`Unable to download Terraform from ${url}`);
}

return pathToCLI;
// Cache for later
const cachedPath = await tc.cacheDir(pathToCLI, CACHE_KEY, version);
return cachedPath;
}

async function checkWrapper (pathToCLI) {
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
const target = [pathToCLI, `terraform-bin${exeSuffix}`].join(path.sep);

core.debug('Checking for existing wrapper');

const hasWrapper = io.which(target);

if (hasWrapper) {
core.debug('Wrapper found, skipping creation.');
}

return hasWrapper;
}

async function installWrapper (pathToCLI) {
Expand Down Expand Up @@ -70,9 +91,6 @@ async function installWrapper (pathToCLI) {
core.error(`Unable to copy ${source} to ${target}.`);
throw e;
}

// Export a new environment variable, so our wrapper can locate the binary
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);
}

// Add credentials to CLI Configuration File
Expand Down Expand Up @@ -126,14 +144,24 @@ async function run () {
throw new Error(`Terraform version ${version} not available for ${platform} and ${arch}`);
}

// Download requested version
const pathToCLI = await downloadCLI(build.url);
// Check cache for requested version, then download if not present
let pathToCLI = tc.find(CACHE_KEY, release.version, os.arch());

// Check to see if wrapper has been installed in a previous run
const hasWrapper = pathToCLI && checkWrapper(pathToCLI);

if (!pathToCLI) {
pathToCLI = await downloadCLI(build.url, release.version);
}

// Install our wrapper
if (wrapper) {
if (wrapper && !hasWrapper) {
await installWrapper(pathToCLI);
}

// Export a new environment variable, so our wrapper can locate the binary
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);

// Add to path
core.addPath(pathToCLI);

Expand Down

0 comments on commit 8c7b377

Please sign in to comment.