Skip to content

Commit 74cc489

Browse files
authored
Merge pull request #1854 from alphagov/whi-tw/stop-using-self-hosted-runner
Use AWS credentials to deploy review apps
2 parents 1c18dd3 + 5db180e commit 74cc489

File tree

3 files changed

+76
-139
lines changed

3 files changed

+76
-139
lines changed
Lines changed: 13 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,20 @@
11
name: "Review apps: on PR change"
22
on:
33
pull_request:
4-
# being explicit about what to trigger on.
5-
# matches the docs for the default types
6-
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request
74
types: [opened, reopened, synchronize]
8-
jobs:
9-
update-review-app:
10-
# this references a codebuild project configured in forms-deploy
11-
# see: https://docs.aws.amazon.com/codebuild/latest/userguide/action-runner.html
12-
runs-on: codebuild-review-forms-runner-gha-runner-${{github.run_id}}-${{github.run_attempt}}
13-
14-
permissions:
15-
pull-requests: write
16-
17-
steps:
18-
- name: Generate container image URI
19-
run: |
20-
echo "CONTAINER_IMAGE_URI=842676007477.dkr.ecr.eu-west-2.amazonaws.com/forms-runner:pr-${{github.event.pull_request.number}}-${{github.event.pull_request.head.sha}}-$(date +%s)" >> "$GITHUB_ENV"
21-
22-
- name: Checkout code
23-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
24-
25-
- name: Build container
26-
run: |
27-
# Docker credentials are configured in CodeBuild
28-
# CodeBuild retrieves the credentials from ParameterStore
29-
echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
30-
docker build \
31-
--tag "${{env.CONTAINER_IMAGE_URI}}" \
32-
.
33-
34-
- name: Push container
35-
id: build-container
36-
run: |
37-
aws ecr get-login-password --region eu-west-2 \
38-
| docker login --username AWS --password-stdin 842676007477.dkr.ecr.eu-west-2.amazonaws.com
39-
40-
echo "Pushing container image"
41-
echo "${{env.CONTAINER_IMAGE_URI}}"
42-
43-
docker push "${CONTAINER_IMAGE_URI}"
44-
45-
- name: Determine Terraform version
46-
id: terraform-version
47-
run: |
48-
echo "TF_VERSION=$(< .review_apps/.terraform-version)" >> "$GITHUB_OUTPUT"
49-
50-
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0
51-
with:
52-
terraform_version: ${{steps.terraform-version.outputs.TF_VERSION}}
53-
54-
- name: Deploy review app
55-
id: deploy
56-
run: |
57-
cd .review_apps/
585

59-
terraform init -backend-config="key=review-apps/forms-runner/pr-${{github.event.pull_request.number}}.tfstate"
6+
concurrency:
7+
group: "review-apps-pr-${{ github.event.pull_request.number }}"
8+
cancel-in-progress: false
609

61-
terraform apply \
62-
-var "pull_request_number=${{github.event.pull_request.number}}" \
63-
-var "forms_runner_container_image=${{env.CONTAINER_IMAGE_URI}}" \
64-
-no-color \
65-
-auto-approve
10+
permissions:
11+
id-token: write
12+
contents: read
13+
pull-requests: write
6614

67-
# shellcheck disable=SC2129 # SC2129 is "mainly a stylistic issue" and it breaks our flow
68-
echo "REVIEW_APP_URL=$(terraform output -raw review_app_url)" >> "$GITHUB_OUTPUT"
69-
echo "ADMIN_APP_URL=$(terraform output -raw admin_app_url)" >> "$GITHUB_OUTPUT"
70-
echo "ECS_CLUSTER_ID=$(terraform output -raw review_app_ecs_cluster_id)" >> "$GITHUB_OUTPUT"
71-
echo "ECS_SERVICE_NAME=$(terraform output -raw review_app_ecs_service_name)" >> "$GITHUB_OUTPUT"
72-
73-
- name: Wait for AWS ECS deployments to finish
74-
run: |
75-
aws ecs wait services-stable \
76-
--cluster "${{steps.deploy.outputs.ECS_CLUSTER_ID}}" \
77-
--services "${{steps.deploy.outputs.ECS_SERVICE_NAME}}"
78-
79-
- name: Comment on PR
80-
env:
81-
COMMENT_MARKER: <!-- review apps on pr change -->
82-
GH_TOKEN: ${{ github.token }}
83-
run: |
84-
cat <<EOF > "${{runner.temp}}/pr-comment.md"
85-
:tada: A review copy of this PR has been deployed! It is made of up two components
86-
87-
1. [A review copy of forms-runner](${{steps.deploy.outputs.REVIEW_APP_URL}})
88-
2. [A production copy of forms-admin](${{steps.deploy.outputs.ADMIN_APP_URL}})
89-
90-
> [!IMPORTANT]
91-
> Not all of the functionality of forms-runner is present in review apps.
92-
> Functionality such as sending emails, file upload, and S3 submission types are
93-
> deliberately disabled for the sake of simplifying review apps.
94-
>
95-
> You should use the full dev environment to test the functionality which is disabled here.
96-
97-
It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready
98-
after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account
99-
to debug, or otherwise ask an infrastructure person.
100-
101-
For the sign in details and more information, [see the review apps wiki page](https://github.com/alphagov/forms-team/wiki/Review-apps).
102-
103-
$COMMENT_MARKER
104-
EOF
105-
106-
# shellcheck disable=SC2016
107-
# `jq` uses single-quote characters on Unix shells
108-
old_comment_ids=$(gh api "repos/{owner}/{repo}/issues/${{github.event.pull_request.number}}/comments" --jq 'map(select((.user.login == "github-actions[bot]") and (.body | endswith($ENV.COMMENT_MARKER + "\n")))) | .[].id')
109-
for comment_id in $old_comment_ids; do
110-
gh api -X DELETE "repos/{owner}/{repo}/issues/comments/${comment_id}"
111-
done
112-
113-
gh pr comment "${{github.event.pull_request.html_url}}" --body-file "${{runner.temp}}/pr-comment.md"
15+
jobs:
16+
update-review-app:
17+
name: Update review app
18+
uses: alphagov/forms-deploy/.github/workflows/reusable-review_apps_on_pr_change.yml@main
19+
with:
20+
app-name: forms-runner
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
11
name: "Review apps: on PR close"
22
on:
33
pull_request:
4-
# only run when a PR is closed or merged
54
types: [closed]
6-
env:
7-
IMAGE_TAG: "842676007477.dkr.ecr.eu-west-2.amazonaws.com/forms-runner:pr-${{github.event.pull_request.number}}-${{github.event.pull_request.head.ref}}"
8-
jobs:
9-
delete-review-app:
10-
# this references a codebuild project configured in forms-deploy
11-
# see: https://docs.aws.amazon.com/codebuild/latest/userguide/action-runner.html
12-
runs-on: codebuild-review-forms-runner-gha-runner-${{github.run_id}}-${{github.run_attempt}}
13-
14-
steps:
15-
- name: Checkout code
16-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
175

18-
- name: Determine Terraform version
19-
id: terraform-version
20-
run: |
21-
echo "TF_VERSION=$(< .review_apps/.terraform-version)" >> "$GITHUB_OUTPUT"
6+
concurrency:
7+
group: "review-apps-pr-${{ github.event.pull_request.number }}"
8+
cancel-in-progress: false
229

23-
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0
24-
with:
25-
terraform_version: ${{steps.terraform-version.outputs.TF_VERSION}}
10+
permissions:
11+
id-token: write
12+
contents: read
13+
pull-requests: write
2614

27-
- name: Delete review app
28-
run: |
29-
cd .review_apps/
30-
31-
terraform init -backend-config="key=review-apps/forms-runner/pr-${{github.event.pull_request.number}}.tfstate"
32-
terraform destroy \
33-
-var "pull_request_number=${{github.event.pull_request.number}}" \
34-
-var "forms_runner_container_image=${{env.IMAGE_TAG}}" \
35-
-no-color \
36-
-auto-approve
15+
jobs:
16+
delete-review-app:
17+
name: Delete review app
18+
uses: alphagov/forms-deploy/.github/workflows/reusable-review_apps_on_pr_close.yml@main
19+
with:
20+
app-name: forms-runner

.review_apps/ecs_task_definition.tf

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@ resource "aws_ecs_task_definition" "task" {
9393
portMappings = [
9494
{
9595
containerPort = 3001
96+
hostPort = 3001
9697
protocol = "tcp"
9798
appProtocol = "http"
9899
}
99100
]
100101

102+
mountPoints = []
103+
systemControls = []
104+
volumesFrom = []
105+
101106
logConfiguration = {
102107
logDriver = "awslogs"
103108
options = {
@@ -112,6 +117,7 @@ resource "aws_ecs_task_definition" "task" {
112117
interval = 30
113118
retries = 5
114119
startPeriod = 180
120+
timeout = 5
115121
}
116122

117123
dependsOn = [
@@ -155,11 +161,16 @@ resource "aws_ecs_task_definition" "task" {
155161
portMappings = [
156162
{
157163
containerPort = 3000
164+
hostPort = 3000
158165
protocol = "tcp"
159166
appProtocol = "http"
160167
}
161168
]
162169

170+
mountPoints = []
171+
systemControls = []
172+
volumesFrom = []
173+
163174
logConfiguration = {
164175
logDriver = "awslogs"
165176
options = {
@@ -174,6 +185,7 @@ resource "aws_ecs_task_definition" "task" {
174185
interval = 30
175186
retries = 5
176187
startPeriod = 180
188+
timeout = 5
177189
}
178190

179191
dependsOn = [
@@ -195,7 +207,17 @@ resource "aws_ecs_task_definition" "task" {
195207
command = []
196208
essential = true
197209

198-
portMappings = [{ containerPort = 5432 }]
210+
portMappings = [
211+
{
212+
containerPort = 5432
213+
hostPort = 5432
214+
protocol = "tcp"
215+
}
216+
]
217+
218+
mountPoints = []
219+
systemControls = []
220+
volumesFrom = []
199221

200222
environment = [
201223
{ name = "POSTGRES_PASSWORD", value = "postgres" }
@@ -211,7 +233,10 @@ resource "aws_ecs_task_definition" "task" {
211233
}
212234

213235
healthCheck = {
214-
command = ["CMD-SHELL", "psql -h localhost -p 5432 -U postgres -c \"SELECT current_timestamp - pg_postmaster_start_time();\""]
236+
command = ["CMD-SHELL", "psql -h localhost -p 5432 -U postgres -c \"SELECT current_timestamp - pg_postmaster_start_time();\""]
237+
interval = 30
238+
retries = 3
239+
timeout = 5
215240
}
216241
},
217242

@@ -226,7 +251,17 @@ resource "aws_ecs_task_definition" "task" {
226251
],
227252
essential = true
228253

229-
portMappings = [{ containerPort = 6379 }]
254+
portMappings = [
255+
{
256+
containerPort = 6379
257+
hostPort = 6379
258+
protocol = "tcp"
259+
}
260+
]
261+
262+
mountPoints = []
263+
systemControls = []
264+
volumesFrom = []
230265

231266
logConfiguration = {
232267
logDriver = "awslogs"
@@ -238,7 +273,10 @@ resource "aws_ecs_task_definition" "task" {
238273
}
239274

240275
healthCheck = {
241-
command = ["CMD-SHELL", "redis-cli", "ping"]
276+
command = ["CMD-SHELL", "redis-cli", "ping"]
277+
interval = 30
278+
retries = 3
279+
timeout = 5
242280
}
243281
},
244282

@@ -251,6 +289,10 @@ resource "aws_ecs_task_definition" "task" {
251289
environment = local.forms_runner_env_vars
252290
readonlyRootFilesystem = true
253291

292+
mountPoints = []
293+
systemControls = []
294+
volumesFrom = []
295+
254296
logConfiguration = {
255297
logDriver = "awslogs"
256298
options = {
@@ -277,6 +319,10 @@ resource "aws_ecs_task_definition" "task" {
277319
environment = local.forms_admin_env_vars
278320
readonlyRootFilesystem = true
279321

322+
mountPoints = []
323+
systemControls = []
324+
volumesFrom = []
325+
280326
logConfiguration = {
281327
logDriver = "awslogs"
282328
options = {

0 commit comments

Comments
 (0)