-
Notifications
You must be signed in to change notification settings - Fork 0
358 lines (298 loc) · 10.3 KB
/
validate.yaml
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
name: Validate
on:
workflow_dispatch: { }
push:
branches: [main]
pull_request:
jobs:
fmt:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
provider: [aws]
steps:
- uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
name: Checkout PR branch
with:
ref: ${{ github.head_ref }}
- uses: actions/checkout@v3
if: ${{ github.event_name != 'pull_request' }}
name: Checkout
with:
fetch-depth: 0
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ">=1.2.0"
- name: Terraform Format
run: terraform fmt
working-directory: ${{ matrix.provider }}
- name: Extract branch name
run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
commit-message: "GitHub Actions: Refactor: Automated formatting of terraform code"
title: Reformat terraform files
body: Update terraform files to canonical format using `terraform fmt`
branch: automated-terraform-fmt
branch-suffix: short-commit-hash
delete-branch: true
add-paths: ${{ matrix.provider }}
checkov:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
provider: [aws]
steps:
- uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
name: Checkout PR branch
with:
ref: ${{ github.head_ref }}
- uses: actions/checkout@v3
if: ${{ github.event_name != 'pull_request' }}
name: Checkout
with:
fetch-depth: 0
- name: Set up Python 3.8 for Checkov
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Test with Checkov
id: checkov
uses: bridgecrewio/[email protected]
with:
directory: ${{ matrix.provider }}
framework: terraform
download_external_modules: true
skip_check: CKV_AWS_79,CKV2_AWS_38
costs:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
provider: [aws]
steps:
- name: Setup Infracost
if: ${{ github.event_name == 'pull_request' }}
uses: infracost/actions/setup@v2
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- name: Checkout base branch
if: ${{ github.event_name == 'pull_request' }}
uses: actions/checkout@v3
with:
ref: '${{ github.event.pull_request.base.ref }}'
- name: Generate Infracost cost estimate baseline
if: ${{ github.event_name == 'pull_request' }}
run: infracost breakdown --path=${{ matrix.provider }} --format=json --out-file=/tmp/infracost-base.json
- uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
name: Checkout PR branch
with:
ref: ${{ github.head_ref }}
- name: Generate Infracost diff
if: ${{ github.event_name == 'pull_request' }}
run: |
infracost diff --path=${{ matrix.provider }} --format=json \
--compare-to=/tmp/infracost-base.json --out-file=/tmp/infracost.json
- name: Post Infracost comment
if: ${{ github.event_name == 'pull_request' }}
run: |
infracost comment github --path=/tmp/infracost.json \
--repo=$GITHUB_REPOSITORY --github-token=${{ github.token }} \
--pull-request=${{ github.event.pull_request.number }} \
--behavior=update
validate:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
provider: [aws]
steps:
- uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
name: Checkout PR branch
with:
ref: ${{ github.head_ref }}
- uses: actions/checkout@v3
if: ${{ github.event_name != 'pull_request' }}
name: Checkout
with:
fetch-depth: 0
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ">=1.2.0"
- name: Terraform fmt
id: fmt
run: terraform fmt -check
continue-on-error: ${{ github.event_name == 'pull_request' }}
working-directory: ${{ matrix.provider }}
- name: Terraform Init
id: init
run: terraform init -backend=false
working-directory: ${{ matrix.provider }}
- name: Terraform Validate
id: validate
run: terraform validate -no-color
continue-on-error: ${{ github.event_name == 'pull_request' }}
working-directory: ${{ matrix.provider }}
- uses: actions/github-script@v6
if: ${{ github.event_name == 'pull_request' }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style') && comment.body.includes('${{ matrix.provider }}') && comment.body.includes('${{ matrix.os }}')
})
// 2. Prepare format of the comment
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>
\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, OS: \`${{ matrix.os }}\`, Working Directory: \`${{ matrix.provider }}\`, Workflow: \`${{ github.workflow }}\`*`;
// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}
docs:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
provider: [aws]
steps:
- uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
name: Checkout PR branch
with:
ref: ${{ github.head_ref }}
- uses: actions/checkout@v3
if: ${{ github.event_name != 'pull_request' }}
name: Checkout
with:
fetch-depth: 0
- name: Render terraform docs and push changes back
uses: terraform-docs/gh-actions@v1
with:
working-dir: ${{ matrix.provider }}
output-file: README.md
output-method: inject
git-push: ${{ github.event_name != 'pull_request' }}
fail-on-diff: ${{ github.event_name == 'pull_request' }}
git-commit-message: "GitHub Actions: Docs: Automated update of README.md"
tflint:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
provider: [aws]
steps:
- uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
name: Checkout PR branch
with:
ref: ${{ github.head_ref }}
- uses: actions/checkout@v3
if: ${{ github.event_name != 'pull_request' }}
name: Checkout
with:
fetch-depth: 0
- uses: actions/cache@v2
name: Cache TFLint plugin dir
with:
path: ~/.tflint.d/plugins
key: ${{ matrix.os }}-tflint-${{ hashFiles('.tflint.hcl') }}
- uses: terraform-linters/setup-tflint@v2
name: Setup TFLint
with:
tflint_version: v0.42.0
- name: Run TFLint
run: |
tflint --init
tflint -f compact
working-directory: ${{ matrix.provider }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
tfsec:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
provider: [aws]
permissions:
actions: read
contents: read
pull-requests: write
security-events: write
statuses: write
steps:
- uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
name: Checkout PR branch
with:
ref: ${{ github.head_ref }}
- uses: actions/checkout@v3
if: ${{ github.event_name != 'pull_request' }}
name: Checkout
with:
fetch-depth: 0
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ">=1.2.0"
- name: Terraform Init
id: init
run: terraform init -backend=false
working-directory: ${{ matrix.provider }}
- name: tfsec
if: ${{ github.event_name == 'pull_request' }}
uses: aquasecurity/[email protected]
with:
github_token: ${{ github.token }}
working_directory: ${{ matrix.provider }}
- name: tfsec
if: ${{ github.event_name != 'pull_request' }}
uses: aquasecurity/tfsec-sarif-action@master
with:
sarif_file: tfsec.sarif
working_directory: ${{ matrix.provider }}
- name: 'Upload Artifact'
if: ${{ github.event_name != 'pull_request' }}
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.provider }}_tfsec.sarif
path: tfsec.sarif
retention-days: 7
- name: Upload SARIF file
if: ${{ github.event_name != 'pull_request' }}
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: tfsec.sarif