-
Notifications
You must be signed in to change notification settings - Fork 0
275 lines (248 loc) · 11.2 KB
/
terraform-module-ci.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
name: "DSB Terraform Module CI"
#
# A requirement for this workflow to run is that the following 'permissions' are granted by the calling workflow:
# permissions:
# id-token: write # required for Azure password-less auth
# contents: read # required for actions/checkout
# pull-requests: write # required for commenting on PRs
#
# The following secrets must be available in the github 'secrets' context:
# - secrets.REPO_AZURE_DSB_TENANT_ID <-- ID of the Azure tenant to run the workflow in context of
# - secrets.REPO_AZURE_SUBSCRIPTION_ID <-- ID of the Azure subscription to run the workflow in context of
# - secrets.REPO_AZURE_TERRAFORM_USER_SERVICE_PRINCIPAL <-- ID of the Entra ID service principal with access to the Azure subscription
#
# From the calling workflow this can be achieved by either handing over all secrets:
# secrets: inherit
# or by handing over just the required secrets from the calling workflow:
# secrets:
# REPO_AZURE_DSB_TENANT_ID: "secret value"
# REPO_AZURE_SUBSCRIPTION_ID: "secret value"
# REPO_AZURE_TERRAFORM_USER_SERVICE_PRINCIPAL: "secret value"
#
on:
workflow_call:
inputs:
terraform-version:
description: "Terraform version to use for the tests."
required: true
type : string
tflint-version:
description: "TFLint version to use for the tests."
required: true
type : string
env:
ARM_TENANT_ID: ${{ secrets.REPO_AZURE_DSB_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.REPO_AZURE_SUBSCRIPTION_ID }}
ARM_CLIENT_ID: ${{ secrets.REPO_AZURE_TERRAFORM_USER_SERVICE_PRINCIPAL }}
ARM_USE_OIDC: true
ARM_USE_AZUREAD: true
TF_IN_AUTOMATION: true
jobs:
create-matrix:
name: Create job matrix
runs-on: [self-hosted, dsb-terraformer, linux, x64]
defaults:
run:
shell: bash
outputs:
all-tests: ${{ steps.create-matrix.outputs.all-tests }}
plugin-cache-directory: ${{ steps.setup-terraform-cache.outputs.plugin-cache-directory }}
plugin-cache-key-monthly-rolling: ${{ steps.setup-terraform-cache.outputs.plugin-cache-key-monthly-rolling }}
steps:
- name: "🧹 Clean workspace"
uses: dsb-norge/directory-recreate@v1
- name: "⬇ Checkout working branch"
uses: actions/checkout@v4
- name: "🎰 Create env matrix"
id: create-matrix
uses: dsb-norge/github-actions-terraform/create-tftest-matrix@v0
- name: "📥 Setup Terraform"
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
# woraround for extra gibberish in output https://github.com/hashicorp/setup-terraform/issues/20
terraform_wrapper: false
- name: "🗄️ Setup Terraform provider plugin cache"
id: setup-terraform-cache
uses: dsb-norge/github-actions-terraform/setup-terraform-plugin-cache@v0
- name: "📥 Setup TFLint"
id: setup-tflint
uses: dsb-norge/github-actions-terraform/setup-tflint@v0
with:
tflint-version: ${{ inputs.tflint-version }}
working-directory: ${{ github.workspace }}
- name: "🚀 Cache Terraform provider plugins"
uses: actions/cache@v4
with:
path: ${{ steps.setup-terraform-cache.outputs.plugin-cache-directory }}
key: ${{ steps.setup-terraform-cache.outputs.plugin-cache-key-monthly-rolling }}
- name: ⚙️ Terraform Init
id: init
uses: dsb-norge/github-actions-terraform/terraform-init@v0
with:
working-directory: ${{ github.workspace }}
additional-dirs-json: null
plugin-cache-directory: ${{ steps.setup-terraform-cache.outputs.plugin-cache-directory }}
continue-on-error: true # allow job to continue, step outcome is evaluated later
- name: 🖌 Terraform Format
id: fmt
uses: dsb-norge/github-actions-terraform/terraform-fmt@v0
with:
working-directory: ${{ github.workspace }}
format-check-in-root-dir: true
continue-on-error: true # allow job to continue, step outcome is evaluated later
- name: ✔ Terraform Validate
id: validate
uses: dsb-norge/github-actions-terraform/terraform-validate@v0
with:
working-directory: ${{ github.workspace }}
continue-on-error: true # allow job to continue, step outcome is evaluated later
- name: 🧹 Lint with TFLint
id: lint
uses: dsb-norge/github-actions-terraform/lint-with-tflint@v0
with:
working-directory: ${{ github.workspace }}
continue-on-error: true # allow job to continue, step outcome is evaluated later
- name: 📝 Create validation summary
id: create-validation-summary
if: github.event_name == 'pull_request'
uses: dsb-norge/github-actions-terraform/create-validation-summary@v0
with:
environment-name: "module"
plan-txt-output-file: ""
status-init: ${{ steps.init.outcome }}
status-fmt: ${{ steps.fmt.outcome }}
status-validate: ${{ steps.validate.outcome }}
status-lint: ${{ steps.lint.outcome }}
status-plan: "N/A"
continue-on-error: true # allow job to continue, step outcome is ignored
- name: 🏷️ Add validation summary as pull request comment
id: validation-summary-on-pr
if: steps.create-validation-summary.outcome == 'success'
uses: dsb-norge/github-actions/ci-cd/comment-on-pr@v2
with:
pr-comment-text: ${{ steps.create-validation-summary.outputs.summary }}
delete-comments-starting-with: ${{ steps.create-validation-summary.outputs.prefix }}
continue-on-error: true # allow job to continue, step outcome is ignored
- name: "🧐 Validation outcome: ⚙️ Init"
run: |
if [ ! "${{ steps.init.outcome }}" == 'success' ]; then
echo "::error title=Init failed::Outcome of terraform init step was '${{ steps.init.outcome }}'!"
exit 1
fi
continue-on-error: false
- name: "🧐 Validation outcome: 🖌 Format"
run: |
if [ ! "${{ steps.fmt.outcome }}" == 'success' ]; then
echo "::error title=Format check failed::Outcome of terraform fmt step was '${{ steps.fmt.outcome }}'!"
exit 1
fi
continue-on-error: false
- name: "🧐 Validation outcome: ✔ Validate"
run: |
if [ ! "${{ steps.validate.outcome }}" == 'success' ]; then
echo "::error title=Validate failed::Outcome of terraform validate step was '${{ steps.validate.outcome }}' !"
exit 1
fi
continue-on-error: false
- name: "🧐 Validation outcome: 🧹 TFLint"
run: |
if [ ! "${{ steps.lint.outcome }}" == 'success' ]; then
echo "::error title=Lint failed::Outcome of TFLint step was '${{ steps.lint.outcome }}'!"
exit 1
fi
continue-on-error: false
terraform-module-ci:
name: "Terraform Test"
needs: create-matrix
runs-on: [self-hosted, dsb-terraformer, linux, x64]
strategy:
fail-fast: false # Allow jobs to continue even though one more env(s) fail
matrix:
test-file: ${{ fromJSON(needs.create-matrix.outputs.all-tests).files }}
defaults:
run:
shell: bash
steps:
- name: "⬇ Checkout"
uses: actions/checkout@v4
- name: "📥 Setup Terraform"
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
# woraround for extra gibberish in output https://github.com/hashicorp/setup-terraform/issues/20
terraform_wrapper: false
- name: "🚀 Cache Terraform provider plugins"
uses: actions/cache@v4
with:
path: ${{ needs.create-matrix.outputs.plugin-cache-directory }}
key: ${{ needs.create-matrix.outputs.plugin-cache-key-monthly-rolling }}
- name: ⚙️ Terraform Init
id: init
uses: dsb-norge/github-actions-terraform/terraform-init@v0
with:
working-directory: ${{ github.workspace }}
additional-dirs-json: null
plugin-cache-directory: ${{ needs.create-matrix.outputs.plugin-cache-directory }}
continue-on-error: true # allow job to continue, step outcome is evaluated later
- name: 🧪 Terraform Test
id: test
uses: dsb-norge/github-actions-terraform/terraform-test@v0
with:
test-file: ${{ matrix.test-file }}
continue-on-error: true # allow job to continue, step outcome is evaluated later
- name: 📝 Create test report
id: create-test-report
if: github.event_name == 'pull_request'
uses: dsb-norge/github-actions-terraform/create-test-report@v0
with:
test-out-file: ${{ steps.test.outputs.json }}
status-init: ${{ steps.init.outcome }}
status-test: ${{ steps.test.outcome }}
test-summary: ${{ steps.test.outputs.summary }}
test-report: ${{ steps.test.outputs.report }}
test-file: ${{ matrix.test-file }}
continue-on-error: true # allow job to continue, step outcome is evaluated later
- name: 🏷️ Add validation summary as pull request comment
id: validation-summary-on-pr
if: steps.create-test-report.outcome == 'success'
uses: dsb-norge/github-actions/ci-cd/comment-on-pr@v2
with:
pr-comment-text: ${{ steps.create-test-report.outputs.summary }}
delete-comments-starting-with: ${{ steps.create-test-report.outputs.prefix }}
continue-on-error: true # allow job to continue, step outcome is ignored
# Terminate the job with 'failure' if any validation check did not succeed.
# If 'allow-failing-terraform-operations' is 'true' for the environment the job will not terminate.
- name: "🧐 Validation outcome: ⚙️ Init"
run: |
if [ ! "${{ steps.init.outcome }}" == 'success' ]; then
echo "::error title=Init failed::Outcome of terraform init step was '${{ steps.init.outcome }}'!"
exit 1
fi
continue-on-error: false
- name: "🧐 Validation outcome: 🧪 Test"
run: |
if [ ! "${{ steps.test.outcome }}" == 'success' ]; then
echo "::error title=Test failed::Outcome of terraform test step was '${{ steps.test.outcome }}' for file '${{ matrix.test-file }}'!"
exit 1
fi
continue-on-error: false
# create a global result indicating if workflow steps succeeded or not,
# handy for branch protection rules
conclusion:
if: always()
name: "Terraform conclusion"
needs: [create-matrix, terraform-module-ci]
runs-on: ubuntu-latest # no need to schedule this on our own runners
defaults:
run:
shell: bash
steps:
- run: exit 1
# for explanation of '>-' below see https://stackoverflow.com/a/67532120/4907315
# job 'result': possible values are 'success', 'failure', 'cancelled', or 'skipped'
if: >-
${{
contains(needs.*.result, 'failure')
|| contains(needs.*.result, 'cancelled')
}}