From 763c819aea0d538a8cb6fb39661cc914b59593ab Mon Sep 17 00:00:00 2001 From: rodelgc Date: Thu, 16 Mar 2023 08:09:58 +0800 Subject: [PATCH] Checkout composite actions --- .../actions/tests/run-api-tests/action.yml | 37 ++++++ .../actions/tests/run-e2e-tests/action.yml | 41 +++++++ .github/actions/tests/run-k6-tests/action.yml | 17 +++ .../setup-local-test-environment/action.yml | 29 +++++ .../tests/slack-alert-on-pr-merge/action.yml | 41 +++++++ .../scripts/compose-slack-message.js | 114 ++++++++++++++++++ .../upload-allure-files-to-bucket/action.yml | 47 ++++++++ 7 files changed, 326 insertions(+) create mode 100644 .github/actions/tests/run-api-tests/action.yml create mode 100644 .github/actions/tests/run-e2e-tests/action.yml create mode 100644 .github/actions/tests/run-k6-tests/action.yml create mode 100644 .github/actions/tests/setup-local-test-environment/action.yml create mode 100644 .github/actions/tests/slack-alert-on-pr-merge/action.yml create mode 100644 .github/actions/tests/slack-alert-on-pr-merge/scripts/compose-slack-message.js create mode 100644 .github/actions/tests/upload-allure-files-to-bucket/action.yml diff --git a/.github/actions/tests/run-api-tests/action.yml b/.github/actions/tests/run-api-tests/action.yml new file mode 100644 index 00000000000..c1dd9383a72 --- /dev/null +++ b/.github/actions/tests/run-api-tests/action.yml @@ -0,0 +1,37 @@ +name: Run API tests +description: Runs the WooCommerce Core API tests and generates Allure report. +permissions: {} + +inputs: + report-name: + description: Name of Allure report to be generated. + required: true + +runs: + using: composite + steps: + - name: Run API tests. + id: run-api-tests + working-directory: plugins/woocommerce + shell: bash + env: + BASE_URL: http://localhost:8086 + USER_KEY: admin + USER_SECRET: password + run: pnpm exec playwright test --config=tests/api-core-tests/playwright.config.js + + - name: Generate Test report. + if: success() || ( failure() && steps.run-api-tests.conclusion == 'failure' ) + working-directory: plugins/woocommerce + shell: bash + run: pnpm exec allure generate --clean ${{ env.ALLURE_RESULTS_DIR }} --output ${{ env.ALLURE_REPORT_DIR }} + + - name: Archive test report + if: success() || ( failure() && steps.run-api-tests.conclusion == 'failure' ) + uses: actions/upload-artifact@v3 + with: + name: ${{ inputs.report-name }} + path: | + ${{ env.ALLURE_RESULTS_DIR }} + ${{ env.ALLURE_REPORT_DIR }} + retention-days: 20 diff --git a/.github/actions/tests/run-e2e-tests/action.yml b/.github/actions/tests/run-e2e-tests/action.yml new file mode 100644 index 00000000000..7c7a974ed53 --- /dev/null +++ b/.github/actions/tests/run-e2e-tests/action.yml @@ -0,0 +1,41 @@ +name: Run E2E tests +description: Runs the WooCommerce Core E2E tests and generates Allure report. +permissions: {} + +inputs: + report-name: + description: Name of Allure report to be generated. + required: true + +runs: + using: composite + steps: + - name: Download and install Chromium browser. + working-directory: plugins/woocommerce + shell: bash + run: pnpm exec playwright install chromium + + - name: Run E2E tests. + id: run-e2e-tests + env: + FORCE_COLOR: 1 + USE_WP_ENV: 1 + working-directory: plugins/woocommerce + shell: bash + run: pnpm exec playwright test --config=tests/e2e-pw/playwright.config.js + + - name: Generate Test report. + if: success() || ( failure() && steps.run-e2e-tests.conclusion == 'failure' ) + working-directory: plugins/woocommerce + shell: bash + run: pnpm exec allure generate --clean ${{ env.ALLURE_RESULTS_DIR }} --output ${{ env.ALLURE_REPORT_DIR }} + + - name: Archive test report + if: success() || ( failure() && steps.run-e2e-tests.conclusion == 'failure' ) + uses: actions/upload-artifact@v3 + with: + name: ${{ inputs.report-name }} + path: | + ${{ env.ALLURE_RESULTS_DIR }} + ${{ env.ALLURE_REPORT_DIR }} + retention-days: 20 diff --git a/.github/actions/tests/run-k6-tests/action.yml b/.github/actions/tests/run-k6-tests/action.yml new file mode 100644 index 00000000000..fbf67c7f9f6 --- /dev/null +++ b/.github/actions/tests/run-k6-tests/action.yml @@ -0,0 +1,17 @@ +name: Run k6 performance tests +description: Runs the WooCommerce Core k6 performance tests. +permissions: {} + +runs: + using: composite + steps: + - name: Install k6 + shell: bash + run: | + curl https://github.com/grafana/k6/releases/download/v0.33.0/k6-v0.33.0-linux-amd64.tar.gz -L | tar xvz --strip-components 1 + + - name: Run k6 performance tests + id: run-k6-tests + shell: bash + run: | + ./k6 run plugins/woocommerce/tests/performance/tests/gh-action-pr-requests.js diff --git a/.github/actions/tests/setup-local-test-environment/action.yml b/.github/actions/tests/setup-local-test-environment/action.yml new file mode 100644 index 00000000000..c5386a32c77 --- /dev/null +++ b/.github/actions/tests/setup-local-test-environment/action.yml @@ -0,0 +1,29 @@ +name: Setup local test environment +description: Set up a wp-env testing environment +permissions: {} + +inputs: + test-type: + required: true + type: choice + options: + - e2e + - api + - k6 + +runs: + using: composite + steps: + - name: Load docker images and start containers for E2E or API tests + if: ( inputs.test-type == 'e2e' ) || ( inputs.test-type == 'api' ) + working-directory: plugins/woocommerce + shell: bash + run: pnpm run env:test + + - name: Load docker images and start containers for k6 performance tests + if: inputs.test-type == 'k6' + working-directory: plugins/woocommerce + shell: bash + run: | + pnpm env:dev --filter=woocommerce + pnpm env:performance-init --filter=woocommerce diff --git a/.github/actions/tests/slack-alert-on-pr-merge/action.yml b/.github/actions/tests/slack-alert-on-pr-merge/action.yml new file mode 100644 index 00000000000..6fbc11ab62a --- /dev/null +++ b/.github/actions/tests/slack-alert-on-pr-merge/action.yml @@ -0,0 +1,41 @@ +name: Send Slack alert on PR merge test failure +description: Send a Slack alert when automated tests failed on trunk after PR merge. +permissions: {} + +inputs: + slack-bot-token: + required: true + channel-id: + required: true + test-type: + required: true + type: choice + options: + - E2E + - API + - k6 + +runs: + using: composite + steps: + - name: Compose Slack message + id: compose-slack-message + uses: actions/github-script@v6 + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + PR_TITLE: ${{ github.event.pull_request.title }} + SHA: ${{ github.event.pull_request.merge_commit_sha }} + TEST_TYPE: ${{ inputs.test-type }} + with: + script: | + const script = require('./.github/actions/tests/slack-alert-on-pr-merge/scripts/compose-slack-message.js') + const slackMessage = script() + core.setOutput('slack-message', slackMessage) + + - name: Send Slack alert + uses: slackapi/slack-github-action@v1.23.0 + env: + SLACK_BOT_TOKEN: ${{ inputs.slack-bot-token }} + with: + channel-id: ${{ inputs.channel-id }} + payload: ${{ steps.compose-slack-message.outputs.slack-message }} diff --git a/.github/actions/tests/slack-alert-on-pr-merge/scripts/compose-slack-message.js b/.github/actions/tests/slack-alert-on-pr-merge/scripts/compose-slack-message.js new file mode 100644 index 00000000000..ca194507547 --- /dev/null +++ b/.github/actions/tests/slack-alert-on-pr-merge/scripts/compose-slack-message.js @@ -0,0 +1,114 @@ +module.exports = () => { + const { + GITHUB_BASE_REF, + GITHUB_RUN_ID, + PR_NUMBER, + PR_TITLE, + SHA, + TEST_TYPE, + } = process.env; + + // Slack message blocks + const blocks = []; + const dividerBlock = { + type: 'divider', + }; + const introBlock = { + type: 'section', + text: { + type: 'mrkdwn', + text: `${ TEST_TYPE } tests failed on \`${ GITHUB_BASE_REF }\` after merging PR `, + }, + }; + const prTitleBlock = { + type: 'header', + text: { + type: 'plain_text', + text: PR_TITLE, + emoji: true, + }, + }; + const prButtonBlock = { + type: 'actions', + elements: [ + { + type: 'button', + text: { + type: 'plain_text', + text: 'View pull request :pr-merged:', + emoji: true, + }, + value: 'view_pr', + url: `https://github.com/woocommerce/woocommerce/pull/${ PR_NUMBER }`, + action_id: 'view-pr', + }, + ], + }; + const mergeCommitBlock = { + type: 'actions', + elements: [ + { + type: 'button', + text: { + type: 'plain_text', + text: `View merge commit ${ SHA.substring( + 0, + 7 + ) } :alphabet-yellow-hash:`, + emoji: true, + }, + value: 'view_commit', + url: `https://github.com/woocommerce/woocommerce/commit/${ SHA }`, + action_id: 'view-commit', + }, + ], + }; + const githubBlock = { + type: 'actions', + elements: [ + { + type: 'button', + text: { + type: 'plain_text', + text: 'View GitHub run log :github:', + emoji: true, + }, + value: 'view_github', + url: `https://github.com/woocommerce/woocommerce/actions/runs/${ GITHUB_RUN_ID }`, + action_id: 'view-github', + }, + ], + }; + const reportBlock = { + type: 'actions', + elements: [ + { + type: 'button', + text: { + type: 'plain_text', + text: 'View test report :colorful-bar-chart:', + emoji: true, + }, + value: 'view_report', + url: `https://woocommerce.github.io/woocommerce-test-reports/pr-merge/${ PR_NUMBER }/${ TEST_TYPE.toLowerCase() }`, + action_id: 'view-report', + }, + ], + }; + + // Assemble blocks + blocks.push( dividerBlock ); + blocks.push( introBlock ); + blocks.push( prTitleBlock ); + blocks.push( prButtonBlock ); + blocks.push( mergeCommitBlock ); + blocks.push( githubBlock ); + + if ( [ 'e2e', 'api' ].includes( TEST_TYPE.toLowerCase() ) ) { + blocks.push( reportBlock ); + } + + blocks.push( dividerBlock ); + + return { blocks }; +}; diff --git a/.github/actions/tests/upload-allure-files-to-bucket/action.yml b/.github/actions/tests/upload-allure-files-to-bucket/action.yml new file mode 100644 index 00000000000..ad745b990e8 --- /dev/null +++ b/.github/actions/tests/upload-allure-files-to-bucket/action.yml @@ -0,0 +1,47 @@ +name: Upload Allure files to bucket +description: Upload Allure files to bucket. +permissions: {} + +inputs: + destination-dir: + description: Directory under the "artifacts" S3 folder to which the Allure files would be uploaded. + required: true + aws-region: + required: true + aws-access-key-id: + required: true + aws-secret-access-key: + required: true + s3-bucket: + required: true + include-allure-results: + dafault: true + +runs: + using: composite + steps: + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1-node16 + with: + aws-region: ${{ inputs.aws-region }} + aws-access-key-id: ${{ inputs.aws-access-key-id }} + aws-secret-access-key: ${{ inputs.aws-secret-access-key }} + + - name: Upload 'allure-results' folder + if: inputs.include-allure-results == true + shell: bash + run: | + echo "Uploading allure-results folder..." + aws s3 sync ${{ env.ALLURE_RESULTS_DIR }} \ + ${{ inputs.s3-bucket }}/artifacts/${{ github.run_id }}/${{ inputs.destination-dir }}/allure-results \ + --quiet + echo "Done" + + - name: Upload 'allure-report' folder + shell: bash + run: | + echo "Uploading allure-report folder..." + aws s3 sync ${{ env.ALLURE_REPORT_DIR }} \ + ${{ inputs.s3-bucket }}/artifacts/${{ github.run_id }}/${{ inputs.destination-dir }}/allure-report \ + --quiet + echo "Done"