-
Notifications
You must be signed in to change notification settings - Fork 4
373 lines (327 loc) · 15.1 KB
/
reusable-workflow-terraform.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
---
on: # yamllint disable-line rule:truthy
workflow_call:
inputs:
component:
required: true
type: string
terraform-version:
default: latest
required: false
type: string
concurrency:
group: ${{ inputs.component }}
jobs:
determine-workflow-mode:
name: Determine Workflow Mode
runs-on: ubuntu-latest
outputs:
mode: ${{ steps.determine_mode.outputs.mode }}
steps:
- name: Determine Mode
id: determine_mode
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "mode=Apply" >>"${GITHUB_OUTPUT}"
else
echo "mode=Plan" >>"${GITHUB_OUTPUT}"
fi
terraform-static-analysis:
if: (github.ref != 'refs/heads/main' && github.actor != 'dependabot[bot]')
name: Static Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout
id: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Build path-filters file
id: build_path_filters
run: bash scripts/path-filter/configuration-generator.sh terraform
- name: Prepare Environment
id: prepare_environment
shell: bash
run: |
workingDirectory=$(yq -e ".${{ inputs.component }}" .github/path-filter/terraform.yml | sed 's/.\{3\}$//')
export workingDirectory
echo "working-directory=${workingDirectory}" >>"${GITHUB_ENV}"
- name: Checkov
if: github.ref != 'refs/heads/main'
id: terraform_static_analysis_checkov
uses: bridgecrewio/checkov-action@abac673d65af73d7e91577b64a313f7e7401bd6e # v12.2959.0
with:
directory: ${{ env.working-directory }}
framework: terraform
download_external_modules: false
quiet: true
output_format: cli
continue-on-error: true
- name: Trivy
if: github.ref != 'refs/heads/main'
id: terraform_static_analysis_trivy
uses: aquasecurity/trivy-action@18f2510ee396bbf400402947b394f2dd8c87dbb0 # v0.29.0
with:
scan-type: config
scan-ref: ${{ env.working-directory }}
exit-code: 1
continue-on-error: true
- name: Check for Static Analysis Override Label
if: github.ref != 'refs/heads/main'
id: check_for_static_analysis_override
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
result-encoding: string
script: |
const labels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const overrideStaticAnalysis = labels.data.find(label => {
return label.name === 'override-static-analysis'
})
if (overrideStaticAnalysis) {
console.log('Found label')
return "true"
} else {
console.log('Did not find label')
return "false"
}
- name: Comment on Pull Request
if: github.event_name == 'pull_request'
id: comment
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const output = `### Terraform Component 🧱: \`${{ inputs.component }}\`
#### Checkov 🛂: \`${{ steps.terraform_static_analysis_checkov.outcome }}\`
#### Trivy 🛂: \`${{ steps.terraform_static_analysis_trivy.outcome }}\`
#### Static Analysis Override Label 🏷️: \`${{ steps.check_for_static_analysis_override.outputs.result }}\`
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.working-directory }}\`, Workflow: \`${{ github.workflow }}\`, Marker: \`${{ inputs.component }}_static_analysis\`*`;
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('${{ inputs.component }}_static_analysis')
})
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
})
}
- name: Process Outcomes
if: github.ref != 'refs/heads/main'
id: process_outcomes
shell: bash
run: |
failBuild=false
if [[ "${{ steps.terraform_static_analysis_checkov.outcome }}" != "success" ]] && [[ "${{ github.actor }}" == "dependabot[bot]" ]]; then
echo "OVERRIDDEN (Dependabot): Terraform Static Analysis - Checkov" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_static_analysis_checkov.outcome }}" != "success" ]] && [[ "${{ steps.check_for_static_analysis_override.outputs.result }}" == "false" ]]; then
export failBuild=true
echo "FAIL: Terraform Static Analysis - Checkov" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_static_analysis_checkov.outcome }}" != "success" ]] && [[ "${{ steps.check_for_static_analysis_override.outputs.result }}" == "true" ]]; then
echo "OVERRIDDEN (by label): Terraform Static Analysis - Checkov" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_static_analysis_checkov.outcome }}" == "success" ]]; then
echo "PASS: Terraform Static Analysis - Checkov" >>"${GITHUB_STEP_SUMMARY}"
fi
if [[ "${{ steps.terraform_static_analysis_trivy.outcome }}" != "success" ]] && [[ "${{ github.actor }}" == "dependabot[bot]" ]]; then
echo "OVERRIDDEN (Dependabot): Terraform Static Analysis - Trivy" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_static_analysis_trivy.outcome }}" != "success" ]] && [[ "${{ steps.check_for_static_analysis_override.outputs.result }}" == "false" ]]; then
export failBuild=true
echo "FAIL: Terraform Static Analysis - Trivy" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_static_analysis_trivy.outcome }}" != "success" ]] && [[ "${{ steps.check_for_static_analysis_override.outputs.result }}" == "true" ]]; then
echo "OVERRIDDEN (by label): Terraform Static Analysis - Trivy" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_static_analysis_trivy.outcome }}" == "success" ]]; then
echo "PASS: Terraform Static Analysis - Trivy" >>"${GITHUB_STEP_SUMMARY}"
fi
if [[ "${failBuild}" == "true" ]]; then
exit 1
fi
terraform:
needs: [determine-workflow-mode]
name: ${{ needs.determine-workflow-mode.outputs.mode }}
runs-on: ubuntu-latest
steps:
- name: Checkout
id: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Build path-filters file
id: build_path_filters
run: bash scripts/path-filter/configuration-generator.sh terraform
- name: Prepare Environment
id: prepare_environment
shell: bash
run: |
workingDirectory=$(yq -e ".${{ inputs.component }}" .github/path-filter/terraform.yml | sed 's/.\{3\}$//')
export workingDirectory
echo "working-directory=${workingDirectory}" >>"${GITHUB_ENV}"
- name: Configure AWS Credentials
id: configure_aws_credentials
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
with:
aws-region: eu-west-1
role-to-assume: arn:aws:iam::042130406152:role/GlobalGitHubActionAccess
- name: Assume GlobalGitHubActionAdmin Role
id: assume_global_github_action_admin_role
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
with:
aws-region: eu-west-1
role-to-assume: arn:aws:iam::042130406152:role/GlobalGitHubActionAdmin
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ env.AWS_SESSION_TOKEN }}
role-skip-session-tagging: true
- name: Retrieve AWS Secrets Manager Secrets
id: retrieve_aws_secrets_manager_secrets
uses: aws-actions/aws-secretsmanager-get-secrets@1d6311ab61b4856de027ff508aac818ddc1e141b # v2.0.7
with:
secret-ids: |
GITHUB_ROBOT_TOKEN, github-token
SLACK_WEBHOOK_URL, slack-webhook-url/terraform-failure
- name: Assume GlobalGitHubActionAccess Role
id: assume_global_github_action_access_role
uses: aws-actions/configure-aws-credentials@4fc4975a852c8cd99761e2de1f4ba73402e44dd9 # v4.0.3
with:
aws-region: eu-west-1
role-to-assume: arn:aws:iam::042130406152:role/GlobalGitHubActionAccess
- name: Set up Terraform
id: setup_terraform
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Prepare Terraform
id: terraform_prepare
shell: bash
run: |
echo "TF_IN_AUTOMATION=true" >> ${GITHUB_ENV}
- name: Initialise Terraform
id: terraform_init
shell: bash
working-directory: ${{ env.working-directory }}
run: |
terraform init \
-backend-config="assume_role={role_arn=\"arn:aws:iam::042130406152:role/GlobalGitHubActionAdmin\"}" \
-reconfigure \
-upgrade \
-no-color
- name: Validate Terraform
if: github.ref != 'refs/heads/main'
id: terraform_validate
working-directory: ${{ env.working-directory }}
shell: bash
run: |
terraform validate -no-color
- name: Plan Terraform
id: terraform_plan
working-directory: ${{ env.working-directory }}
shell: bash
run: |
terraform plan -input=false -no-color
continue-on-error: true
- name: Comment on Pull Request
if: github.event_name == 'pull_request'
id: comment
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const output = `### Terraform Component 🧱: \`${{ inputs.component }}\`
#### Terraform Initialization ⚙️: \`${{ steps.terraform_init.outcome }}\`
#### Terraform Validation 🤖: \`${{ steps.terraform_validate.outcome }}\`
#### Terraform Plan 🛠️: \`${{ steps.terraform_plan.outcome }}\`
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.working-directory }}\`, Workflow: \`${{ github.workflow }}\`, Marker: \`${{ inputs.component }}_plan\`*`;
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('${{ inputs.component }}_plan')
})
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
})
}
- name: Process Outcomes
if: github.ref != 'refs/heads/main'
id: process_outcomes
shell: bash
run: |
failBuild=false
if [[ "${{ steps.terraform_validate.outcome }}" != "success" ]]; then
export failBuild=true
echo "FAIL: Terraform Validation" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_validate.outcome }}" == "success" ]]; then
echo "PASS: Terraform Validation" >>"${GITHUB_STEP_SUMMARY}"
fi
if [[ "${{ steps.terraform_plan.outcome }}" != "success" ]]; then
export failBuild=true
echo "FAIL: Terraform Plan" >>"${GITHUB_STEP_SUMMARY}"
elif [[ "${{ steps.terraform_plan.outcome }}" == "success" ]]; then
echo "PASS: Terraform Plan" >>"${GITHUB_STEP_SUMMARY}"
fi
if [[ "${failBuild}" == "true" ]]; then
exit 1
fi
- name: Automatic Approval for Dependabot
if: (github.event_name == 'pull_request' && github.actor == 'dependabot[bot]' && contains(steps.terraform_plan.outputs.stdout, 'No changes. Your infrastructure matches the configuration.') || contains(steps.terraform_plan.outputs.stdout, 'No changes. Infrastructure is up-to-date.'))
id: automatic_approval
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ env.GITHUB_ROBOT_TOKEN }}
script: |
github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
body: 'Automatically approving as no changes were detected in the Terraform plan, and the pull request was raised by Dependabot.\n\n@dependabot merge 🤖 🤝 🤖',
event: 'APPROVE'
})
- name: Apply Terraform
if: github.ref == 'refs/heads/main'
id: terraform_apply
working-directory: ${{ env.working-directory }}
shell: bash
run: |
terraform apply -input=false -auto-approve -no-color
- name: Extract PR number from commit message
if: failure() && steps.terraform_apply.outcome == 'failure'
id: extract_pr
run: |
PR_NUMBER=$(git log -1 --pretty=%B | grep -oP '#\K\d+')
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
- name: Notify Slack on Failure
if: failure() && steps.terraform_apply.outcome == 'failure'
id: slack_notification
uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0
with:
webhook: ${{ env.SLACK_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
{
"run_id": "${{ github.run_id }}",
"pr_number": "${{ steps.extract_pr.outputs.pr_number }}"
}