Skip to content

Commit e868b6d

Browse files
committed
Deploy / destroy review apps with CodeBuild
Instead of running Terraform directly in the GitHub Actions runners, we now trigger AWS CodeBuild projects to handle the deployment and destruction of review apps. This means that the repository no longer needs extensive AWS permissions in GitHub Actions, and the actual available AWS operations are limited.
1 parent 776c207 commit e868b6d

2 files changed

Lines changed: 22 additions & 131 deletions

File tree

Lines changed: 11 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +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]
85

96
concurrency:
10-
group: "review-apps-forms-admin-pr-${{ github.event.pull_request.number }}"
7+
group: "review-apps-pr-${{ github.event.pull_request.number }}"
118
cancel-in-progress: false
12-
jobs:
13-
update-review-app:
14-
# this references a codebuild project configured in forms-deploy
15-
# see: https://docs.aws.amazon.com/codebuild/latest/userguide/action-runner.html
16-
runs-on: codebuild-review-forms-admin-gha-runner-${{github.run_id}}-${{github.run_attempt}}
17-
18-
permissions:
19-
pull-requests: write
20-
21-
steps:
22-
- name: Generate container image URI
23-
run: |
24-
echo "CONTAINER_IMAGE_URI=842676007477.dkr.ecr.eu-west-2.amazonaws.com/forms-admin:pr-${{github.event.pull_request.number}}-${{github.event.pull_request.head.sha}}-$(date +%s)" >> "$GITHUB_ENV"
25-
26-
- name: Checkout code
27-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
28-
29-
- name: Build container
30-
run: |
31-
# Docker credentials are configured in CodeBuild
32-
# CodeBuild retrieves the credentials from ParameterStore
33-
echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
34-
docker build \
35-
--tag "${{env.CONTAINER_IMAGE_URI}}" \
36-
.
37-
38-
- name: Push container
39-
id: build-container
40-
run: |
41-
aws ecr get-login-password --region eu-west-2 \
42-
| docker login --username AWS --password-stdin 842676007477.dkr.ecr.eu-west-2.amazonaws.com
43-
44-
echo "Pushing container image"
45-
echo "${{env.CONTAINER_IMAGE_URI}}"
46-
47-
docker push "${CONTAINER_IMAGE_URI}"
48-
49-
- name: Determine Terraform version
50-
id: terraform-version
51-
run: |
52-
TF_VERSION=$(< .review_apps/.terraform-version)
53-
printf "TF_VERSION=%s\n" "$TF_VERSION" >> "$GITHUB_OUTPUT"
54-
55-
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0
56-
with:
57-
terraform_version: ${{steps.terraform-version.outputs.TF_VERSION}}
589

59-
- name: Deploy review app
60-
id: deploy
61-
run: |
62-
cd .review_apps/
10+
permissions:
11+
id-token: write
12+
contents: read
13+
pull-requests: write
6314

64-
terraform init -backend-config="key=review-apps/forms-admin/pr-${{github.event.pull_request.number}}.tfstate"
65-
66-
terraform apply \
67-
-var "pull_request_number=${{github.event.pull_request.number}}" \
68-
-var "forms_admin_container_image=${{env.CONTAINER_IMAGE_URI}}" \
69-
-no-color \
70-
-auto-approve
71-
REVIEW_APP_URL=$(terraform output -raw review_app_url)
72-
ECS_CLUSTER_ID=$(terraform output -raw review_app_ecs_cluster_id)
73-
ECS_SERVICE_NAME=$(terraform output -raw review_app_ecs_service_name)
74-
{
75-
printf 'REVIEW_APP_URL=%s\n' "$REVIEW_APP_URL"
76-
printf 'ECS_CLUSTER_ID=%s\n' "$ECS_CLUSTER_ID"
77-
printf 'ECS_SERVICE_NAME=%s\n' "$ECS_SERVICE_NAME"
78-
} >> "$GITHUB_OUTPUT"
79-
80-
- name: Wait for AWS ECS deployments to finish
81-
run: |
82-
aws ecs wait services-stable \
83-
--cluster "${{steps.deploy.outputs.ECS_CLUSTER_ID}}" \
84-
--services "${{steps.deploy.outputs.ECS_SERVICE_NAME}}"
85-
86-
- name: Comment on PR
87-
env:
88-
COMMENT_MARKER: <!-- review apps on pr change -->
89-
GH_TOKEN: ${{ github.token }}
90-
run: |
91-
cat <<EOF > "${{runner.temp}}/pr-comment.md"
92-
:tada: A review copy of this PR has been deployed! You can reach it at: ${{steps.deploy.outputs.REVIEW_APP_URL}}
93-
94-
It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready
95-
after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account
96-
to debug, or otherwise ask an infrastructure person.
97-
98-
For the sign in details and more information, [see the review apps wiki page](https://github.com/alphagov/forms-team/wiki/Review-apps).
99-
100-
$COMMENT_MARKER
101-
EOF
102-
103-
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")
104-
for comment_id in $old_comment_ids; do
105-
gh api -X DELETE "repos/{owner}/{repo}/issues/comments/${comment_id}"
106-
done
107-
108-
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-admin
Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +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]
65

76
concurrency:
8-
group: "review-apps-forms-admin-pr-${{ github.event.pull_request.number }}"
7+
group: "review-apps-pr-${{ github.event.pull_request.number }}"
98
cancel-in-progress: false
10-
env:
11-
IMAGE_TAG: "842676007477.dkr.ecr.eu-west-2.amazonaws.com/forms-admin:pr-${{github.event.pull_request.number}}-${{github.event.pull_request.head.ref}}"
12-
jobs:
13-
delete-review-app:
14-
# this references a codebuild project configured in forms-deploy
15-
# see: https://docs.aws.amazon.com/codebuild/latest/userguide/action-runner.html
16-
runs-on: codebuild-review-forms-admin-gha-runner-${{github.run_id}}-${{github.run_attempt}}
17-
18-
steps:
19-
- name: Checkout code
20-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
21-
22-
- name: Determine Terraform version
23-
id: terraform-version
24-
run: |
25-
TF_VERSION=$(< .review_apps/.terraform-version)
26-
printf "TF_VERSION=%s\n" "$TF_VERSION" >> "$GITHUB_OUTPUT"
279

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

32-
- name: Delete review app
33-
run: |
34-
cd .review_apps/
35-
36-
terraform init -backend-config="key=review-apps/forms-admin/pr-${{github.event.pull_request.number}}.tfstate"
37-
terraform destroy \
38-
-var "pull_request_number=${{github.event.pull_request.number}}" \
39-
-var "forms_admin_container_image=${{env.IMAGE_TAG}}" \
40-
-no-color \
41-
-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-admin

0 commit comments

Comments
 (0)