|
| 1 | +name: Compare Bundle Size |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Build"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + actions: read |
| 11 | + # To create the comment |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + compare: |
| 16 | + name: Compare Bundle Stats |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Harden Runner |
| 21 | + uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1 |
| 22 | + with: |
| 23 | + egress-policy: audit |
| 24 | + |
| 25 | + - name: Download PR artifact (head) |
| 26 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 27 | + with: |
| 28 | + script: | |
| 29 | + const fs = require('fs'); |
| 30 | + const path = require('path'); |
| 31 | +
|
| 32 | + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 33 | + owner: context.repo.owner, |
| 34 | + repo: context.repo.repo, |
| 35 | + run_id: context.payload.workflow_run.id, |
| 36 | + }); |
| 37 | +
|
| 38 | + const artifact = artifacts.data.artifacts.find(a => a.name === 'webpack-stats'); |
| 39 | +
|
| 40 | + if (!artifact) { |
| 41 | + throw new Error('No artifact found!'); |
| 42 | + } |
| 43 | +
|
| 44 | + const download = await github.rest.actions.downloadArtifact({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + artifact_id: artifact.id, |
| 48 | + archive_format: 'zip', |
| 49 | + }); |
| 50 | +
|
| 51 | + fs.mkdirSync('head-stats', { recursive: true }); |
| 52 | + fs.writeFileSync('head-stats/stats.zip', Buffer.from(download.data)); |
| 53 | +
|
| 54 | + - name: Download main branch artifact (base) |
| 55 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 56 | + with: |
| 57 | + script: | |
| 58 | + const fs = require('fs'); |
| 59 | +
|
| 60 | + const runs = await github.rest.actions.listWorkflowRuns({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + workflow_id: context.payload.workflow_run.workflow_id, |
| 64 | + branch: 'main', |
| 65 | + status: 'completed', |
| 66 | + per_page: 1, |
| 67 | + }); |
| 68 | +
|
| 69 | + if (runs.length < 1) { |
| 70 | + throw new Error('No runs found!'); |
| 71 | + } |
| 72 | +
|
| 73 | + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 74 | + owner: context.repo.owner, |
| 75 | + repo: context.repo.repo, |
| 76 | + run_id: runs.data.workflow_runs[0].id, |
| 77 | + }); |
| 78 | +
|
| 79 | + const artifact = artifacts.data.artifacts.find(a => a.name === 'webpack-stats'); |
| 80 | +
|
| 81 | + if (!artifact) { |
| 82 | + throw new Error('No artifact found!'); |
| 83 | + } |
| 84 | +
|
| 85 | + const download = await github.rest.actions.downloadArtifact({ |
| 86 | + owner: context.repo.owner, |
| 87 | + repo: context.repo.repo, |
| 88 | + artifact_id: artifact.id, |
| 89 | + archive_format: 'zip', |
| 90 | + }); |
| 91 | +
|
| 92 | + fs.mkdirSync('base-stats', { recursive: true }); |
| 93 | + fs.writeFileSync('base-stats/stats.zip', Buffer.from(download.data)); |
| 94 | +
|
| 95 | + - name: Unzip artifacts |
| 96 | + run: | |
| 97 | + unzip -o head-stats/stats.zip -d head-stats |
| 98 | + unzip -o base-stats/stats.zip -d base-stats |
| 99 | +
|
| 100 | + - uses: github/webpack-bundlesize-compare-action@89161bb25577f08577ce053c3264c0e3b7d7558f # v2.1.0 |
| 101 | + with: |
| 102 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + current-stats-json-path: ./head-stats/webpack-stats.json |
| 104 | + base-stats-json-path: ./base-stats/webpack-stats.json |
0 commit comments