@@ -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,8 @@ function mapOS (os) {
28
31
return mappings [ os ] || os ;
29
32
}
30
33
31
- async function downloadCLI ( url ) {
34
+ async function downloadCLI ( url , version ) {
35
+ // Look for CLI in the cache first
32
36
core . debug ( `Downloading Terraform CLI from ${ url } ` ) ;
33
37
const pathToCLIZip = await tc . downloadTool ( url ) ;
34
38
@@ -40,7 +44,24 @@ async function downloadCLI (url) {
40
44
throw new Error ( `Unable to download Terraform from ${ url } ` ) ;
41
45
}
42
46
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 ;
44
65
}
45
66
46
67
async function installWrapper ( pathToCLI ) {
@@ -70,9 +91,6 @@ async function installWrapper (pathToCLI) {
70
91
core . error ( `Unable to copy ${ source } to ${ target } .` ) ;
71
92
throw e ;
72
93
}
73
-
74
- // Export a new environment variable, so our wrapper can locate the binary
75
- core . exportVariable ( 'TERRAFORM_CLI_PATH' , pathToCLI ) ;
76
94
}
77
95
78
96
// Add credentials to CLI Configuration File
@@ -126,14 +144,24 @@ async function run () {
126
144
throw new Error ( `Terraform version ${ version } not available for ${ platform } and ${ arch } ` ) ;
127
145
}
128
146
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
+ }
131
156
132
157
// Install our wrapper
133
- if ( wrapper ) {
158
+ if ( wrapper && ! hasWrapper ) {
134
159
await installWrapper ( pathToCLI ) ;
135
160
}
136
161
162
+ // Export a new environment variable, so our wrapper can locate the binary
163
+ core . exportVariable ( 'TERRAFORM_CLI_PATH' , pathToCLI ) ;
164
+
137
165
// Add to path
138
166
core . addPath ( pathToCLI ) ;
139
167
0 commit comments