From 72bec28688bb3c0366d7f19889d9d78e7840d023 Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Thu, 27 Jun 2024 14:07:50 -0400 Subject: [PATCH 1/7] E2E workflows --- .github/workflows/ci.yml | 4 +- .github/workflows/create-preview-env.yaml | 76 +++++++++++ .github/workflows/destroy-preview-env.yaml | 43 +++++++ .github/workflows/e2e-tests.yaml | 135 +++++++++++++++++++ .github/workflows/pr-close.yaml | 53 ++++++++ .github/workflows/pr-open.yml | 143 +++++++++++++++++++++ 6 files changed, 453 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/create-preview-env.yaml create mode 100644 .github/workflows/destroy-preview-env.yaml create mode 100644 .github/workflows/e2e-tests.yaml create mode 100644 .github/workflows/pr-close.yaml create mode 100644 .github/workflows/pr-open.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18f4e7c4cc..9bf3fce628 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,7 @@ name: CI -on: [pull_request] +on: +# - pull_request + - workflow_call env: NODE_VERSION: 18.19.0 diff --git a/.github/workflows/create-preview-env.yaml b/.github/workflows/create-preview-env.yaml new file mode 100644 index 0000000000..a84ae14102 --- /dev/null +++ b/.github/workflows/create-preview-env.yaml @@ -0,0 +1,76 @@ +on: + workflow_call: + outputs: + service-url: + value: "${{ jobs.create-preview-env.outputs.service-url }}" + inputs: + env-name: + type: string + description: Environment name + required: true + branch-name: + type: string + description: Branch to use to create preview env + required: true + app-id: + type: string + description: App Name (likely the GH repo) + required: true + aws-region: + type: string + default: us-east-1 + required: false + +jobs: + + create-preview-env: + timeout-minutes: 10 + runs-on: ubuntu-latest + name: Create Preview Env + permissions: + id-token: write + contents: read + pull-requests: write + discussions: write + env: + ENV_NAME: ${{ inputs.env-name }} + AWS_REGION: us-east-1 + steps: + - name: configure aws credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::992382368072:role/cmiml-feature-oidc-github-role + role-session-name: gha-preview-env + aws-region: ${{ inputs.aws-region }} + - name: Create preview branch + run: | + BRANCHES=$(aws amplify list-branches --app-id ${{ inputs.app-id }} --query 'length(branches[?branchName==`${{ inputs.env-name }}`])') + if [ $BRANCHES -eq 0 ]; then + aws amplify create-branch --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --display-name ${{ inputs.env-name }} --stage PULL_REQUEST + fi + sleep 10 + - name: Deploy branch + id: deploy + run: | + JOB_ID=$(aws amplify start-job --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.env-name }} --job-type RELEASE --query 'jobSummary.jobId') + echo "job-id=$JOB_ID" >> "$GITHUB_OUTPUT" + - name: Wait for job + run: | + while true; do + JOB_STATUS=$(aws amplify get-job --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.env-name }} --job-id ${{ steps.deploy.outputs.job-id }} --query 'job.summary.status' --output text) + echo "Current job ${{ steps.deploy.outputs.job-id }} status: $JOB_STATUS" + case "$JOB_STATUS" in + "SUCCEED") + break + ;; + "FAILED" | "CANCELLED") + exit 1 + ;; + *) + echo "Waiting..." + ;; + esac + sleep 2 + done + outputs: + service-url: https://${{ inputs.env-name }}.${{ inputs.app-id }}.amplifyapp.com diff --git a/.github/workflows/destroy-preview-env.yaml b/.github/workflows/destroy-preview-env.yaml new file mode 100644 index 0000000000..8f652e368d --- /dev/null +++ b/.github/workflows/destroy-preview-env.yaml @@ -0,0 +1,43 @@ +name: Destroy preview environment + +on: + workflow_call: + inputs: + env-name: + type: string + description: Environment name (likely the pr number) + required: true + app-id: + type: string + description: Amplify App ID + required: true + aws-region: + type: string + default: us-east-1 + required: false + +jobs: + destroy-preview-env: + runs-on: ubuntu-latest + name: Destroy Preview Env + permissions: + id-token: write + contents: read + env: + AWS_REGION: us-east-1 + steps: + - name: Configure aws credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::992382368072:role/cmiml-feature-oidc-github-role + role-session-name: gha-preview-env + aws-region: ${{ inputs.aws-region }} + - name: Destroy Preview env + run: | + ENV_COUNT=$(aws amplify list-branches --app-id ${{ inputs.app-id }} --query 'length(branches[?branchName==`${{ inputs.env-name }}`])') + if [ $ENV_COUNT -ne 0 ]; then + aws amplify delete-branch --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.env-name }} + else + echo "Environment ${{ inputs.env-name }} does not exist" + exit 1 + fi diff --git a/.github/workflows/e2e-tests.yaml b/.github/workflows/e2e-tests.yaml new file mode 100644 index 0000000000..f894f62681 --- /dev/null +++ b/.github/workflows/e2e-tests.yaml @@ -0,0 +1,135 @@ +name: E2E Tests + +on: + workflow_call: + inputs: + service-url: + type: string + description: Service URL endpoint + required: true + e2e-tests-ref: + type: string + description: Git ref to checkout from TAF repo + required: false + default: dev + outputs: + report-url: + description: URL of the test report + value: ${{ jobs.publish-report.outputs.report-url }} + +env: + AWS_REGION: us-east-1 + +jobs: + run-e2e-tests: + name: Run E2E Test Suite + runs-on: ubuntu-latest + + permissions: + id-token: write + contents: read + pull-requests: write + discussions: write + + steps: + - name: configure aws credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::917902836630:role/cmiml-devops-oidc-github-role + role-session-name: OIDC-GHA-session + aws-region: ${{ env.AWS_REGION }} + - uses: actions/checkout@v4 + name: Checkout + with: + repository: ChildMindInstitute/MindLogger-TAF + # Matching deploy key in TAF repo + ssh-key: ${{ secrets.TAF_PRIVATE_KEY }} + ref: ${{ inputs.e2e-tests-ref }} + - name: Install + run: npm install + - name: Setup Environment + run: | + sed -i 's/WEB_APP_BASE_URL.*//' .env ; + echo 'WEB_APP_BASE_URL=${{ inputs.service-url }}' >> .env + sed -i 's/API_DOMAIN.*//' .env ; + echo 'API_DOMAIN=https://api-dev.cmiml.net' >> .env + - name: Validate Environment + run: cat .env + - name: Get Secrets by Name and by ARN + uses: aws-actions/aws-secretsmanager-get-secrets@v2 + with: + secret-ids: | + taf/dev + parse-json-secrets: true + + - name: Run tests + run: npm run test:ui:regression + continue-on-error: true + id: e2e-tests + + - name: Collect artifacts + uses: actions/upload-artifact@v4 + with: + name: e2e-results + path: test-results/ui + if-no-files-found: error + + - name: Fail if tests failed + if: steps.e2e-tests.outcome != 'success' + uses: actions/github-script@v7 + with: + script: | + core.setFailed('E2E tests failed') + + publish-report: + name: Publish Report + needs: [ run-e2e-tests ] + if: ${{ always() }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + name: Checkout + with: + ref: gh-pages + path: gh-pages + + - name: Fetch results + uses: actions/download-artifact@v4 + with: + name: e2e-results + path: test-results/ui + - name: debug + run: ls + + - name: Build test report + uses: simple-elf/allure-report-action@master + if: always() + with: + # Where allure will write the generated report + allure_report: e2e + # Results dir + allure_results: test-results/ui/report-junit + # Path to folder to be published + allure_history: allure-history + # Path to folder where gh-pages was checked out + gh_pages: gh-pages + # a subfolder + subfolder: e2e + keep_reports: 20 + + - name: Deploy report to Github Pages + if: always() + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: allure-history + + - name: Generate Report URL + if: always() + id: report-url + uses: actions/github-script@v7 + with: + script: | + core.setOutput('report-url', 'https://childmindinstitute.github.io/mindlogger-web-refactor/e2e/${{ github.run_number }}') + outputs: + report-url: ${{ steps.report-url.outputs.report-url }} \ No newline at end of file diff --git a/.github/workflows/pr-close.yaml b/.github/workflows/pr-close.yaml new file mode 100644 index 0000000000..5621010caa --- /dev/null +++ b/.github/workflows/pr-close.yaml @@ -0,0 +1,53 @@ +name: Tear down preview environment for pull request + +on: + pull_request: + types: + - closed + +# Cancel any in progress since we are shutting down +concurrency: + cancel-in-progress: true + group: "preview-env-manage-${{ github.event.number }}" + +env: + APP_NAME: ${{ github.event.repository.name }} + COPILOT_SERVICE: mindlogger-backend + AWS_REGION: us-east-1 + + +jobs: + destroy-preview-env: + name: Destroy preview env + uses: ./.github/workflows/destroy-preview-env.yaml + with: + env-name: "pr-${{ github.event.number }}" + app-id: d2ojy049t6jawn + + comment-destroy-preview-env: + name: Comment on preview env + runs-on: ubuntu-latest + needs: [ destroy-preview-env ] + if: ${{ always() }} + steps: + - name: Comment on PR + uses: thollander/actions-comment-pull-request@v2 + with: + message: | + :arrow_right: Preview environment failed to be destroyed + comment_tag: preview-env + - name: "Send Slack message on failure" + uses: rtCamp/action-slack-notify@v2 + if: ${{ always() && needs.destroy-preview-env.result == 'failure' }} + env: + SLACK_COLOR: failure + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_TEST_RESULTS }} + SLACK_TITLE: | + :rotating_light: Preview Environment Destroy Failed + SLACK_MESSAGE: >- + Failed to destroy preview environment pr-${{ github.event.number }} for ${{ github.repository }} + \n\n + :arrow_right: [Action Run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) + \n\n + :label: [Pull Request](${{ github.event.pull_request.html_url || github.event.head_commit.url }}) + MSG_MINIMAL: true \ No newline at end of file diff --git a/.github/workflows/pr-open.yml b/.github/workflows/pr-open.yml new file mode 100644 index 0000000000..c291599212 --- /dev/null +++ b/.github/workflows/pr-open.yml @@ -0,0 +1,143 @@ +name: Preview and Test PR + +on: + pull_request: + types: + - opened + - reopened + - synchronize + + +# Only run the latest. Cancel old runs in progress. +concurrency: + cancel-in-progress: true + group: "preview-env-manage-${{ github.event.number }}" + + +jobs: + create-preview-env: + uses: ./.github/workflows/create-preview-env.yaml + with: + env-name: "pr-${{ github.event.number }}" + app-id: d2ojy049t6jawn + branch-name: ${{ github.head_ref }} + secrets: inherit + + comment-create-preview-env: + name: Comment on preview env + runs-on: ubuntu-latest + needs: [create-preview-env] + if: ${{ always() }} + steps: + - name: Comment on PR + uses: thollander/actions-comment-pull-request@v2 + with: + message: | + :arrow_right: Preview environment created: [Click Me!](${{ needs.create-preview-env.outputs.service-url }}) + reactions: eyes, rocket + comment_tag: service-url + - name: "Send Slack message on failure" + uses: rtCamp/action-slack-notify@v2 + if: ${{ always() && needs.create-preview-env.result == 'failure' }} + env: + SLACK_COLOR: failure + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_TEST_RESULTS }} + SLACK_TITLE: Preview Environment + SLACK_MESSAGE: Failed to create preview environment + SLACK_ICON: https://github.com/github.png?size=48 + MSG_MINIMAL: actions url + + run-unit-tests: + uses: ./.github/workflows/ci.yml + secrets: inherit + + comment-unit-tests: + name: Comment on unit test outcome + if: ${{ always() }} + runs-on: ubuntu-latest + needs: [run-unit-tests] + steps: +# - name: Post the link to the report +# uses: guibranco/github-status-action-v2@v1 +# with: +# authToken: ${{secrets.PAT_TOKEN}} +# context: 'Unit Test report' +# state: ${{ needs.run-unit-tests.result }} +# sha: ${{ github.event.pull_request.head.sha }} +# target_url: ${{ needs.run-unit-tests.outputs.report-url }} + - name: "Send Slack message on failure" + if: ${{ needs.run-unit-tests.result != 'success' }} + uses: rtCamp/action-slack-notify@v2 + env: + SLACK_COLOR: failure + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_TEST_RESULTS }} + SLACKIFY_MARKDOWN: true + SLACK_TITLE: | + :rotating_light: Unit test suite failed in ${{ github.repository }} + SLACK_MESSAGE: >- + Unit tests for PR-${{ github.event.pull_request.number}} failed + \n\n + :arrow_right: [Action Run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) + \n\n + :label: [Pull Request](${{ github.event.pull_request.html_url || github.event.head_commit.url }}) + SLACK_ICON: https://github.com/github.png?size=48 + MSG_MINIMAL: true + +# TODO Figure out if there can be E2E + run-e2e-tests: + name: Run E2E Test Suite + needs: [ create-preview-env, run-unit-tests ] + if: ${{ always() && needs.run-unit-tests.result == 'success' && needs.create-preview-env.result == 'success' }} + uses: ./.github/workflows/e2e-tests.yaml + secrets: inherit + with: + service-url: ${{ needs.create-preview-env.outputs.service-url }} + + comment-e2e-tests: + name: Comment on test outcome + if: ${{ always() }} + runs-on: ubuntu-latest + needs: [run-e2e-tests] + steps: + - name: "Send Slack message on failure" + if: ${{ needs.run-e2e-tests.result != 'success' }} + uses: rtCamp/action-slack-notify@v2 + env: + SLACK_COLOR: failure + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_TEST_RESULTS }} + SLACKIFY_MARKDOWN: true + SLACK_TITLE: | + :rotating_light: E2E test suite failed in ${{ github.repository }} + SLACK_MESSAGE: >- + E2E tests for PR-${{ github.event.pull_request.number}} failed + \n\n + :arrow_right: [Action Run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) + \n\n + :label: [Pull Request](${{ github.event.pull_request.html_url || github.event.head_commit.url }}) | :chart_with_upwards_trend: [Test Report](${{ needs.run-e2e-tests.outputs.report-url }}) + SLACK_ICON: https://github.com/github.png?size=48 + MSG_MINIMAL: true + - name: Comment on PR with test success + if: ${{ needs.run-e2e-tests.result == 'success' }} + uses: thollander/actions-comment-pull-request@v2 + with: + message: | + :white_check_mark: E2E tests passed! + comment_tag: e2e-results + - name: Comment on PR with test failure + if: ${{ needs.run-e2e-tests.result != 'success' }} + uses: thollander/actions-comment-pull-request@v2 + with: + message: | + :x: E2E tests failed + comment_tag: e2e-results + + - name: Post the link to the report + if: always() + uses: guibranco/github-status-action-v2@v1 + with: + authToken: ${{secrets.GITHUB_TOKEN}} + context: 'E2E Test report' + state: ${{ needs.run-e2e-tests.result }} + sha: ${{ github.event.pull_request.head.sha }} + target_url: ${{ needs.run-e2e-tests.outputs.report-url }} + From 7f0bcd52fc76f6f4f0fd7a1f695b1ef256387e0f Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Thu, 27 Jun 2024 14:33:04 -0400 Subject: [PATCH 2/7] fix branch --- .github/workflows/create-preview-env.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/create-preview-env.yaml b/.github/workflows/create-preview-env.yaml index a84ae14102..d60c9c1c2d 100644 --- a/.github/workflows/create-preview-env.yaml +++ b/.github/workflows/create-preview-env.yaml @@ -48,16 +48,16 @@ jobs: if [ $BRANCHES -eq 0 ]; then aws amplify create-branch --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --display-name ${{ inputs.env-name }} --stage PULL_REQUEST fi - sleep 10 + sleep 20 - name: Deploy branch id: deploy run: | - JOB_ID=$(aws amplify start-job --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.env-name }} --job-type RELEASE --query 'jobSummary.jobId') + JOB_ID=$(aws amplify start-job --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --job-type RELEASE --query 'jobSummary.jobId') echo "job-id=$JOB_ID" >> "$GITHUB_OUTPUT" - name: Wait for job run: | while true; do - JOB_STATUS=$(aws amplify get-job --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.env-name }} --job-id ${{ steps.deploy.outputs.job-id }} --query 'job.summary.status' --output text) + JOB_STATUS=$(aws amplify get-job --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --job-id ${{ steps.deploy.outputs.job-id }} --query 'job.summary.status' --output text) echo "Current job ${{ steps.deploy.outputs.job-id }} status: $JOB_STATUS" case "$JOB_STATUS" in "SUCCEED") From fe6efafacc128f9fd62e2fa3252baa57fd5bf7a7 Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Thu, 27 Jun 2024 14:50:46 -0400 Subject: [PATCH 3/7] fix branch 2 --- .github/workflows/create-preview-env.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/create-preview-env.yaml b/.github/workflows/create-preview-env.yaml index d60c9c1c2d..33a634900c 100644 --- a/.github/workflows/create-preview-env.yaml +++ b/.github/workflows/create-preview-env.yaml @@ -44,7 +44,7 @@ jobs: aws-region: ${{ inputs.aws-region }} - name: Create preview branch run: | - BRANCHES=$(aws amplify list-branches --app-id ${{ inputs.app-id }} --query 'length(branches[?branchName==`${{ inputs.env-name }}`])') + BRANCHES=$(aws amplify list-branches --app-id ${{ inputs.app-id }} --query 'length(branches[?branchName==`${{ inputs.branch-name }}`])') if [ $BRANCHES -eq 0 ]; then aws amplify create-branch --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --display-name ${{ inputs.env-name }} --stage PULL_REQUEST fi From 3812002e9a5804c64c807c109e500a2d27f34c94 Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Fri, 28 Jun 2024 11:17:07 -0400 Subject: [PATCH 4/7] fixing report --- .github/workflows/e2e-tests.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/e2e-tests.yaml b/.github/workflows/e2e-tests.yaml index f894f62681..74ea7b6ad6 100644 --- a/.github/workflows/e2e-tests.yaml +++ b/.github/workflows/e2e-tests.yaml @@ -53,6 +53,7 @@ jobs: echo 'WEB_APP_BASE_URL=${{ inputs.service-url }}' >> .env sed -i 's/API_DOMAIN.*//' .env ; echo 'API_DOMAIN=https://api-dev.cmiml.net' >> .env + echo 'MAILINATOR_TOKEN=${{ secrets.MAILINATOR_TOKEN }}' >> .env - name: Validate Environment run: cat .env - name: Get Secrets by Name and by ARN @@ -98,9 +99,6 @@ jobs: with: name: e2e-results path: test-results/ui - - name: debug - run: ls - - name: Build test report uses: simple-elf/allure-report-action@master if: always() @@ -108,7 +106,7 @@ jobs: # Where allure will write the generated report allure_report: e2e # Results dir - allure_results: test-results/ui/report-junit + allure_results: test-results/ui/allure/allure-results # Path to folder to be published allure_history: allure-history # Path to folder where gh-pages was checked out From 99e20174bf64a74ff7bf49c610f9426eaada6cce Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Fri, 28 Jun 2024 11:23:25 -0400 Subject: [PATCH 5/7] disable autobuild --- .github/workflows/create-preview-env.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/create-preview-env.yaml b/.github/workflows/create-preview-env.yaml index 33a634900c..16bb313af7 100644 --- a/.github/workflows/create-preview-env.yaml +++ b/.github/workflows/create-preview-env.yaml @@ -46,7 +46,7 @@ jobs: run: | BRANCHES=$(aws amplify list-branches --app-id ${{ inputs.app-id }} --query 'length(branches[?branchName==`${{ inputs.branch-name }}`])') if [ $BRANCHES -eq 0 ]; then - aws amplify create-branch --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --display-name ${{ inputs.env-name }} --stage PULL_REQUEST + aws amplify create-branch --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --display-name ${{ inputs.env-name }} --stage PULL_REQUEST --no-enable-auto-build fi sleep 20 - name: Deploy branch From d63896f0d0ff044f83756974ef99f92bf945b157 Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Mon, 1 Jul 2024 10:48:59 -0400 Subject: [PATCH 6/7] nothing --- .github/workflows/pr-open.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/pr-open.yml b/.github/workflows/pr-open.yml index c291599212..6f87423b23 100644 --- a/.github/workflows/pr-open.yml +++ b/.github/workflows/pr-open.yml @@ -13,7 +13,6 @@ concurrency: cancel-in-progress: true group: "preview-env-manage-${{ github.event.number }}" - jobs: create-preview-env: uses: ./.github/workflows/create-preview-env.yaml From 4101f218ad0bb8ae0fdf5a46b3450278b365dc80 Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Mon, 1 Jul 2024 10:50:15 -0400 Subject: [PATCH 7/7] nothing 2 --- .github/workflows/pr-open.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/pr-open.yml b/.github/workflows/pr-open.yml index 6f87423b23..33b2e6816a 100644 --- a/.github/workflows/pr-open.yml +++ b/.github/workflows/pr-open.yml @@ -7,7 +7,6 @@ on: - reopened - synchronize - # Only run the latest. Cancel old runs in progress. concurrency: cancel-in-progress: true