Skip to content

Commit acd1575

Browse files
authored
feat: cache tflint binary using @actions/tool-cache (#355)
Integrates the official @actions/tool-cache library to cache the TFLint binary installation under the runners' standard tool cache directory (_tool). This significantly improves workflow efficiency by reusing cached binaries across runs instead of downloading fresh on each run. Closes #346
1 parent d75d07d commit acd1575

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

dist/index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88963,6 +88963,15 @@ async function restoreCache() {
8896388963

8896488964

8896588965

88966+
/**
88967+
* Normalize version for tool-cache compatibility
88968+
* @param {string} version - Version string (e.g., "v0.50.0" or "0.50.0")
88969+
* @returns {string} - Normalized version without "v" prefix
88970+
*/
88971+
function normalizeVersion(version) {
88972+
return version.replace(/^v/, '');
88973+
}
88974+
8896688975
/**
8896788976
* Get the GitHub platform architecture name
8896888977
* @param {string} arch - https://nodejs.org/api/os.html#os_os_arch
@@ -89075,14 +89084,26 @@ async function run() {
8907589084
const version = await getTFLintVersion(inputVersion);
8907689085
const platform = mapOS(external_os_.platform());
8907789086
const arch = mapArch(external_os_.arch());
89087+
const normalizedVersion = normalizeVersion(version);
8907889088

89079-
core.debug(`Getting download URL for tflint version ${version}: ${platform} ${arch}`);
89080-
const url = `https://github.com/terraform-linters/tflint/releases/download/${version}/tflint_${platform}_${arch}.zip`;
89089+
// Check if tool is already cached
89090+
let pathToCLI = tool_cache.find('tflint', normalizedVersion, arch);
89091+
if (pathToCLI) {
89092+
core.info(`Found TFLint ${version} in cache @ ${pathToCLI}`);
89093+
} else {
89094+
core.debug(`Getting download URL for tflint version ${version}: ${platform} ${arch}`);
89095+
const url = `https://github.com/terraform-linters/tflint/releases/download/${version}/tflint_${platform}_${arch}.zip`;
89096+
89097+
pathToCLI = await downloadCLI(url, checksums, version);
8908189098

89082-
const pathToCLI = await downloadCLI(url, checksums, version);
89099+
if (wrapper) {
89100+
await installWrapper(pathToCLI);
89101+
}
8908389102

89084-
if (wrapper) {
89085-
await installWrapper(pathToCLI);
89103+
// Cache the tool for future runs
89104+
core.info('Adding to the tool cache...');
89105+
pathToCLI = await tool_cache.cacheDir(pathToCLI, 'tflint', normalizedVersion, arch);
89106+
core.info(`Successfully cached TFLint to ${pathToCLI}`);
8908689107
}
8908789108

8908889109
core.addPath(pathToCLI);

src/setup-tflint.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import { Octokit } from '@octokit/rest';
1212

1313
import restoreCache from './cache-restore.js';
1414

15+
/**
16+
* Normalize version for tool-cache compatibility
17+
* @param {string} version - Version string (e.g., "v0.50.0" or "0.50.0")
18+
* @returns {string} - Normalized version without "v" prefix
19+
*/
20+
function normalizeVersion(version) {
21+
return version.replace(/^v/, '');
22+
}
23+
1524
/**
1625
* Get the GitHub platform architecture name
1726
* @param {string} arch - https://nodejs.org/api/os.html#os_os_arch
@@ -124,14 +133,26 @@ async function run() {
124133
const version = await getTFLintVersion(inputVersion);
125134
const platform = mapOS(os.platform());
126135
const arch = mapArch(os.arch());
136+
const normalizedVersion = normalizeVersion(version);
137+
138+
// Check if tool is already cached
139+
let pathToCLI = tc.find('tflint', normalizedVersion, arch);
140+
if (pathToCLI) {
141+
core.info(`Found TFLint ${version} in cache @ ${pathToCLI}`);
142+
} else {
143+
core.debug(`Getting download URL for tflint version ${version}: ${platform} ${arch}`);
144+
const url = `https://github.com/terraform-linters/tflint/releases/download/${version}/tflint_${platform}_${arch}.zip`;
127145

128-
core.debug(`Getting download URL for tflint version ${version}: ${platform} ${arch}`);
129-
const url = `https://github.com/terraform-linters/tflint/releases/download/${version}/tflint_${platform}_${arch}.zip`;
146+
pathToCLI = await downloadCLI(url, checksums, version);
130147

131-
const pathToCLI = await downloadCLI(url, checksums, version);
148+
if (wrapper) {
149+
await installWrapper(pathToCLI);
150+
}
132151

133-
if (wrapper) {
134-
await installWrapper(pathToCLI);
152+
// Cache the tool for future runs
153+
core.info('Adding to the tool cache...');
154+
pathToCLI = await tc.cacheDir(pathToCLI, 'tflint', normalizedVersion, arch);
155+
core.info(`Successfully cached TFLint to ${pathToCLI}`);
135156
}
136157

137158
core.addPath(pathToCLI);

0 commit comments

Comments
 (0)