Skip to content

Commit b36906b

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 6ec3fa6 commit b36906b

2 files changed

Lines changed: 43 additions & 68 deletions

File tree

.github/workflows/review_apps_on_pr_change.yml

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
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:
107
group: "review-apps-forms-admin-pr-${{ github.event.pull_request.number }}"
118
cancel-in-progress: false
9+
1210
jobs:
1311
update-review-app:
1412
runs-on: ubuntu-24.04-arm
15-
1613
permissions:
1714
id-token: write
1815
contents: read
@@ -24,74 +21,68 @@ jobs:
2421
with:
2522
role-to-assume: arn:aws:iam::842676007477:role/review-github-actions-forms-admin
2623
aws-region: eu-west-2
27-
- name: Generate container image URI
28-
run: |
29-
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"
3024

3125
- name: Checkout code
3226
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
3327

34-
- name: Build container
28+
- name: Generate container image URI
3529
run: |
36-
docker build \
37-
--tag "${{env.CONTAINER_IMAGE_URI}}" \
38-
.
30+
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"
31+
32+
- name: Build container
33+
run: docker build --tag "${{env.CONTAINER_IMAGE_URI}}" .
3934

4035
- name: Push container
41-
id: build-container
4236
run: |
4337
aws ecr get-login-password --region eu-west-2 \
4438
| docker login --username AWS --password-stdin 842676007477.dkr.ecr.eu-west-2.amazonaws.com
45-
46-
echo "Pushing container image"
47-
echo "${{env.CONTAINER_IMAGE_URI}}"
48-
4939
docker push "${CONTAINER_IMAGE_URI}"
5040
51-
- name: Determine Terraform version
52-
id: terraform-version
53-
run: |
54-
TF_VERSION=$(< .review_apps/.terraform-version)
55-
printf "TF_VERSION=%s\n" "$TF_VERSION" >> "$GITHUB_OUTPUT"
56-
57-
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0
41+
- name: Deploy review app via CodeBuild
42+
id: codebuild
43+
uses: aws-actions/aws-codebuild-run-build@4d15a47425739ac2296ba5e7eee3bdd4bfbdd767 # v1.0.18
5844
with:
59-
terraform_version: ${{steps.terraform-version.outputs.TF_VERSION}}
45+
project-name: review-forms-admin-deploy
46+
env-vars-for-codebuild: |
47+
PR_NUMBER,
48+
CONTAINER_IMAGE
49+
env:
50+
PR_NUMBER: ${{ github.event.pull_request.number }}
51+
CONTAINER_IMAGE: ${{ env.CONTAINER_IMAGE_URI }}
6052

61-
- name: Deploy review app
62-
id: deploy
53+
- name: Fetch terraform outputs
54+
id: outputs
6355
run: |
64-
cd .review_apps/
65-
66-
terraform init -backend-config="key=review-apps/forms-admin/pr-${{github.event.pull_request.number}}.tfstate"
67-
68-
terraform apply \
69-
-var "pull_request_number=${{github.event.pull_request.number}}" \
70-
-var "forms_admin_container_image=${{env.CONTAINER_IMAGE_URI}}" \
71-
-no-color \
72-
-auto-approve
73-
REVIEW_APP_URL=$(terraform output -raw review_app_url)
74-
ECS_CLUSTER_ID=$(terraform output -raw review_app_ecs_cluster_id)
75-
ECS_SERVICE_NAME=$(terraform output -raw review_app_ecs_service_name)
56+
# Extract build UUID from ARN (format: arn:aws:codebuild:region:account:build/project:uuid)
57+
BUILD_ID="${{ steps.codebuild.outputs.aws-build-id }}"
58+
BUILD_UUID="${BUILD_ID##*:}"
59+
60+
# Download artifact
61+
aws s3 cp "s3://forms-review-codebuild-artifacts/${BUILD_UUID}/review-forms-admin-deploy/outputs.json" outputs.json
62+
63+
# Parse outputs
7664
{
77-
printf 'REVIEW_APP_URL=%s\n' "$REVIEW_APP_URL"
78-
printf 'ECS_CLUSTER_ID=%s\n' "$ECS_CLUSTER_ID"
79-
printf 'ECS_SERVICE_NAME=%s\n' "$ECS_SERVICE_NAME"
65+
echo "REVIEW_APP_URL=$(jq -r '.review_app_url.value' outputs.json)"
66+
echo "ECS_CLUSTER_ID=$(jq -r '.review_app_ecs_cluster_id.value' outputs.json)"
67+
echo "ECS_SERVICE_NAME=$(jq -r '.review_app_ecs_service_name.value' outputs.json)"
8068
} >> "$GITHUB_OUTPUT"
8169
70+
# Clean up artifact
71+
aws s3 rm "s3://forms-review-codebuild-artifacts/${BUILD_UUID}/review-forms-admin-deploy/outputs.json"
72+
8273
- name: Wait for AWS ECS deployments to finish
8374
run: |
8475
aws ecs wait services-stable \
85-
--cluster "${{steps.deploy.outputs.ECS_CLUSTER_ID}}" \
86-
--services "${{steps.deploy.outputs.ECS_SERVICE_NAME}}"
76+
--cluster "${{ steps.outputs.outputs.ECS_CLUSTER_ID }}" \
77+
--services "${{ steps.outputs.outputs.ECS_SERVICE_NAME }}"
8778
8879
- name: Comment on PR
8980
env:
9081
COMMENT_MARKER: <!-- review apps on pr change -->
9182
GH_TOKEN: ${{ github.token }}
9283
run: |
9384
cat <<EOF > "${{runner.temp}}/pr-comment.md"
94-
:tada: A review copy of this PR has been deployed! You can reach it at: ${{steps.deploy.outputs.REVIEW_APP_URL}}
85+
:tada: A review copy of this PR has been deployed! You can reach it at: ${{steps.outputs.outputs.REVIEW_APP_URL}}
9586
9687
It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready
9788
after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account
Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
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:
87
group: "review-apps-forms-admin-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}}"
9+
1210
jobs:
1311
delete-review-app:
1412
runs-on: ubuntu-24.04-arm
@@ -22,26 +20,12 @@ jobs:
2220
with:
2321
role-to-assume: arn:aws:iam::842676007477:role/review-github-actions-forms-admin
2422
aws-region: eu-west-2
25-
- name: Checkout code
26-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
27-
28-
- name: Determine Terraform version
29-
id: terraform-version
30-
run: |
31-
TF_VERSION=$(< .review_apps/.terraform-version)
32-
printf "TF_VERSION=%s\n" "$TF_VERSION" >> "$GITHUB_OUTPUT"
3323

34-
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0
24+
- name: Destroy review app via CodeBuild
25+
uses: aws-actions/aws-codebuild-run-build@4d15a47425739ac2296ba5e7eee3bdd4bfbdd767 # v1.0.18
3526
with:
36-
terraform_version: ${{steps.terraform-version.outputs.TF_VERSION}}
37-
38-
- name: Delete review app
39-
run: |
40-
cd .review_apps/
41-
42-
terraform init -backend-config="key=review-apps/forms-admin/pr-${{github.event.pull_request.number}}.tfstate"
43-
terraform destroy \
44-
-var "pull_request_number=${{github.event.pull_request.number}}" \
45-
-var "forms_admin_container_image=${{env.IMAGE_TAG}}" \
46-
-no-color \
47-
-auto-approve
27+
project-name: review-forms-admin-destroy
28+
env-vars-for-codebuild: |
29+
PR_NUMBER
30+
env:
31+
PR_NUMBER: ${{ github.event.pull_request.number }}

0 commit comments

Comments
 (0)