@@ -9,6 +9,9 @@ const tc = require('@actions/tool-cache');
99const io = require ( '@actions/io' ) ;
1010const 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]
1417function 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
4667async 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