Skip to content

Commit a8a2cbd

Browse files
authored
feat: add plugin caching (#347)
* feat: add plugin caching Adds opt-in caching for TFLint plugins to eliminate manual cache setup. - Add cache, tflint_config_path, plugin_dir inputs - Implement cache restore/save with @actions/cache - Add post action to save cache after workflow - Support glob patterns for monorepo configs - Cache key: tflint-plugins-{os}-{hash(configs)} - Restore keys: tflint-plugins-{os}- prefix - Add integration test for cache functionality * trigger actions run * chore: remove leftover dist files * test: remove placeholder cache tests * feat: support TFLINT_PLUGIN_DIR env var
1 parent 63ca199 commit a8a2cbd

File tree

14 files changed

+193366
-5095
lines changed

14 files changed

+193366
-5095
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
plugin "terraform" {
2+
enabled = true
3+
version = "0.13.0"
4+
source = "github.com/terraform-linters/tflint-ruleset-terraform"
5+
}

.github/workflows/test.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,22 @@ jobs:
165165
else
166166
echo "TFLint Exit Code captured."
167167
fi
168+
169+
integration-cache:
170+
name: 'Integration test: cache'
171+
runs-on: ubuntu-latest
172+
173+
steps:
174+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
175+
- name: Use Action
176+
uses: ./
177+
with:
178+
cache: true
179+
tflint_config_path: .github/workflows/fixtures/.tflint.hcl
180+
github_token: ${{ secrets.GITHUB_TOKEN }}
181+
- name: Init TFLint
182+
run: tflint --init --config .github/workflows/fixtures/.tflint.hcl
183+
env:
184+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
185+
- name: Verify plugin installed
186+
run: test -d ~/.tflint.d/plugins

README.md

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ Installs a wrapper script to wrap subsequent calls to `tflint` and expose `stdou
3737

3838
Default: `"false"`
3939

40+
### `cache`
41+
42+
Enable caching of TFLint plugins. When enabled, the action will cache the plugin directory and restore it on subsequent runs based on the hash of your TFLint configuration file(s).
43+
44+
Default: `"false"`
45+
46+
### `tflint_config_path`
47+
48+
Glob pattern for TFLint configuration file(s) used to generate the cache key. All matching files will be hashed together to determine cache validity. Supports glob patterns for monorepo setups.
49+
50+
Default: `".tflint.hcl"`
51+
52+
### `plugin_dir`
53+
54+
Directory where TFLint plugins are installed. See [TFLint plugin configuration](https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/config.md#plugin-directory) for details.
55+
56+
Can also be set via `TFLINT_PLUGIN_DIR` environment variable.
57+
58+
Default: `"~/.tflint.d/plugins"`
59+
4060
## Outputs
4161

4262
The following outputs are available when the `tflint_wrapper` input is enabled:
@@ -66,16 +86,12 @@ jobs:
6686
- uses: actions/checkout@v4
6787
name: Checkout source code
6888

69-
- uses: actions/cache@v4
70-
name: Cache plugin dir
71-
with:
72-
path: ~/.tflint.d/plugins
73-
key: ${{ matrix.os }}-tflint-${{ hashFiles('.tflint.hcl') }}
74-
7589
- uses: terraform-linters/setup-tflint@v6
7690
name: Setup TFLint
7791
with:
7892
tflint_version: v0.52.0
93+
cache: true
94+
7995
- name: Show version
8096
run: tflint --version
8197

@@ -133,6 +149,35 @@ or specify it explicitly as
133149
run: echo ${{ steps.tflint.outputs.stdout }}
134150
```
135151
152+
### Plugin Caching
153+
154+
```yaml
155+
- uses: terraform-linters/setup-tflint@v6
156+
with:
157+
cache: true
158+
159+
- run: tflint --init
160+
env:
161+
GITHUB_TOKEN: ${{ github.token }}
162+
163+
- run: tflint -f compact
164+
```
165+
166+
For monorepos with multiple TFLint configurations:
167+
168+
```yaml
169+
- uses: terraform-linters/setup-tflint@v6
170+
with:
171+
cache: true
172+
tflint_config_path: '**/.tflint.hcl'
173+
174+
- run: tflint --init
175+
env:
176+
GITHUB_TOKEN: ${{ github.token }}
177+
178+
- run: tflint -f compact
179+
```
180+
136181
### Checks
137182
138183
This action supports [Problem Matchers](https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md) for `--format compact`. You can see annotations in pull requests when TFLint prints issues with the `compact` format.

action.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ inputs:
1616
checksums:
1717
description: Newline-delimited list of valid checksums (SHA256 hashes) for the downloaded TFLint binary. When set, the action will verify that the binary matches one of these checksums before proceeding.
1818
required: false
19+
cache:
20+
description: Enable caching of TFLint plugins
21+
default: 'false'
22+
required: false
23+
tflint_config_path:
24+
description: Glob pattern for TFLint config files used to generate cache key
25+
default: '.tflint.hcl'
26+
required: false
27+
plugin_dir:
28+
description: Directory where TFLint plugins are installed. Can also be set via TFLINT_PLUGIN_DIR environment variable.
29+
default: '~/.tflint.d/plugins'
30+
required: false
1931
outputs:
2032
stdout:
2133
description: The output (stdout) produced by the tflint command. Only available if `tflint_wrapper` is set to `true`.
@@ -26,6 +38,8 @@ outputs:
2638
runs:
2739
using: 'node24'
2840
main: 'dist/index.js'
41+
post: 'dist/post/index.js'
42+
post-if: success()
2943
branding:
3044
icon: 'terminal'
3145
color: 'purple'

0 commit comments

Comments
 (0)