|
| 1 | +name: Playwright Test Preparation |
| 2 | + |
| 3 | +pull_request: |
| 4 | + types: [opened, synchronize, reopened, ready_for_review] |
| 5 | + |
| 6 | +jobs: |
| 7 | + get-changed-files: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + outputs: |
| 10 | + changed-files: ${{ steps.changed_files.outputs.changed-files }} |
| 11 | + steps: |
| 12 | + - name: Get Changed Files |
| 13 | + id: changed_files |
| 14 | + uses: actions/github-script@v6 |
| 15 | + with: |
| 16 | + script: | |
| 17 | + const { context } = require('@actions/github'); |
| 18 | + const fs = require('fs'); |
| 19 | + const changedFiles = fs.readFileSync(context.payload.pull_request.patch_url, 'utf-8') |
| 20 | + .match(/^\+(?:\s*\w+\/)*(\S+\.ts)$/gm) |
| 21 | + .map(line => line.replace('+', '').trim()); |
| 22 | + core.setOutput('changed-files', JSON.stringify(changedFiles)); |
| 23 | +
|
| 24 | + analyze-and-trigger: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + needs: get-changed-files |
| 27 | + steps: |
| 28 | + - name: Call Test Selection API |
| 29 | + id: test_selection |
| 30 | + run: | |
| 31 | + changed_files=${{ steps.changed_files.outputs.changed-files }} |
| 32 | + tests=$(curl -X POST 'https://tests-selection.azurewebsites.net/api' \ |
| 33 | + -H 'Content-Type: application/json' \ |
| 34 | + -d "$changed_files") |
| 35 | + echo "::set-output name=selected-tests::$tests" |
| 36 | +
|
| 37 | + - name: Analyze Test Timings and Determine Shard Count |
| 38 | + uses: actions/github-script@v6 |
| 39 | + run: | |
| 40 | + const testsData = JSON.parse('${{ steps.test_selection.outputs.selected-tests }}'); |
| 41 | + const totalTestDuration = testsData.reduce((sum, test) => sum + test.duration, 0); |
| 42 | + const targetDurationPerShard = 5 * 60; // assuming 5 minutes per shard |
| 43 | + const optimalShardCount = Math.ceil(totalTestDuration / targetDurationPerShard); |
| 44 | + const shardIndices = []; |
| 45 | + for (let i = 1; i <= optimalShardCount; i++) { |
| 46 | + shardIndices.push(i); |
| 47 | + } |
| 48 | + core.setOutput('optimal_shard_count', optimalShardCount); |
| 49 | + core.setOutput('shard_indices', JSON.stringify(shardIndices)); |
| 50 | +
|
| 51 | + - name: Trigger Workflow 2 with Shard Count |
| 52 | + uses: benc-uk/workflow-dispatch@v1 |
| 53 | + with: |
| 54 | + workflow: playwright.yml |
| 55 | + inputs: '{"shardCount": "${{ optimal_shard_count }}"}' |
| 56 | + |
| 57 | + |
| 58 | +name: Run Playwright Tests |
| 59 | + |
| 60 | +on: |
| 61 | + workflow_dispatch: |
| 62 | + inputs: |
| 63 | + shardCount: |
| 64 | + description: 'Number of shards to use' |
| 65 | + required: true |
| 66 | + type: string |
| 67 | + shardIndices: |
| 68 | + description: 'Array of shard indices to execute' |
| 69 | + required: true |
| 70 | + type: string |
| 71 | + |
| 72 | +jobs: |
| 73 | + check-dependencies: |
| 74 | + runs-on: ubuntu-latest |
| 75 | + needs: get-changed-files |
| 76 | + outputs: |
| 77 | + should-install-fresh-deps: ${{ steps.check_deps.outputs.should-install }} |
| 78 | + steps: |
| 79 | + - name: Check if package files changed |
| 80 | + id: check_deps |
| 81 | + run: | |
| 82 | + changed_files=(${CHANGED_FILES}) |
| 83 | + if [[ " ${changed_files[*]} " =~ "package.json" || " ${changed_files[*]} " =~ "package-lock.json" ]]; then |
| 84 | + echo "::set-output name=should-install::true" |
| 85 | + else |
| 86 | + echo "::set-output name=should-install::false" |
| 87 | + fi |
| 88 | +
|
| 89 | + install-and-cache-node-deps: |
| 90 | + runs-on: ubuntu-latest |
| 91 | + needs: check-dependencies |
| 92 | + if: steps.check_deps.outputs.should-install == 'true' |
| 93 | + steps: |
| 94 | + - name: Install dependencies (Vue app) |
| 95 | + working-directory: vue-app |
| 96 | + run: npm ci |
| 97 | + |
| 98 | + - name: Cache Node Dependencies |
| 99 | + uses: actions/cache@v3 |
| 100 | + with: |
| 101 | + path: vue-app/node_modules |
| 102 | + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} |
| 103 | + restore-keys: | |
| 104 | + ${{ runner.os }}-node- |
| 105 | +
|
| 106 | + build-and-start-vue: |
| 107 | + runs-on: ubuntu-latest |
| 108 | + needs: check-dependencies |
| 109 | + if: steps.check_deps.outputs.should-install == 'false' |
| 110 | + steps: |
| 111 | + - name: Use cached Node dependencies |
| 112 | + uses: actions/cache@v3 |
| 113 | + with: |
| 114 | + path: vue-app/node_modules |
| 115 | + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} |
| 116 | + restore-keys: | |
| 117 | + ${{ runner.os }}-node- |
| 118 | +
|
| 119 | + - name: Start Vue app |
| 120 | + run: npm run dev & # Runs in background |
| 121 | + |
| 122 | + - name: Make sure Vue server is up and running |
| 123 | + run: | |
| 124 | + until $(curl --output /dev/null --silent --head --fail http://localhost:3000); do |
| 125 | + printf '.' |
| 126 | + sleep 3 |
| 127 | + done |
| 128 | + echo "Vue server is up and running!" |
| 129 | +
|
| 130 | + playwright-setup: |
| 131 | + runs-on: ubuntu-latest |
| 132 | + steps: |
| 133 | + - name: Cache Playwright Dependencies |
| 134 | + uses: actions/cache@v3 |
| 135 | + with: |
| 136 | + path: ~/.cache/ms-playwright |
| 137 | + key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }} |
| 138 | + restore-keys: | |
| 139 | + ${{ runner.os }}-playwright- |
| 140 | +
|
| 141 | + - name: Install Playwright (if not cached) |
| 142 | + if: steps.cache.outputs.cache-hit != 'true' |
| 143 | + run: npx playwright install --with-deps |
| 144 | + |
| 145 | + run-playwright-tests: |
| 146 | + runs-on: ubuntu-latest |
| 147 | + strategy: |
| 148 | + fail-fast: false |
| 149 | + matrix: |
| 150 | + shardIndex: ${{ fromJson(github.event.inputs.outputs.shard_indices) }} |
| 151 | + shardTotal: [ ${{ github.event.inputs.shardCount }} ] |
| 152 | + timeout-minutes: 10 |
| 153 | + needs: [call-test-selection-api, get-changed-files, playwright-setup, build-and-start-vue] |
| 154 | + env: |
| 155 | + CI: true |
| 156 | + steps: |
| 157 | + - uses: actions/checkout@v3 |
| 158 | + - uses: actions/setup-node@v3 |
| 159 | + with: |
| 160 | + node-version: 18 |
| 161 | + |
| 162 | + - name: Run Playwright Tests |
| 163 | + run: | |
| 164 | + tests=${{ steps.test_selection.outputs.selected-tests }} |
| 165 | + npx playwright test $tests --max-failures=2 --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }} |
0 commit comments