-
Notifications
You must be signed in to change notification settings - Fork 294
163 lines (140 loc) · 5.76 KB
/
pre-commit.yaml
File metadata and controls
163 lines (140 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
name: pre-commit
on:
pull_request:
branches:
- main
paths:
- '**.tf'
- '**.yml'
- '**.yaml'
env:
TERRAFORM_DOCS_VERSION: v0.20.0
TFSEC_VERSION: v1.28.14
TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/.terraform.d/plugin-cache
TFLINT_VERSION: v0.56.0
concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
cancel-in-progress: true
permissions:
contents: read
pull-requests: read
jobs:
collectInputs:
name: Collect workflow inputs
runs-on: ubuntu-latest
outputs:
directories: ${{ steps.dirs.outputs.directories }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get root directories
id: dirs
uses: clowdhaus/terraform-composite-actions/directories@v1.11.1
preCommitMinVersions:
name: Min TF pre-commit
needs: collectInputs
runs-on: ubuntu-latest
strategy:
matrix:
directory: ${{ fromJson(needs.collectInputs.outputs.directories) }}
steps:
- name: Remove default Terraform
run: rm -rf $(which terraform)
- name: Checkout
uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: changes
with:
# We only need to check Terraform files for the current directory
# because the `preCommitMaxVersion` job will run the full,
# exhaustive checks (always)
filters: |
src:
- '${{ matrix.directory }}/*.tf'
- name: Config Terraform plugin cache
if: steps.changes.outputs.src== 'true'
run: mkdir --parents ${{ env.TF_PLUGIN_CACHE_DIR }}
- name: Restore Terraform Cache
id: restore-cache
uses: actions/cache/restore@v4
if: steps.changes.outputs.src== 'true'
with:
# restore latest matching cache to the tf cache dir
# matches exact branch and versions hash, then any cache from the same branch, then any cache from any branch
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ github.head_ref }}-${{ hashFiles('**/versions.tf') }}
restore-keys: |
${{ runner.os }}-terraform-${{ github.head_ref }}
${{ runner.os }}-terraform-
- name: Terraform min/max versions
uses: clowdhaus/terraform-min-max@v1.3.2
if: steps.changes.outputs.src== 'true'
id: minMax
with:
directory: ${{ matrix.directory }}
- name: Pre-commit Terraform ${{ steps.minMax.outputs.minVersion }}
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.11.1
# Run only validate pre-commit check on min version supported
if: ${{ matrix.directory != '.' && steps.changes.outputs.src== 'true' }}
with:
terraform-version: ${{ steps.minMax.outputs.minVersion }}
args: 'terraform_validate --color=always --show-diff-on-failure --files ${{ matrix.directory }}/*'
- name: Pre-commit Terraform ${{ steps.minMax.outputs.minVersion }}
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.11.1
# Run only validate pre-commit check on min version supported
if: ${{ matrix.directory == '.' && steps.changes.outputs.src== 'true' }}
with:
terraform-version: ${{ steps.minMax.outputs.minVersion }}
args: 'terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf)'
preCommitMaxVersion:
name: Max TF pre-commit
runs-on: ubuntu-latest
needs: collectInputs
steps:
- name: Remove default Terraform
run: rm -rf $(which terraform)
- name: Checkout
uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: changes
with:
filters: |
src:
- '**/*.tf'
- name: Config Terraform plugin cache
if: steps.changes.outputs.src== 'true'
run: mkdir --parents ${{ env.TF_PLUGIN_CACHE_DIR }}
- name: Restore Terraform Cache
id: restore-cache
uses: actions/cache/restore@v4
if: steps.changes.outputs.src== 'true'
with:
# restore latest matching cache to the tf cache dir
# matches exact branch and versions hash, then any cache from the same branch, then any cache from any branch
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ github.head_ref }}-${{ hashFiles('**/versions.tf') }}
restore-keys: |
${{ runner.os }}-terraform-${{ github.head_ref }}
${{ runner.os }}-terraform-
- name: Install tfsec
if: steps.changes.outputs.src== 'true'
run: curl -sSLo ./tfsec https://github.com/aquasecurity/tfsec/releases/download/${{ env.TFSEC_VERSION }}/tfsec-$(uname)-amd64 && chmod +x tfsec && sudo mv tfsec /usr/bin/
- name: Terraform min/max versions
id: minMax
uses: clowdhaus/terraform-min-max@v1.3.2
if: steps.changes.outputs.src== 'true'
- name: Pre-commit Terraform ${{ steps.minMax.outputs.maxVersion }}
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.11.1
if: steps.changes.outputs.src== 'true'
with:
terraform-version: ${{ steps.minMax.outputs.maxVersion }}
terraform-docs-version: ${{ env.TERRAFORM_DOCS_VERSION }}
tflint-version: ${{ env.TFLINT_VERSION }}
- name: Save Terraform Cache
uses: actions/cache/save@v4
if: steps.changes.outputs.src== 'true'
with:
# caches cannot be updated, we can only save this hash once,
# after validating all blueprints we should have all plugins in cache.
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ github.head_ref }}-${{ hashFiles('**/versions.tf') }}