From 600135489f3b304538f969347578cdbe9f3f9451 Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 01:46:32 +0100 Subject: [PATCH 01/15] Add a workflow to print bundle size --- .github/workflows/bundle-size.yaml | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 .github/workflows/bundle-size.yaml diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml new file mode 100644 index 00000000..e11176b9 --- /dev/null +++ b/.github/workflows/bundle-size.yaml @@ -0,0 +1,84 @@ +name: Bundle size + +on: + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + name: Build on the base branch and the PR branch + strategy: + matrix: + branch: + - ${{ github.base_ref }} + - ${{ github.ref }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ matrix.branch }} + + - name: Setup + uses: ./.github/actions/setup + + - name: Build the packages + run: turbo run build + + - name: Collect sizes in bytes + id: sizes + run: du -sb packages/*/dist | cut -f 1 > sizes-${{ matrix.branch }}.txt + + - name: Upload the sizes + uses: actions/upload-artifact@v4 + with: + name: sizes-${{ matrix.branch }} + path: sizes-${{ matrix.branch }}.txt + + print: + name: Print the sizes + runs-on: ubuntu-latest + needs: build + steps: + - name: Setup + uses: ./.github/actions/setup + + - name: Download the sizes + uses: actions/download-artifact@v4 + + - name: Create the report + uses: actions/github-script@v7 + with: + result-encoding: string + script: | + function parseDuOutput(output) { + return Object.fromEntries(output.split('\n').map(line => { + const [size, folderName] = line.split(/\s+/); + const [_packages, packageName, _dist] = folderName.split('/'); + return [packageName, parseInt(size, 10)]; + })); + } + function formatSize(sizeInBytes) { + if (sizeInBytes < 1000) { + return `${sizeInBytes} B`; + } else { + const sizeInKb = sizeInBytes / 1000; + return `${sizeInKb.toFixed(2)} KB`; + } + } + const fs = require('fs'); + const sizes = parseDuOutput(fs.readFileSync('sizes-${{ github.base_ref }}.txt', 'utf8')); + const sizesPR = parseDuOutput(fs.readFileSync('sizes-${{ github.ref }}.txt', 'utf8')); + const report = [ + '### 📊 Package size report', + '', + '| Package | Before | After |', + ...Object.keys(sizes).map((packageName) => { + const size = sizes[packageName]; + const sizePR = sizesPR[packageName]; + return `| ${packageName} | ${formatSize(size)} | ${sizePR === size ? 'No change' : formatSize(sizePR)} |`; + }), + ].join('\n'); + core.summary.addRaw(report, /* addEOL: */ true); From 1ab1bf1c157eb7fc7e890354e09879c8cf631139 Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 01:49:05 +0100 Subject: [PATCH 02/15] Use `head_ref` instead of `ref` --- .github/workflows/bundle-size.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index e11176b9..ee9ac5f8 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -14,7 +14,7 @@ jobs: matrix: branch: - ${{ github.base_ref }} - - ${{ github.ref }} + - ${{ github.head_ref }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -70,7 +70,7 @@ jobs: } const fs = require('fs'); const sizes = parseDuOutput(fs.readFileSync('sizes-${{ github.base_ref }}.txt', 'utf8')); - const sizesPR = parseDuOutput(fs.readFileSync('sizes-${{ github.ref }}.txt', 'utf8')); + const sizesPR = parseDuOutput(fs.readFileSync('sizes-${{ github.head_ref }}.txt', 'utf8')); const report = [ '### 📊 Package size report', '', From 82df7dd3d9f1b4053ec7d3522534ffda45d2757a Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 01:51:33 +0100 Subject: [PATCH 03/15] Remove the unnecessary setup step and guard against injections --- .github/workflows/bundle-size.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index ee9ac5f8..cc1cc570 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -42,9 +42,6 @@ jobs: runs-on: ubuntu-latest needs: build steps: - - name: Setup - uses: ./.github/actions/setup - - name: Download the sizes uses: actions/download-artifact@v4 @@ -69,8 +66,8 @@ jobs: } } const fs = require('fs'); - const sizes = parseDuOutput(fs.readFileSync('sizes-${{ github.base_ref }}.txt', 'utf8')); - const sizesPR = parseDuOutput(fs.readFileSync('sizes-${{ github.head_ref }}.txt', 'utf8')); + const sizes = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.base_ref) }}}.txt`, 'utf8')); + const sizesPR = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.head_ref) }}}.txt`, 'utf8')); const report = [ '### 📊 Package size report', '', From 0d977262e5abc2d5afe5ab34bdf18e6f8d79645d Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 01:54:33 +0100 Subject: [PATCH 04/15] Download all artifacts into this folder --- .github/workflows/bundle-size.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index cc1cc570..df8b5a2d 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -44,6 +44,9 @@ jobs: steps: - name: Download the sizes uses: actions/download-artifact@v4 + with: + merge-multiple: true + path: '.' - name: Create the report uses: actions/github-script@v7 From 50e6d0a9e1cc40fb99621b18c43d861945f274c5 Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 01:56:49 +0100 Subject: [PATCH 05/15] Don't cut away the package size --- .github/workflows/bundle-size.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index df8b5a2d..2c402273 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -29,7 +29,7 @@ jobs: - name: Collect sizes in bytes id: sizes - run: du -sb packages/*/dist | cut -f 1 > sizes-${{ matrix.branch }}.txt + run: du -sb packages/*/dist > sizes-${{ matrix.branch }}.txt - name: Upload the sizes uses: actions/upload-artifact@v4 From 5eb2bd080423fbbd48a75c556f0c05871ba25f2c Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 01:59:42 +0100 Subject: [PATCH 06/15] Trim du output --- .github/workflows/bundle-size.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index 2c402273..8a1b60c7 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -54,7 +54,7 @@ jobs: result-encoding: string script: | function parseDuOutput(output) { - return Object.fromEntries(output.split('\n').map(line => { + return Object.fromEntries(output.trim().split('\n').map(line => { const [size, folderName] = line.split(/\s+/); const [_packages, packageName, _dist] = folderName.split('/'); return [packageName, parseInt(size, 10)]; From 1da13ac36a597fc7eae23d860eab1a13a8b3f9fa Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 02:05:23 +0100 Subject: [PATCH 07/15] Write out the summary --- .github/workflows/bundle-size.yaml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index 8a1b60c7..5d26b727 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -71,14 +71,14 @@ jobs: const fs = require('fs'); const sizes = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.base_ref) }}}.txt`, 'utf8')); const sizesPR = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.head_ref) }}}.txt`, 'utf8')); - const report = [ - '### 📊 Package size report', - '', - '| Package | Before | After |', - ...Object.keys(sizes).map((packageName) => { - const size = sizes[packageName]; - const sizePR = sizesPR[packageName]; - return `| ${packageName} | ${formatSize(size)} | ${sizePR === size ? 'No change' : formatSize(sizePR)} |`; - }), - ].join('\n'); - core.summary.addRaw(report, /* addEOL: */ true); + core.summary.addHeading('📊 Package size report', '3'); + core.summary.addTable([ + { data: 'Package', header: true }, + { data: 'Before', header: true }, + { data: 'After', header: true }, + ].concat(Object.keys(sizes).flatMap((packageName) => { + const size = sizes[packageName]; + const sizePR = sizesPR[packageName]; + return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; + })); + core.summary.write(); From ce4cd6af96f4ac3aef54e192ee5d2cf2ed1037f5 Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 02:07:28 +0100 Subject: [PATCH 08/15] ))) --- .github/workflows/bundle-size.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index 5d26b727..61ee5bbc 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -80,5 +80,5 @@ jobs: const size = sizes[packageName]; const sizePR = sizesPR[packageName]; return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; - })); + }))); core.summary.write(); From 86b711067f330b324191ed95c36030ca2f6bac2f Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 02:11:13 +0100 Subject: [PATCH 09/15] wtf --- .github/workflows/bundle-size.yaml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index 61ee5bbc..feee8c93 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -73,12 +73,11 @@ jobs: const sizesPR = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.head_ref) }}}.txt`, 'utf8')); core.summary.addHeading('📊 Package size report', '3'); core.summary.addTable([ - { data: 'Package', header: true }, - { data: 'Before', header: true }, - { data: 'After', header: true }, - ].concat(Object.keys(sizes).flatMap((packageName) => { - const size = sizes[packageName]; - const sizePR = sizesPR[packageName]; - return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; - }))); + ...(['Package', 'Before', 'After'].map((data) => ({ data, header: true }))), + ...Object.keys(sizes).flatMap((packageName) => { + const size = sizes[packageName]; + const sizePR = sizesPR[packageName]; + return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; + }).map((data) => ({ data })), + ); core.summary.write(); From 2f2baad8522087b209d46affab0ebb7234d594a9 Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 02:12:30 +0100 Subject: [PATCH 10/15] ]]] --- .github/workflows/bundle-size.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index feee8c93..74a8649a 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -79,5 +79,5 @@ jobs: const sizePR = sizesPR[packageName]; return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; }).map((data) => ({ data })), - ); + ]); core.summary.write(); From 4cf33b04d87cc39588603bbf1a1eb678ef534b4f Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 02:16:54 +0100 Subject: [PATCH 11/15] thanks for nothing docs --- .github/workflows/bundle-size.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index 74a8649a..65fbf451 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -73,11 +73,11 @@ jobs: const sizesPR = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.head_ref) }}}.txt`, 'utf8')); core.summary.addHeading('📊 Package size report', '3'); core.summary.addTable([ - ...(['Package', 'Before', 'After'].map((data) => ({ data, header: true }))), - ...Object.keys(sizes).flatMap((packageName) => { + (['Package', 'Before', 'After'].map((data) => ({ data, header: true }))), + ...Object.keys(sizes).map((packageName) => { const size = sizes[packageName]; const sizePR = sizesPR[packageName]; return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; - }).map((data) => ({ data })), + }), ]); core.summary.write(); From 508cdce386f45cca6f20092582ae3f3cd08a27a7 Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Tue, 17 Dec 2024 02:17:19 +0100 Subject: [PATCH 12/15] easy on the brackets --- .github/workflows/bundle-size.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size.yaml index 65fbf451..6ee0ba25 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size.yaml @@ -73,7 +73,7 @@ jobs: const sizesPR = parseDuOutput(fs.readFileSync(`sizes-${${{ toJson(github.head_ref) }}}.txt`, 'utf8')); core.summary.addHeading('📊 Package size report', '3'); core.summary.addTable([ - (['Package', 'Before', 'After'].map((data) => ({ data, header: true }))), + ['Package', 'Before', 'After'].map((data) => ({ data, header: true })), ...Object.keys(sizes).map((packageName) => { const size = sizes[packageName]; const sizePR = sizesPR[packageName]; From 3e81d81fc638263d66fa5bd557622a30ad7c199b Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Thu, 19 Dec 2024 23:23:37 +0100 Subject: [PATCH 13/15] Split the workflow into two --- ...dle-size.yaml => bundle-size-trusted.yaml} | 58 ++++++++----------- .github/workflows/bundle-size-untrusted.yaml | 44 ++++++++++++++ 2 files changed, 67 insertions(+), 35 deletions(-) rename .github/workflows/{bundle-size.yaml => bundle-size-trusted.yaml} (68%) create mode 100644 .github/workflows/bundle-size-untrusted.yaml diff --git a/.github/workflows/bundle-size.yaml b/.github/workflows/bundle-size-trusted.yaml similarity index 68% rename from .github/workflows/bundle-size.yaml rename to .github/workflows/bundle-size-trusted.yaml index 6ee0ba25..5e0c89c4 100644 --- a/.github/workflows/bundle-size.yaml +++ b/.github/workflows/bundle-size-trusted.yaml @@ -1,54 +1,32 @@ -name: Bundle size +# This is the trusted counterpart of the Bundle size workflow. +# It generates a report and posts a comment on the PR. + +name: Bundle size (trusted) on: - pull_request: + workflow_run: + workflows: [Bundle size] + types: + - completed -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true +permissions: + pull-requests: write jobs: - build: - name: Build on the base branch and the PR branch - strategy: - matrix: - branch: - - ${{ github.base_ref }} - - ${{ github.head_ref }} - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - ref: ${{ matrix.branch }} - - - name: Setup - uses: ./.github/actions/setup - - - name: Build the packages - run: turbo run build - - - name: Collect sizes in bytes - id: sizes - run: du -sb packages/*/dist > sizes-${{ matrix.branch }}.txt - - - name: Upload the sizes - uses: actions/upload-artifact@v4 - with: - name: sizes-${{ matrix.branch }} - path: sizes-${{ matrix.branch }}.txt - print: name: Print the sizes runs-on: ubuntu-latest - needs: build steps: - name: Download the sizes uses: actions/download-artifact@v4 with: merge-multiple: true path: '.' + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} - name: Create the report + id: create-report uses: actions/github-script@v7 with: result-encoding: string @@ -80,4 +58,14 @@ jobs: return [packageName, formatSize(size), sizePR === size ? 'No change' : formatSize(sizePR)]; }), ]); + const report = core.summary.stringify(); core.summary.write(); + return report; + + - name: Post the report to the PR + uses: thollander/actions-comment-pull-request@v3 + with: + message: ${{ steps.create-report.outputs.result }} + pr-number: ${{ github.event.workflow_run.pull_requests.number }} + comment-tag: bundle-size + mode: recreate diff --git a/.github/workflows/bundle-size-untrusted.yaml b/.github/workflows/bundle-size-untrusted.yaml new file mode 100644 index 00000000..8cbddee2 --- /dev/null +++ b/.github/workflows/bundle-size-untrusted.yaml @@ -0,0 +1,44 @@ +# This workflow builds the packages on the base branch and the PR branch, and then records the sizes of the built packages. +# The size comparison is done in a separate workflow because this one can't leave PR comments. + +name: Bundle size + +on: + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + build: + name: Build on the base branch and the PR branch + strategy: + matrix: + branch: + - ${{ github.base_ref }} + - ${{ github.head_ref }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ matrix.branch }} + + - name: Setup + uses: ./.github/actions/setup + + - name: Build the packages + run: turbo run build + + - name: Collect sizes in bytes + id: sizes + run: du -sb packages/*/dist > sizes-${{ matrix.branch }}.txt + + - name: Upload the sizes + uses: actions/upload-artifact@v4 + with: + name: sizes-${{ matrix.branch }} + path: sizes-${{ matrix.branch }}.txt From 5d5687a9dd9cc4b4cf40c01a2bc05c1d8d9495dc Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Thu, 19 Dec 2024 23:26:18 +0100 Subject: [PATCH 14/15] Pin a hash instead of a version --- .github/workflows/bundle-size-trusted.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bundle-size-trusted.yaml b/.github/workflows/bundle-size-trusted.yaml index 5e0c89c4..9ab2b1de 100644 --- a/.github/workflows/bundle-size-trusted.yaml +++ b/.github/workflows/bundle-size-trusted.yaml @@ -63,7 +63,7 @@ jobs: return report; - name: Post the report to the PR - uses: thollander/actions-comment-pull-request@v3 + uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 with: message: ${{ steps.create-report.outputs.result }} pr-number: ${{ github.event.workflow_run.pull_requests.number }} From 5adf5027bc301f1a5f8f6f0e66f802e5e4c887ad Mon Sep 17 00:00:00 2001 From: Lev Chelyadinov Date: Fri, 20 Dec 2024 12:34:40 +0100 Subject: [PATCH 15/15] Use the `upsert` mode --- .github/workflows/bundle-size-trusted.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/bundle-size-trusted.yaml b/.github/workflows/bundle-size-trusted.yaml index 9ab2b1de..8ff1e313 100644 --- a/.github/workflows/bundle-size-trusted.yaml +++ b/.github/workflows/bundle-size-trusted.yaml @@ -68,4 +68,3 @@ jobs: message: ${{ steps.create-report.outputs.result }} pr-number: ${{ github.event.workflow_run.pull_requests.number }} comment-tag: bundle-size - mode: recreate