generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 2
454 lines (412 loc) · 18.4 KB
/
hmpps.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
---
name: hmpps
# This pipeline works across multiple terragrunt projects within a team
# directory with locking (turnstyle) and PR comment functionality.
#
# The init step figures out which projects to plan/apply
# The plan/apply steps run across each project as a matrix
#
# It allow both plan/apply via manual dispatch.
# For CI/CD, it runs a plan when PR raised and apply when merged.
#
on:
workflow_dispatch:
inputs:
projects:
description: 'One or more project to plan/apply, space separated'
required: true
default: ''
action:
description: 'Set to plan or apply'
required: true
default: 'plan'
pull_request:
types:
- opened
- edited
- synchronize
- reopened
branches:
- main
paths:
- teams/hmpps/**
- modules/imagebuilder/**
- .github/workflows/hmpps.yml
- ansible/**
push:
branches:
- main
paths:
- teams/hmpps/**
- modules/imagebuilder/**
- .github/workflows/hmpps.yml
- ansible/**
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
pull-requests: write # For posting comments to PR
env:
AWS_REGION: "eu-west-2"
ENVIRONMENT_MANAGEMENT: ${{ secrets.MODERNISATION_PLATFORM_ENVIRONMENTS }}
# set branch to main on pull requests so plan changes are same as push-to-main
TF_IN_AUTOMATION: true
TF_VAR_BRANCH_NAME: ${{ github.event_name == 'pull_request' && 'main' || (github.head_ref || github.ref_name) }}
TF_VAR_GH_ACTOR_NAME: ${{ github.actor}}
TF_ENV: production
TEAM_DIR: teams/hmpps
PROJECT_DIR_DEPTH: 3
DEFAULT_TERRAFORM_VERSION: 1.5.7
TERRAGRUNT_VERSION: v0.36.1
defaults:
run:
shell: bash
jobs:
init:
runs-on: ubuntu-latest
outputs:
action: ${{ steps.parseinput.outputs.action }}
projects: ${{ steps.parseinput.outputs.projects }}
pr_number: ${{ steps.parseinput.outputs.pr_number }}
matrix: ${{ steps.setupproject.outputs.matrix }}
steps:
- name: Checkout the code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set Account Number
run: |
ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "::add-mask::$ACCOUNT_NUMBER"
echo ACCOUNT_NUMBER=$ACCOUNT_NUMBER >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
# get PR number: for posting PR comments and figuring out changed files
- name: Get PR number on push
id: get_pr_number
if: ${{ github.event_name == 'push' }}
run: |
pr_number=$(curl -sS \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/pulls \
| jq -r '.[0].number')
if [[ $pr_number == "null" ]]; then
echo "Could not find PR number for commit=${{ github.sha }}"
exit 1
fi
echo "pr=${pr_number}" >> $GITHUB_OUTPUT
# for workflow dispatch, check valid project specified
- name: Validate workflow dispatch
id: projects_workflow_dispatch
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
echo "Validate [projects=${{ github.event.inputs.projects }}] [action=${{ github.event.inputs.action }}]"
set +o pipefail
projects=${{ github.event.inputs.projects }}
if [[ ${projects} == "all" ]]; then
projects=$(find "${TEAM_DIR}" -name terragrunt.hcl | grep -v '/.terra' | sed -r 's|/[^/]+$||' | cut -d/ -f${PROJECT_DIR_DEPTH}- | sort -u)
else
for project in ${{ github.event.inputs.projects }}; do
if [[ ! -e "${TEAM_DIR}/${project}/terragrunt.hcl" ]]; then
echo "Project not found ${TEAM_DIR}/${project}/terragrunt.hcl" >&2
exit 1
fi
done
fi
action=${{ github.event.inputs.action }}
if [[ $action != "plan" && $action != "apply" && $action != "driftcheck" ]]; then
echo "Unexpected value for $action [$action], must be plan/apply/driftcheck" >&2
exit 1
fi
echo "action=[$action]; list of projects" [${projects}]
echo "projects=${projects}" >> $GITHUB_OUTPUT
echo "action=${action}" >> $GITHUB_OUTPUT
- name: Get list of committed files
id: files
if: ${{ github.event_name == 'pull_request' || github.event_name == 'push' }}
run: |
if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
pr_number="${{ github.event.pull_request.number }}"
elif [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then
pr_number="${{ steps.get_pr_number.outputs.pr }}"
else
echo "Unexpected github event name ${GITHUB_EVENT_NAME}"
exit 1
fi
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
pr_files=$(curl -sS \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository }}/pulls/${pr_number}/files \
| jq .[].filename \
| sed 's/"//g' \
| tr '\n' ' ')
echo "file_list=${pr_files}" >> $GITHUB_OUTPUT
- name: Detect pull/push projects
id: projects_pull_push
if: ${{ github.event_name == 'pull_request' || github.event_name == 'push' }}
run: |
updated_files="${{ steps.files.outputs.file_list }}"
echo "Updated files: ${updated_files[@]}"
set +o pipefail
allprojects=$(find ${TEAM_DIR} -name terragrunt.hcl | grep -v '/.terra' | grep -v "${TEAM_DIR}/terragrunt.hcl" | sed -r "s;${TEAM_DIR}/([^/]+)/.+;\1;" | sort -u)
echo "all projects: ${allprojects[@]}"
changed=$(echo "${{ steps.files.outputs.file_list }}" | tr "[:space:]" "\n" | grep -v "${TEAM_DIR}/components/" | sed -r 's|/[^/]+$||' | grep "${TEAM_DIR}/" | sed -r "s;${TEAM_DIR}/([^/]+);\1;g" | sort -u)
workflow_path=.github/workflows/${GITHUB_WORKFLOW}.yml
common_files=( $(ls -p ${TEAM_DIR} | grep -v /) ) # base dir files
common_files=( "${common_files[@]/#/${TEAM_DIR}\/}" ) # prefix with base dir
common_files+=("${workflow_path}") # add workflow to common files
common_files+=("modules/imagebuilder/.+")
echo "common files: ${common_files[@]}"
projects=$(comm -12 <(echo "${changed}") <(echo "${allprojects}"))
for file in ${common_files[@]}; do
if [[ " ${updated_files[*]} " =~ " ${file} " ]]; then
projects=${allprojects[@]}
fi
done
echo List of projects [ ${projects} ]
echo projects=${projects} >> $GITHUB_OUTPUT
- name: Parse inputs
id: parseinput
run: |
echo "Parsing input parameters event=${GITHUB_EVENT_NAME}"
action="plan"
pr_number=""
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
projects="${{ steps.projects_workflow_dispatch.outputs.projects }}"
action="${{ steps.projects_workflow_dispatch.outputs.action }}"
elif [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
projects="${{ steps.projects_pull_push.outputs.projects }}"
pr_number="${{ github.event.pull_request.number }}"
elif [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then
projects="${{ steps.projects_pull_push.outputs.projects }}"
pr_number="${{ steps.get_pr_number.outputs.pr }}"
action="apply"
else
echo "Unsupported event ${GITHUB_EVENT_NAME}"
exit 1
fi
echo "Set action=${action} pr_number=${pr_number} project=${projects}"
echo "projects=${projects}" >> $GITHUB_OUTPUT
echo "action=${action}" >> $GITHUB_OUTPUT
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
- name: Setup project matrix
id: setupproject
env:
projects: ${{ steps.parseinput.outputs.projects }}
run: |
echo "Setup ansible actions for projects [${projects}]"
echo -n "matrix={\"include\":[" >> $GITHUB_OUTPUT
delimiter=""
for project in ${projects}; do
echo -n "${delimiter}" >> $GITHUB_OUTPUT
echo -n '{"project":"'${project}'"}' >> $GITHUB_OUTPUT
delimiter=","
done
echo "]}" >> $GITHUB_OUTPUT
- name: Hide previous PR comments
if: ${{ github.event_name == 'pull_request' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENT_BODY_CONTAINS: "**`${{ env.TEAM_DIR }}/"
PR_NUMBER: "${{ github.event.pull_request.number }}"
run: |
cd ${GITHUB_WORKSPACE}/scripts/minimise-comments
go build
./minimise-comments
plan:
needs: init
if: ${{ needs.init.outputs.projects != '' }}
strategy:
matrix: ${{ fromJson(needs.init.outputs.matrix) }}
fail-fast: false
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set Account Number
run: |
ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "::add-mask::$ACCOUNT_NUMBER"
echo ACCOUNT_NUMBER=$ACCOUNT_NUMBER >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Get terraform version
id: discover
run: |
echo "Get terraform version for ${{ matrix.project }}"
set +o pipefail
required_version=$(grep ^terraform "${TEAM_DIR}/${{ matrix.project }}"/*.tf -A 20 | grep -w required_version | cut -d\" -f2)
if [[ -z "$required_version" ]]; then
echo "Using default terraform version specified in pipeline $DEFAULT_TERRAFORM_VERSION" >&2
required_version="=${DEFAULT_TERRAFORM_VERSION}"
fi
if [[ ! "$required_version" =~ =[0-9]+.[0-9]+.[0-9]+ ]]; then
echo "Unexpected terraform required_version format, expect '=x.y.z': $required_version" >&2
exit 1
fi
version=$(echo "$required_version" | cut -d= -f2)
echo "version=${version}" >> $GITHUB_OUTPUT
- name: Setup Terraform
uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8 # v3.1.1
with:
terraform_version: ${{ steps.discover.outputs.version }}
terraform_wrapper: false
- name: Setup Terragrunt
run: |
wget -O /usr/local/bin/terragrunt https://github.com/gruntwork-io/terragrunt/releases/download/${TERRAGRUNT_VERSION}/terragrunt_linux_amd64
chmod u+x /usr/local/bin/terragrunt
- name: Init
working-directory: "${{ env.TEAM_DIR }}/${{ matrix.project }}"
run: |
terragrunt init
terragrunt workspace select "core-shared-services-${TF_ENV}" || terragrunt workspace new "core-shared-services-${TF_ENV}"
- name: Plan
id: plan
working-directory: "${{ env.TEAM_DIR }}/${{ matrix.project }}"
run: |
exitcode=0
chmod +x ${GITHUB_WORKSPACE}/scripts/redact-output.sh
terragrunt plan -no-color -detailed-exitcode | ${GITHUB_WORKSPACE}/scripts/redact-output.sh | tee tfplan.txt || exitcode=$?
echo "terragrunt plan exit code = $exitcode"
echo "exitcode=${exitcode}" >> $GITHUB_OUTPUT
(( exitcode == 1 )) && exit 1 || exit 0
- name: Create Plan PR message
if: ${{ (github.event_name == 'pull_request' || github.event_name == 'push') && steps.plan.outputs.exitcode == '2' }}
working-directory: "${{ env.TEAM_DIR }}/${{ matrix.project }}"
run: |
comment() {
url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
len=$(cat tfplan.txt | wc -c)
echo '**`${{ env.TEAM_DIR }}/${{ matrix.project }}`** terragrunt plan on `${{ github.event_name }}` event [#${{ github.run_number }}](${url})'
echo
echo '```'
head -c 65476 tfplan.txt
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: Post Plan to PR
env:
message: "${{ env.TF_PLAN_OUT }}"
pr_number: "${{ needs.init.outputs.pr_number }}"
run: |
escaped_message=$(echo "$message" | jq -Rsa .)
curl -sS -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${pr_number}/comments" \
-d '{"body":'"${escaped_message}"'}'
apply:
needs:
- init
- plan
if: ${{ needs.init.outputs.projects != '' && needs.init.outputs.action == 'apply' }}
strategy:
matrix: ${{ fromJson(needs.init.outputs.matrix) }}
fail-fast: false
runs-on: ubuntu-latest
environment:
name: core-shared-services
steps:
- name: Checkout the code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set Account Number
run: |
ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "::add-mask::$ACCOUNT_NUMBER"
echo ACCOUNT_NUMBER=$ACCOUNT_NUMBER >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Get terraform version
id: discover
run: |
echo "Get terraform version for ${{ matrix.project }}"
set +o pipefail
required_version=$(grep ^terraform "${TEAM_DIR}/${{ matrix.project }}"/*.tf -A 20 | grep -w required_version | cut -d\" -f2)
if [[ -z "$required_version" ]]; then
echo "Using default terraform version specified in pipeline $DEFAULT_TERRAFORM_VERSION" >&2
required_version="=${DEFAULT_TERRAFORM_VERSION}"
fi
if [[ ! "$required_version" =~ =[0-9]+.[0-9]+.[0-9]+ ]]; then
echo "Unexpected terraform required_version format, expect '=x.y.z': $required_version" >&2
exit 1
fi
version=$(echo "$required_version" | cut -d= -f2)
echo "version=${version}" >> $GITHUB_OUTPUT
- name: Setup Terraform
uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8 # v3.1.1
with:
terraform_version: ${{ steps.discover.outputs.version }}
terraform_wrapper: false
- name: Setup Terragrunt
run: |
wget -O /usr/local/bin/terragrunt https://github.com/gruntwork-io/terragrunt/releases/download/${TERRAGRUNT_VERSION}/terragrunt_linux_amd64
chmod u+x /usr/local/bin/terragrunt
- name: Init
working-directory: "${{ env.TEAM_DIR }}/${{ matrix.project }}"
run: |
terragrunt init
terragrunt workspace select "core-shared-services-${TF_ENV}" || terragrunt workspace new "core-shared-services-${TF_ENV}"
- name: Plan
id: plan
working-directory: "${{ env.TEAM_DIR }}/${{ matrix.project }}"
run: |
exitcode=0
chmod +x ${GITHUB_WORKSPACE}/scripts/redact-output.sh
terragrunt plan -detailed-exitcode -no-color -out=tf.plan | ${GITHUB_WORKSPACE}/scripts/redact-output.sh || exitcode=$?
echo "terragrunt plan exit code = $exitcode"
echo "exitcode=${exitcode}" >> $GITHUB_OUTPUT
(( exitcode == 1 )) && exit 1 || exit 0
- name: Apply
if: ${{ steps.plan.outputs.exitcode == '2' }}
working-directory: "${{ env.TEAM_DIR }}/${{ matrix.project }}"
run: |
terragrunt apply tf.plan -no-color | ${GITHUB_WORKSPACE}/scripts/redact-output.sh | tee tfapply.txt
- name: Create Apply PR message
if: ${{ github.event_name == 'push' && steps.plan.outputs.exitcode == '2' }}
working-directory: "${{ env.TEAM_DIR }}/${{ matrix.project }}"
run: |
comment() {
url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
len=$(cat tfapply.txt | wc -c)
echo '**`${{ env.TEAM_DIR }}/${{ matrix.project }}`** terraform apply on `${{ github.event_name }}` event [#${{ github.run_number }}](${url})'
echo
echo '```'
head -c 65476 tfapply.txt
echo
echo '```'
if [[ $len -gt 65476 ]]; then
echo "** Truncated output. See $url for the rest **"
fi
}
echo 'TF_APPLY_OUT<<EOF' >> $GITHUB_ENV
comment >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Post Apply to PR
if: ${{ github.event_name == 'push' && steps.plan.outputs.exitcode == '2' }}
env:
message: "${{ env.TF_APPLY_OUT }}"
pr_number: "${{ needs.init.outputs.pr_number }}"
run: |
escaped_message=$(echo "$message" | jq -Rsa .)
curl -sS -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${pr_number}/comments" \
-d '{"body":'"${escaped_message}"'}'