generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
328 lines (293 loc) · 14.1 KB
/
reusable_terraform_plan_apply_test.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
---
name: terraform plan apply
# Reusable pipeline for running terraform plan and/or apply on a single
# modernisation platform application environment, e.g. nomis-test.
#
# Constraints:
# - The terraform state must be in a workspace with the same name as the
# application account, e.g. ${application}-${environment}
#
# Features:
# - redacts plan and apply output for use in public repo
# - the apply step sets a deployment environment so a separate approval
# step can be added in the github UI.
# - the apply step is skipped if there is nothing to do in the plan (to
# avoid unnecessary apply approvals)
# - you can optional refresh state as part of the plan (useful if there
# are often AWS changes outside of terraform, and you don't want to
# see this in the plan step)
# - you can optionally post PR plans into the corresponding PR
# - colour output is used unless a PR plan is going to be posted into
# a PR.
on:
workflow_call:
inputs:
application:
type: string
required: true
description: "Name of the application, e.g. nomis"
environment:
type: string
required: true
description: "Name of the environment, e.g. development"
action:
type: string
required: false
description: "Set to plan or plan_apply"
default: plan
terraform_version:
type: string
required: false
description: "The terraform version to use"
default: "~1.5"
plan_apply_tfargs:
type: string
required: false
description: "Any additional terraform arguments to be passed in to terraform plan/apply, e.g. -var 'foo=bar'"
default: ""
do_state_refresh_on_plan:
type: boolean
required: false
description: "Set to true to do a state refresh prior to the plan"
default: false
post_plan_to_pr:
type: boolean
required: false
description: "Set to true to post terraform plan as a comment to the PR"
default: false
secrets:
modernisation_platform_environments:
required: true
pipeline_github_token:
required: true
env:
ACCOUNT_NAME: "${{ inputs.application }}-${{ inputs.environment }}"
WORKSPACE_NAME: "${{ inputs.application }}-${{ inputs.environment }}"
ENVIRONMENT_MANAGEMENT: "${{ secrets.modernisation_platform_environments }}"
GITHUB_TOKEN: "${{ secrets.pipeline_github_token }}"
jobs:
plan:
name: "plan"
runs-on: ubuntu-latest
outputs:
plan_exitcode: "${{ steps.plan.outputs.exitcode }}"
steps:
- name: Debug
run: |
echo "application=${{ inputs.application }}"
echo "environment=${{ inputs.environment }}"
echo "action=${{ inputs.action }}"
echo "init_plan_apply_tfargs=${{ inputs.init_plan_apply_tfargs }}"
echo "plan_apply_tfargs=${{ inputs.plan_apply_tfargs }}"
echo "do_state_refresh_on_plan=${{ inputs.do_state_refresh_on_plan }}"
echo "post_plan_to_pr=${{ inputs.post_plan_to_pr }}"
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Get AWS Account Number
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: "eu-west-2"
- name: Setup Terraform
uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
with:
terraform_version: "${{ inputs.terraform_version }}"
terraform_wrapper: false
- name: Terraform Init
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform --version
echo "terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}"
terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}
- name: Terraform Workspace Select
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform workspace select "${WORKSPACE_NAME}"
- name: Terraform State Refresh (Optional)
if: inputs.do_state_refresh_on_plan == true
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
set -o pipefail
tf_args="${{ inputs.init_plan_apply_tfargs }} ${{ inputs.plan_apply_tfargs }}"
echo "terraform apply -refresh-only -auto-approve ${tf_args}"
terraform apply -refresh-only -auto-approve ${tf_args} | bash ${GITHUB_WORKSPACE}/scripts/redact-output.sh
- name: Terraform Plan
id: plan
env:
POST_PLAN_TO_PR: "${{ github.event_name == 'pull_request' && inputs.post_plan_to_pr == true }}"
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
set -o pipefail
exitcode=0
tf_args="-detailed-exitcode ${{ inputs.init_plan_apply_tfargs }} ${{ inputs.plan_apply_tfargs }}"
[[ ${POST_PLAN_TO_PR} == 'true' ]] && tf_args="${tf_args} -no-color"
[[ ${{ inputs.do_state_refresh_on_plan }} == 'true' ]] && tf_args="${tf_args} -refresh=false"
echo "terraform plan ${tf_args}"
terraform plan ${tf_args} | bash ${GITHUB_WORKSPACE}/scripts/redact-output.sh | tee tfplan.txt || exitcode=$?
echo "exitcode=${exitcode}" # 0=clean plan, 1=error, 2=stuff in plan
echo "exitcode=${exitcode}" >> $GITHUB_OUTPUT
(( exitcode == 1 )) && exit 1 || exit 0
- name: Create Plan PR message (Optional)
if: github.event_name == 'pull_request' && steps.plan.outputs.exitcode == '2' && inputs.post_plan_to_pr == true
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
comment() {
url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
len=$(cat tfplan.txt | wc -c)
echo '**`${{ env.WORKSPACE_NAME }}`** terraform plan on `${{ github.event_name }}` event [#${{ github.run_number }}]('${url}')'
echo
echo '```'
head -c 65476 tfplan.txt | sed -n '/Terraform will perform/,$p'
echo
echo '```'
if [[ $len -gt 65476 ]]; then
echo "** Truncated output. See $url for the rest **"
fi
}
echo 'TF_PLAN_OUT<<EOF' >> $GITHUB_ENV
comment >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Hide Previous PR comment (Optional)
if: ${{ github.event_name == 'pull_request' }}
working-directory: "scripts/minimise-comments"
env:
COMMENT_BODY_CONTAINS: "**`${{ env.WORKSPACE_NAME }}`**"
PR_NUMBER: "${{ github.event.pull_request.number }}"
run: |
go build
./minimise-comments
- name: Post Plan to PR (Optional)
if: github.event_name == 'pull_request' && steps.plan.outputs.exitcode == '2' && inputs.post_plan_to_pr == true
env:
message: "${{ env.TF_PLAN_OUT }}"
run: |
escaped_message=$(echo "$message" | jq -Rsa .)
curl -sS -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ env.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
-d '{"body":'"${escaped_message}"'}'
terratest:
name: "terratest"
needs: plan
if: inputs.action == 'plan_apply' && needs.plan.outputs.plan_exitcode == '2' && inputs.environment == 'development'
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Get AWS Account Number
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: "eu-west-2"
# - name: Run Terratest
# uses: cloudposse/github-action-terratest@main
# with:
# sourceDir: test/src
apply:
name: "apply"
needs: plan
if: inputs.action == 'plan_apply' && needs.plan.outputs.plan_exitcode == '2'
runs-on: ubuntu-latest
environment: "${{ inputs.application }}-${{ inputs.environment }}"
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Get AWS Account Number
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: "eu-west-2"
- name: Setup Terraform
uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
with:
terraform_version: "${{ inputs.terraform_version }}"
terraform_wrapper: false
- name: Terraform Init
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform --version
echo "terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}"
terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}
- name: Terraform Workspace Select
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform workspace select "${WORKSPACE_NAME}"
- name: Terraform Plan
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
set -o pipefail
tf_args="-out x.tfplan ${{ inputs.init_plan_apply_tfargs }} ${{ inputs.plan_apply_tfargs }}"
echo "terraform plan ${tf_args}"
terraform plan ${tf_args} | bash ${GITHUB_WORKSPACE}/scripts/redact-output.sh
- name: Terraform Apply
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
set -o pipefail
tf_args="${{ inputs.init_plan_apply_tfargs }} ${{ inputs.plan_apply_tfargs }} x.tfplan"
echo "terraform apply ${tf_args}"
terraform apply ${tf_args} | bash ${GITHUB_WORKSPACE}/scripts/redact-output.sh
smoketest:
name: "smoketest"
needs: apply
if: inputs.action == 'plan_apply' && needs.plan.outputs.plan_exitcode == '2' && inputs.environment == 'development'
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Get AWS Account Number
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: "eu-west-2"
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Run smoke tests
working-directory: ./terraform/environments/data-platform/tests/
run: |
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \
$(aws sts assume-role \
--role-arn arn:aws:iam::${ACCOUNT_NUMBER}:role/MemberInfrastructureAccess \
--role-session-name smoketesting-data-platform \
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
--output text))
export API_AUTH=$(aws secretsmanager get-secret-value --secret-id data-platform-api-auth-token --query 'SecretString' --output text --no-cli-pager)
pip install -r requirements.txt && python run_smoke_tests.py