Skip to content

Commit 8c7b377

Browse files
committed
Uses tool cache - skips wrapper create on hit.
1 parent 3d8debd commit 8c7b377

File tree

2 files changed

+72
-16
lines changed

2 files changed

+72
-16
lines changed

dist/index.js

+36-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const tc = __webpack_require__(7784);
3434
const io = __webpack_require__(7436);
3535
const releases = __webpack_require__(9947);
3636

37+
// Constants
38+
const CACHE_KEY = 'terraform';
39+
3740
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
3841
// return value in [amd64, 386, arm]
3942
function mapArch (arch) {
@@ -53,7 +56,8 @@ function mapOS (os) {
5356
return mappings[os] || os;
5457
}
5558

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

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

68-
return pathToCLI;
72+
// Cache for later
73+
const cachedPath = await tc.cacheDir(pathToCLI, CACHE_KEY, version);
74+
return cachedPath;
75+
}
76+
77+
async function checkWrapper (pathToCLI) {
78+
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
79+
const target = [pathToCLI, `terraform-bin${exeSuffix}`].join(path.sep);
80+
81+
core.debug('Checking for existing wrapper');
82+
83+
const hasWrapper = io.which(target);
84+
85+
if (hasWrapper) {
86+
core.debug('Wrapper found, skipping creation.');
87+
}
88+
89+
return hasWrapper;
6990
}
7091

7192
async function installWrapper (pathToCLI) {
@@ -95,9 +116,6 @@ async function installWrapper (pathToCLI) {
95116
core.error(`Unable to copy ${source} to ${target}.`);
96117
throw e;
97118
}
98-
99-
// Export a new environment variable, so our wrapper can locate the binary
100-
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);
101119
}
102120

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

154-
// Download requested version
155-
const pathToCLI = await downloadCLI(build.url);
172+
// Check cache for requested version, then download if not present
173+
let pathToCLI = tc.find(CACHE_KEY, release.version, os.arch());
174+
175+
// Check to see if wrapper has been installed in a previous run
176+
const hasWrapper = pathToCLI && checkWrapper(pathToCLI);
177+
178+
if (!pathToCLI) {
179+
pathToCLI = await downloadCLI(build.url, release.version);
180+
}
156181

157182
// Install our wrapper
158-
if (wrapper) {
183+
if (wrapper && !hasWrapper) {
159184
await installWrapper(pathToCLI);
160185
}
161186

187+
// Export a new environment variable, so our wrapper can locate the binary
188+
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);
189+
162190
// Add to path
163191
core.addPath(pathToCLI);
164192

lib/setup-terraform.js

+36-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const tc = require('@actions/tool-cache');
99
const io = require('@actions/io');
1010
const releases = require('@hashicorp/js-releases');
1111

12+
// Constants
13+
const CACHE_KEY = 'terraform';
14+
1215
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
1316
// return value in [amd64, 386, arm]
1417
function mapArch (arch) {
@@ -28,7 +31,8 @@ function mapOS (os) {
2831
return mappings[os] || os;
2932
}
3033

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

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

43-
return pathToCLI;
47+
// Cache for later
48+
const cachedPath = await tc.cacheDir(pathToCLI, CACHE_KEY, version);
49+
return cachedPath;
50+
}
51+
52+
async function checkWrapper (pathToCLI) {
53+
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
54+
const target = [pathToCLI, `terraform-bin${exeSuffix}`].join(path.sep);
55+
56+
core.debug('Checking for existing wrapper');
57+
58+
const hasWrapper = io.which(target);
59+
60+
if (hasWrapper) {
61+
core.debug('Wrapper found, skipping creation.');
62+
}
63+
64+
return hasWrapper;
4465
}
4566

4667
async function installWrapper (pathToCLI) {
@@ -70,9 +91,6 @@ async function installWrapper (pathToCLI) {
7091
core.error(`Unable to copy ${source} to ${target}.`);
7192
throw e;
7293
}
73-
74-
// Export a new environment variable, so our wrapper can locate the binary
75-
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);
7694
}
7795

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

129-
// Download requested version
130-
const pathToCLI = await downloadCLI(build.url);
147+
// Check cache for requested version, then download if not present
148+
let pathToCLI = tc.find(CACHE_KEY, release.version, os.arch());
149+
150+
// Check to see if wrapper has been installed in a previous run
151+
const hasWrapper = pathToCLI && checkWrapper(pathToCLI);
152+
153+
if (!pathToCLI) {
154+
pathToCLI = await downloadCLI(build.url, release.version);
155+
}
131156

132157
// Install our wrapper
133-
if (wrapper) {
158+
if (wrapper && !hasWrapper) {
134159
await installWrapper(pathToCLI);
135160
}
136161

162+
// Export a new environment variable, so our wrapper can locate the binary
163+
core.exportVariable('TERRAFORM_CLI_PATH', pathToCLI);
164+
137165
// Add to path
138166
core.addPath(pathToCLI);
139167

0 commit comments

Comments
 (0)