generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 2
335 lines (297 loc) · 12.8 KB
/
components.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
---
name: components
on:
workflow_dispatch:
inputs:
action:
description: 'Set to plan or apply'
required: true
default: 'plan'
pull_request:
types:
- opened
- edited
- synchronize
- reopened
branches:
- main
paths:
- commonimages/components/**
- .github/workflows/components.yml
push:
branches:
- main
paths:
- commonimages/components/**
- .github/workflows/components.yml
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
BASE_DIR: commonimages/components
DEFAULT_TERRAFORM_VERSION: 1.5.7
defaults:
run:
shell: bash
jobs:
init:
runs-on: ubuntu-latest
outputs:
action: ${{ steps.parseinput.outputs.action }}
pr_number: ${{ steps.parseinput.outputs.pr_number }}
steps:
- name: Checkout the code
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $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
- name: Validate workflow dispatch
id: projects_workflow_dispatch
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
echo "Validate [action=${{ github.event.inputs.action }}]"
set +o pipefail
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}" >> $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
action="${{ steps.projects_workflow_dispatch.outputs.action }}"
elif [[ "${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 }}"
action="apply"
else
echo "Unsupported event ${GITHUB_EVENT_NAME}"
exit 1
fi
echo "Set action=${action} pr_number=${pr_number}"
echo "action=${action}" >> $GITHUB_OUTPUT
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
- name: Hide previous PR comments
if: ${{ github.event_name == 'pull_request' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENT_BODY_CONTAINS: "**`${{ env.BASE_DIR }}"
PR_NUMBER: "${{ github.event.pull_request.number }}"
run: |
cd ${GITHUB_WORKSPACE}/scripts/minimise-comments
go build
./minimise-comments
plan:
needs: init
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $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"
set +o pipefail
required_version=$(grep ^terraform "${BASE_DIR}"/*.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: Init
working-directory: "${{ env.BASE_DIR }}"
run: |
terraform init
terraform workspace select "core-shared-services-${TF_ENV}" || terraform workspace new "core-shared-services-${TF_ENV}"
- name: Plan
id: plan
working-directory: "${{ env.BASE_DIR }}"
run: |
exitcode=0
chmod +x ${GITHUB_WORKSPACE}/scripts/redact-output.sh
terraform plan -detailed-exitcode -no-color -out=tf.plan | ${GITHUB_WORKSPACE}/scripts/redact-output.sh | tee tfplan.txt || exitcode=$?
echo "terraform 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.BASE_DIR }}"
run: |
comment() {
url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
len=$(cat tfplan.txt | wc -c)
echo '**`${{ env.BASE_DIR }}`** terraform 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.action == 'apply' }}
runs-on: ubuntu-latest
environment:
name: core-shared-services
steps:
- name: Checkout the code
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $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"
set +o pipefail
required_version=$(grep ^terraform "${BASE_DIR}"/*.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: Init
working-directory: "${{ env.BASE_DIR }}"
run: |
terraform init
terraform workspace select "core-shared-services-${TF_ENV}" || terraform workspace new "core-shared-services-${TF_ENV}"
- name: Abandon old image builder components in state
working-directory: "${{ env.BASE_DIR }}/${{ matrix.project }}"
run: |
components_to_abandon=( $(terraform plan -no-color | grep '# aws_imagebuilder_component\.this\["[^"]*"] must be replaced' | sed 's/# \(.*\) must be replaced/\1/g') ) || components_to_abandon=()
for comp in "${components_to_abandon[@]}" ; do
terraform state rm $comp
done
- name: Plan
id: plan
working-directory: "${{ env.BASE_DIR }}"
run: |
exitcode=0
chmod +x ${GITHUB_WORKSPACE}/scripts/redact-output.sh
terraform plan -detailed-exitcode -no-color -out=tf.plan | ${GITHUB_WORKSPACE}/scripts/redact-output.sh || exitcode=$?
echo "terraform 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.BASE_DIR }}"
run: |
${GITHUB_WORKSPACE}/scripts/terraform-apply.sh . tf.plan | tee tfapply.txt
- name: Create Apply PR message
if: ${{ github.event_name == 'push' && steps.plan.outputs.exitcode == '2' }}
working-directory: "${{ env.BASE_DIR }}"
run: |
comment() {
url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
len=$(cat tfapply.txt | wc -c)
echo '**`${{ env.BASE_DIR }}`** 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}"'}'