@@ -9,6 +9,9 @@ const tc = require('@actions/tool-cache');
9
9
const io = require ( '@actions/io' ) ;
10
10
const releases = require ( '@hashicorp/js-releases' ) ;
11
11
12
+ // Constants
13
+ const CACHE_KEY = 'terraform' ;
14
+
12
15
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
13
16
// return value in [amd64, 386, arm]
14
17
function mapArch ( arch ) {
@@ -28,7 +31,7 @@ function mapOS (os) {
28
31
return mappings [ os ] || os ;
29
32
}
30
33
31
- async function downloadCLI ( url ) {
34
+ async function downloadCLI ( url , version ) {
32
35
core . debug ( `Downloading Terraform CLI from ${ url } ` ) ;
33
36
const pathToCLIZip = await tc . downloadTool ( url ) ;
34
37
@@ -40,7 +43,24 @@ async function downloadCLI (url) {
40
43
throw new Error ( `Unable to download Terraform from ${ url } ` ) ;
41
44
}
42
45
43
- return pathToCLI ;
46
+ // Cache for later
47
+ const cachedPath = await tc . cacheDir ( pathToCLI , CACHE_KEY , version ) ;
48
+ return cachedPath ;
49
+ }
50
+
51
+ async function checkWrapper ( pathToCLI ) {
52
+ const exeSuffix = os . platform ( ) . startsWith ( 'win' ) ? '.exe' : '' ;
53
+ const target = [ pathToCLI , `terraform-bin${ exeSuffix } ` ] . join ( path . sep ) ;
54
+
55
+ core . debug ( 'Checking for existing wrapper' ) ;
56
+
57
+ const hasWrapper = io . which ( target ) ;
58
+
59
+ if ( hasWrapper ) {
60
+ core . debug ( 'Wrapper found, skipping creation.' ) ;
61
+ }
62
+
63
+ return hasWrapper ;
44
64
}
45
65
46
66
async function installWrapper ( pathToCLI ) {
@@ -70,9 +90,6 @@ async function installWrapper (pathToCLI) {
70
90
core . error ( `Unable to copy ${ source } to ${ target } .` ) ;
71
91
throw e ;
72
92
}
73
-
74
- // Export a new environment variable, so our wrapper can locate the binary
75
- core . exportVariable ( 'TERRAFORM_CLI_PATH' , pathToCLI ) ;
76
93
}
77
94
78
95
// Add credentials to CLI Configuration File
@@ -126,14 +143,24 @@ async function run () {
126
143
throw new Error ( `Terraform version ${ version } not available for ${ platform } and ${ arch } ` ) ;
127
144
}
128
145
129
- // Download requested version
130
- const pathToCLI = await downloadCLI ( build . url ) ;
146
+ // Check cache for requested version, then download if not present
147
+ let pathToCLI = tc . find ( CACHE_KEY , release . version , os . arch ( ) ) ;
148
+
149
+ // Check to see if wrapper has been installed in a previous run
150
+ const hasWrapper = pathToCLI && checkWrapper ( pathToCLI ) ;
151
+
152
+ if ( ! pathToCLI ) {
153
+ pathToCLI = await downloadCLI ( build . url , release . version ) ;
154
+ }
131
155
132
156
// Install our wrapper
133
- if ( wrapper ) {
157
+ if ( wrapper && ! hasWrapper ) {
134
158
await installWrapper ( pathToCLI ) ;
135
159
}
136
160
161
+ // Export a new environment variable, so our wrapper can locate the binary
162
+ core . exportVariable ( 'TERRAFORM_CLI_PATH' , pathToCLI ) ;
163
+
137
164
// Add to path
138
165
core . addPath ( pathToCLI ) ;
139
166
0 commit comments