From 877b2e6fa7ce9f418ebd94af55839e461d614782 Mon Sep 17 00:00:00 2001 From: Andrew Weiland Date: Mon, 17 Jun 2024 11:04:34 -0400 Subject: [PATCH] E2E workflows --- .github/workflows/ci.yml | 4 +- .github/workflows/create-preview-env.yaml | 78 ++++++++++++ .github/workflows/e2e-tests.yaml | 131 ++++++++++++++++++++ .github/workflows/pr-open.yml | 142 ++++++++++++++++++++++ 4 files changed, 354 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/create-preview-env.yaml create mode 100644 .github/workflows/e2e-tests.yaml create mode 100644 .github/workflows/pr-open.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03e9828a9..9328aa082 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: 20.11.0 diff --git a/.github/workflows/create-preview-env.yaml b/.github/workflows/create-preview-env.yaml new file mode 100644 index 000000000..e6826d67d --- /dev/null +++ b/.github/workflows/create-preview-env.yaml @@ -0,0 +1,78 @@ +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: + 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 }} + ENV_NAME_SNAKE: ${{ inputs.env-snake-name }} + APP_NAME: ${{ inputs.app-name }} + VPC_ID: ${{ inputs.vpc-id }} + COPILOT_SERVICE: ${{ inputs.copilot-service }} + 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.branch-name }}`])') + if [ "$BRANCHES" == "0" ]; then + aws amplify create-branch --app-id ${{ inputs.app-id }} --branch-name ${{ inputs.branch-name }} --display-name ${{ inputs.env-name }} --stage PULL_REQUEST --enable-auto-build + fi + sleep 10 + - name: Deploy branch + id: deploy + run: | + 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.branch-name }} --job-id ${{ steps.deploy.outputs.job-id }} --query 'job.summary.status') + + case $JOB_STATUS in + SUCCEEDED) + break + ;; + FAILED) + exit 1 + ;; + CANCELLED) + exit 1 + ;; + esac + done + outputs: + service-url: ${{ inputs.env-name }}.${{ inputs.app-id }}.amplifyapp.com diff --git a/.github/workflows/e2e-tests.yaml b/.github/workflows/e2e-tests.yaml new file mode 100644 index 000000000..8cd18205c --- /dev/null +++ b/.github/workflows/e2e-tests.yaml @@ -0,0 +1,131 @@ +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 + - 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:webApp + continue-on-error: true + id: e2e-tests + + - name: Collect artifacts + uses: actions/upload-artifact@v4 + with: + name: e2e-results + path: test-results/api + 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/api + + - 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: allure-results + # 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-backend-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-open.yml b/.github/workflows/pr-open.yml new file mode 100644 index 000000000..cff58e513 --- /dev/null +++ b/.github/workflows/pr-open.yml @@ -0,0 +1,142 @@ +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: d34nh6rsqmrdi2 + 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.yaml + 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 + 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 + + 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 + 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 }} +