From 30037eb959e8ea4f9c91f63b5ff3a16c5d472582 Mon Sep 17 00:00:00 2001 From: Adel Bensaad Date: Mon, 27 May 2024 07:45:18 +0100 Subject: [PATCH] chore: enhance CI with conditional e2e tests recording in Cypress Cloud and parallel execution in GitHub Actions chore: fix workflow and enable yarn caching --- .github/workflows/dhis2-verify-app.yml | 57 ++++++++++++++++++++++---- README.md | 9 ++++ cypress/support/generateTestMatrix.js | 35 ++++++++++++++++ 3 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 cypress/support/generateTestMatrix.js diff --git a/.github/workflows/dhis2-verify-app.yml b/.github/workflows/dhis2-verify-app.yml index 5e8b5f20b..7931c7bca 100644 --- a/.github/workflows/dhis2-verify-app.yml +++ b/.github/workflows/dhis2-verify-app.yml @@ -10,7 +10,10 @@ name: 'dhis2: verify (app)' # 'jobs.release.steps.*.cwd' # 'jobs.release.steps.*.build-dir' -on: push +on: + push: + pull_request: + types: [labeled] concurrency: group: ${{ github.workflow}}-${{ github.ref }} @@ -23,6 +26,18 @@ env: D2_VERBOSE: true jobs: + setup-matrix: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.specs }} + steps: + - uses: actions/checkout@v3 + - name: Generate test matrix + id: set-matrix + run: | + node cypress/support/generateTestMatrix.js > matrix.json + echo "::set-output name=specs::$(cat matrix.json)" + build: runs-on: ubuntu-latest steps: @@ -90,22 +105,42 @@ jobs: e2e: runs-on: ubuntu-latest if: "!github.event.push.repository.fork && github.actor != 'dependabot[bot]'" - - env: - CI_BUILD_ID: ${{ github.sha }}-${{ github.workflow }}-${{ github.event_name }} + needs: [build, lint, test, setup-matrix] strategy: fail-fast: false matrix: - containers: [1, 2, 3, 4] + spec-group: ${{ fromJson(needs.setup-matrix.outputs.matrix) }} + + env: + SHOULD_RECORD: ${{ contains(github.event.head_commit.message, '[e2e record]') || contains(join(github.event.pull_request.labels.*.name), 'e2e record') }} + CI_BUILD_ID: ${{ github.sha }}-${{ github.workflow }}-${{ github.event_name }} steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - - uses: actions/setup-node@v1 + - name: Set up Node.js + uses: actions/setup-node@v3 with: node-version: 14.x + cache: 'yarn' + + - name: Set Cypress Record Environment Variables + if: env.SHOULD_RECORD == 'true' + run: | + echo "CYPRESS_GROUP=e2e-${{ matrix.spec-group.id }}" >> $GITHUB_ENV + echo "CYPRESS_TAG=${{ github.event_name }}" >> $GITHUB_ENV + echo "CYPRESS_CI_BUILD_ID=${{ github.run_id }}" >> $GITHUB_ENV + + - name: Debug Environment Variables + run: | + echo "SHOULD_RECORD=${{ env.SHOULD_RECORD }}" + echo "CI Build ID=${{ env.CI_BUILD_ID }}" + echo "Computed Group=${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_GROUP || '' }}" + echo "Computed Tag=${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_TAG || '' }}" + echo "Computed CI Build ID=${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_CI_BUILD_ID || '' }}" + echo "Spec=${{ join(matrix.spec-group.tests, ',') }}" - name: End-to-End tests uses: cypress-io/github-action@v5 @@ -114,8 +149,12 @@ jobs: start: yarn d2-app-scripts start wait-on: 'http://localhost:3000' wait-on-timeout: 300 - record: true - parallel: true + record: ${{ env.SHOULD_RECORD }} + parallel: ${{ env.SHOULD_RECORD }} + group: ${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_GROUP || '' }} + tag: ${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_TAG || '' }} + ci-build-id: ${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_CI_BUILD_ID || '' }} + spec: ${{ join(matrix.spec-group.tests, ',') }} env: BROWSER: none GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index 2d72d82f3..a3c601aa9 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,12 @@ The category model can be fairly confusing, so we've created a few diagrams to h [Models used in form](./docs/category-combo-diagram.png) [Attribute category combo diagram](./docs/attribute-category-combo-diagram.png) + +## Conditional E2E Test Recording + +To record e2e tests in Cypress Cloud, you can use one of the following methods based on your needs: + +- **Commit Message**: Include `[e2e record]` in your commit messages to activate recording. +- **GitHub Labels**: Apply the `e2e record` label to your pull request to trigger recording. + +This setup helps in managing Cypress Cloud credits more efficiently, ensuring recordings are only made when explicitly required. diff --git a/cypress/support/generateTestMatrix.js b/cypress/support/generateTestMatrix.js new file mode 100644 index 000000000..6f5049f3e --- /dev/null +++ b/cypress/support/generateTestMatrix.js @@ -0,0 +1,35 @@ +const fs = require('fs') +const path = require('path') + +const getAllFiles = (dirPath, arrayOfFiles = []) => { + const files = fs.readdirSync(dirPath) + + files.forEach((file) => { + if (fs.statSync(path.join(dirPath, file)).isDirectory()) { + arrayOfFiles = getAllFiles(path.join(dirPath, file), arrayOfFiles) + } else if (path.extname(file) === '.feature') { + arrayOfFiles.push(path.join(dirPath, file)) + } + }) + + return arrayOfFiles +} + +const createGroups = (files, numberOfGroups = 6) => { + const groups = [] + for (let i = 0; i < numberOfGroups; i++) { + groups.push([]) + } + + files.forEach((file, index) => { + groups[index % numberOfGroups].push(file) + }) + + return groups.map((group, index) => ({ id: index + 1, tests: group })) +} + +const cypressSpecsPath = './cypress/e2e' +const specs = getAllFiles(cypressSpecsPath) +const groupedSpecs = createGroups(specs) + +console.log(JSON.stringify(groupedSpecs))