Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a workflow to print bundle size #150

Merged
merged 16 commits into from
Dec 25, 2024
70 changes: 70 additions & 0 deletions .github/workflows/bundle-size-trusted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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:
workflow_run:
workflows: [Bundle size]
types:
- completed

permissions:
pull-requests: write

jobs:
print:
name: Print the sizes
runs-on: ubuntu-latest
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
script: |
function parseDuOutput(output) {
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)];
}));
}
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-${${{ toJson(github.base_ref) }}}.txt`, 'utf8'));
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).map((packageName) => {
const size = sizes[packageName];
const sizePR = sizesPR[packageName];
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@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
with:
message: ${{ steps.create-report.outputs.result }}
pr-number: ${{ github.event.workflow_run.pull_requests.number }}
comment-tag: bundle-size
44 changes: 44 additions & 0 deletions .github/workflows/bundle-size-untrusted.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading