-
Notifications
You must be signed in to change notification settings - Fork 0
419 lines (393 loc) ยท 13.8 KB
/
terraform-module-validation.yml
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
---
name: Terraform Module Validation
on:
workflow_call:
secrets:
infracost-api-key:
description: "The API key for infracost"
required: false
inputs:
cicd-repository:
default: "appvia/appvia-cicd-workflows"
description: "The repository to pull the CI/CD workflows from"
required: false
type: string
cicd-branch:
default: "main"
description: "The branch to pull the CI/CD workflows from"
required: false
type: string
enable-infracost:
default: false
description: "Whether to run infracost on the Terraform Plan (secrets.infracost-api-key must be set if enabled)"
required: false
type: boolean
enable-commitlint:
default: true
description: "Whether to run commitlint on the commit message"
required: false
type: boolean
enable-terraform-tests:
default: true
description: "Whether to run terraform test"
required: false
type: boolean
enable-terraform-tests-credentials:
default: false
description: "Whether to run terraform test with AWS credentials"
required: false
type: boolean
terraform-dir:
default: "."
description: "The directory to validate"
required: false
type: string
terraform-tests-aws-region:
default: "eu-west-1"
description: "The AWS region to use for the terraform tests"
required: false
type: string
terraform-tests-aws-role:
description: "The AWS role to assume for the terraform tests"
required: false
type: string
terraform-version:
default: "1.7.1"
description: "The version of terraform to use"
required: false
type: string
working-directory:
default: "."
description: "Working directory"
required: false
type: string
env:
AWS_ROLE: ${{ inputs.aws-role }}
AWS_WEB_IDENTITY_TOKEN_FILE: /tmp/web_identity_token_file
TF_LOG: ${{ inputs.terraform-log-level }}
permissions:
contents: read
pull-requests: write
jobs:
commitlint:
name: "Commitlint"
if: github.event_name == 'pull_request' && inputs.enable-commitlint
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.commitlint.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Install Dependencies
run: npm install @commitlint/config-conventional @commitlint/cli
- name: Retrieve Commit Configuration
run: |
wget https://raw.githubusercontent.com/${{ inputs.cicd-repository }}/${{ inputs.cicd-branch }}/config/commitlint.config.js -O commitlint.config.js
- name: Run Commitlint
id: commitlint
run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }}
terraform-format:
name: "Terraform Format"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.format.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Terraform Format
id: format
run: terraform -chdir=${{ inputs.terraform-dir }} fmt -check -recursive
terraform-lint:
name: "Terraform Lint"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.lint.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Linter
uses: terraform-linters/setup-tflint@v4
- name: Terraform Init
run: terraform -chdir=${{ inputs.terraform-dir }} init -backend=false
- name: Setup Linter
uses: terraform-linters/setup-tflint@v4
- name: Linter Initialize
run: tflint --init
- name: Linting Code
id: lint
run: tflint -f compact
terraform-tests:
name: "Terraform Unit Tests"
runs-on: ubuntu-latest
if: inputs.enable-terraform-tests
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.tests.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Retrieve Web Identity Token for AWS Authentication
if: inputs.enable-terraform-tests-credentials
run: |
curl -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" | jq -r '.value' > $AWS_WEB_IDENTITY_TOKEN_FILE
- name: Authenticate with AWS
if: inputs.enable-terraform-tests-credentials
id: auth
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ inputs.terraform-tests-aws-region }}
role-session-name: ${{ github.event.repository.name }}
role-to-assume: ${{ inputs.terraform-tests-aws-role }}
mask-aws-account-id: "no"
- name: Terraform Init
run: terraform -chdir=${{ inputs.terraform-dir }} init -backend=false
- name:
- name: Run Tests
id: tests
run: terraform -chdir=${{ inputs.terraform-dir }} test
terraform-init:
name: "Terraform Init"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.init.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Terraform Init
id: init
run: terraform -chdir=${{ inputs.terraform-dir }} init
terraform-validate:
name: "Terraform Validate"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.validate.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Terraform Init
id: init
run: terraform -chdir=${{ inputs.terraform-dir }} init
- name: Terraform Validate
id: validate
run: terraform -chdir=${{ inputs.terraform-dir }} validate -no-color
terraform-validate-examples:
name: "Terraform Validate Examples"
runs-on: ubuntu-latest
outputs:
result: ${{ steps.validate-examples.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Validate Examples
id: validate-examples
run: |
for dir in examples/*; do
if [ -d "$dir" ]; then
echo "--> Validating $dir"
terraform -chdir=${dir} init -backend=false
terraform -chdir=${dir} validate -no-color
fi
done
terraform-docs:
name: "Terraform Docs"
runs-on: ubuntu-latest
if: ${{ github.actor != 'dependabot[bot]' }}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.docs.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Terraform Docs
id: docs
uses: terraform-docs/[email protected]
with:
fail-on-diff: true
working-dir: ${{ inputs.terraform-dir }}
terraform-security:
name: "Terraform Security"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.security.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Terraform security
id: security
uses: aquasecurity/[email protected]
with:
working_directory: ${{ inputs.working-directory }}
terraform-infracost:
name: "Get Cost Estimate"
if: github.event_name == 'pull_request' && inputs.enable-infracost
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- name: Setup Infracost
uses: infracost/actions/setup@v2
with:
api-key: ${{ secrets.infracost-api-key }}
currency: GBP
- name: Checkout PR Base Branch
uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.base.ref }}"
- name: Check For TF Files
id: baseline
run: |
if ls *.tf 2>/dev/null; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Generate Cost Estimate Baseline
if: steps.baseline.outputs.exists == 'true'
run: |
infracost breakdown --path=${TF_ROOT} \
--format=json \
--out-file=/tmp/infracost-base.json
- name: Checkout PR branch
uses: actions/checkout@v4
- name: Generate Cost Estimate Difference
if: steps.baseline.outputs.exists == 'true'
run: |
infracost diff --path=${TF_ROOT} \
--format=json \
--compare-to=/tmp/infracost-base.json \
--out-file=/tmp/infracost.json
- name: Generate Infracost Cost Estimate
if: steps.baseline.outputs.exists == 'false'
run: |
infracost breakdown --path=${TF_ROOT} \
--format=json \
--out-file=/tmp/infracost.json
- name: Post Infracost comment
run: |
infracost comment github --path=/tmp/infracost.json \
--repo=$GITHUB_REPOSITORY \
--github-token=${{github.token}} \
--pull-request=${{github.event.pull_request.number}} \
--behavior=update
update-pr:
name: "Update PR"
if: github.event_name == 'pull_request' && (success() || failure())
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
needs:
- commitlint
- terraform-docs
- terraform-format
- terraform-init
- terraform-lint
- terraform-tests
- terraform-security
- terraform-validate
- terraform-validate-examples
steps:
- name: Add PR Comment
uses: actions/github-script@v6
with:
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('Pull Request Review Status') && comment.body.includes('<b>Working Directory:</b> \`${{ inputs.working-directory }}\`')
})
// 2. Prepare format of the comment
const output = `### Pull Request Review Status
* ๐ <b>Terraform Format and Style:</b> \`${{ needs.terraform-format.outputs.result }}\`
* ๐ <b>Terraform Linting:</b> \`${{ needs.terraform-lint.outputs.result }}\`
* ๐ฎ <b>Terraform Security Check:</b> \`${{ needs.terraform-security.outputs.result }}\`
* ๐ <b>Terraform Tests:</b> \`${{ needs.terraform-tests.outputs.result }}\`
* ๐ง <b>Terraform Initialisation:</b> \`${{ needs.terraform-init.outputs.result }}\`
* ๐ค <b>Terraform Validation:</b> \`${{ needs.terraform-validate.outputs.result }}\`
* ๐ค <b>Terraform Example Validation:</b> \`${{ needs.terraform-validate-examples.outputs.result }}\`
* ๐ <b>Terraform Documentation:</b> \`${{ needs.terraform-docs.outputs.result }}\`
* ๐ <b>Commitlint:</b> \`${{ needs.commitlint.outputs.result }}\`
*<b>Working Directory:</b> \`${{ inputs.working-directory }}\`*
*<b>Pusher:</b> @${{ github.actor }}, <b>Action:</b> \`${{ github.event_name }}\`*
*<b>Workflow Run Link:</b> ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}*`;
// 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
})
}