|
1 | 1 | name: "Review apps: on PR change" |
2 | 2 | on: |
3 | 3 | 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 |
7 | 4 | 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-admin-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-admin: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 | | - TF_VERSION=$(< .review_apps/.terraform-version) |
49 | | - printf "TF_VERSION=%s\n" "$TF_VERSION" >> "$GITHUB_OUTPUT" |
50 | 5 |
|
51 | | - - uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0 |
52 | | - with: |
53 | | - terraform_version: ${{steps.terraform-version.outputs.TF_VERSION}} |
| 6 | +concurrency: |
| 7 | + group: "review-apps-pr-${{ github.event.pull_request.number }}" |
| 8 | + cancel-in-progress: false |
54 | 9 |
|
55 | | - - name: Deploy review app |
56 | | - id: deploy |
57 | | - run: | |
58 | | - cd .review_apps/ |
| 10 | +permissions: |
| 11 | + id-token: write |
| 12 | + contents: read |
| 13 | + pull-requests: write |
59 | 14 |
|
60 | | - terraform init -backend-config="key=review-apps/forms-admin/pr-${{github.event.pull_request.number}}.tfstate" |
61 | | -
|
62 | | - terraform apply \ |
63 | | - -var "pull_request_number=${{github.event.pull_request.number}}" \ |
64 | | - -var "forms_admin_container_image=${{env.CONTAINER_IMAGE_URI}}" \ |
65 | | - -no-color \ |
66 | | - -auto-approve |
67 | | - REVIEW_APP_URL=$(terraform output -raw review_app_url) |
68 | | - ECS_CLUSTER_ID=$(terraform output -raw review_app_ecs_cluster_id) |
69 | | - ECS_SERVICE_NAME=$(terraform output -raw review_app_ecs_service_name) |
70 | | - { |
71 | | - printf 'REVIEW_APP_URL=%s\n' "$REVIEW_APP_URL" |
72 | | - printf 'ECS_CLUSTER_ID=%s\n' "$ECS_CLUSTER_ID" |
73 | | - printf 'ECS_SERVICE_NAME=%s\n' "$ECS_SERVICE_NAME" |
74 | | - } >> "$GITHUB_OUTPUT" |
75 | | -
|
76 | | - - name: Wait for AWS ECS deployments to finish |
77 | | - run: | |
78 | | - aws ecs wait services-stable \ |
79 | | - --cluster "${{steps.deploy.outputs.ECS_CLUSTER_ID}}" \ |
80 | | - --services "${{steps.deploy.outputs.ECS_SERVICE_NAME}}" |
81 | | -
|
82 | | - - name: Comment on PR |
83 | | - env: |
84 | | - COMMENT_MARKER: <!-- review apps on pr change --> |
85 | | - GH_TOKEN: ${{ github.token }} |
86 | | - run: | |
87 | | - cat <<EOF > "${{runner.temp}}/pr-comment.md" |
88 | | - :tada: A review copy of this PR has been deployed! You can reach it at: ${{steps.deploy.outputs.REVIEW_APP_URL}} |
89 | | -
|
90 | | - It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready |
91 | | - after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account |
92 | | - to debug, or otherwise ask an infrastructure person. |
93 | | -
|
94 | | - For the sign in details and more information, [see the review apps wiki page](https://github.com/alphagov/forms-team/wiki/Review-apps). |
95 | | -
|
96 | | - $COMMENT_MARKER |
97 | | - EOF |
98 | | -
|
99 | | - 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") |
100 | | - for comment_id in $old_comment_ids; do |
101 | | - gh api -X DELETE "repos/{owner}/{repo}/issues/comments/${comment_id}" |
102 | | - done |
103 | | -
|
104 | | - 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 |
0 commit comments