diff --git a/.github/workflows/bundle-size-comment.yml b/.github/workflows/bundle-size-comment.yml new file mode 100644 index 000000000..15431c7c1 --- /dev/null +++ b/.github/workflows/bundle-size-comment.yml @@ -0,0 +1,89 @@ +name: Bundle size comment +on: + workflow_run: + workflows: [Check] + types: [completed] + +# Runs for one PR branch are serialized so they cannot both create a comment. +concurrency: + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} + +permissions: {} + +jobs: + comment: + name: Comment + if: github.event.workflow_run.event == 'pull_request' + runs-on: ubuntu-latest + permissions: + actions: read + pull-requests: write + timeout-minutes: 5 + steps: + # The artifact comes from PR code. Never check out or run it: only numbers from it reach the comment. + - name: Create or update comment + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + RUN_ID: ${{ github.event.workflow_run.id }} + RUN_URL: ${{ github.event.workflow_run.html_url }} + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} + HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }} + HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }} + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} + MARKER: "" + run: | + artifacts=$(gh api "repos/$GH_REPO/actions/runs/$RUN_ID/artifacts?name=bundle-size" --jq .total_count) + if [[ "$artifacts" == 0 ]]; then + echo "Run $RUN_ID has no bundle size report." + exit 0 + fi + # Fork runs list no PR, so find the one open PR whose head is the branch and commit Check measured. + pr=$(gh api -X GET "repos/$GH_REPO/pulls" -f state=open -f base=main -f head="$HEAD_OWNER:$HEAD_BRANCH" \ + --jq '[.[] | select(.head.sha == env.HEAD_SHA and .head.repo.full_name == env.HEAD_REPO) | .number] | if length == 1 then .[0] else empty end') + if [[ -z "$pr" ]]; then + echo "No single open PR from $HEAD_REPO:$HEAD_BRANCH at $HEAD_SHA." + exit 0 + fi + gh run download "$RUN_ID" --name bundle-size --dir sizes + [[ -f sizes/base.json ]] || echo null > sizes/base.json + # Invalid head data fails the job. An unreadable base only turns the deltas into n/a, as in the Check report. + jq -r --rawfile base sizes/base.json ' + def valid: type == "number" and . == floor and . >= 0 and . < 1e9; + def size: if valid then . else error("invalid size") end; + def signed: if . > 0 then "+\(.)" else tostring end; + def row: "| " + (map(tostring) | join(" | ")) + " |"; + .development as $development + | (.baselineGzip | size) as $baseline + | [.apps[] | { + name: (.name | if IN("counter", "features", "routing") then . else error("unknown app") end), + variant, chunks: (.chunks | size), raw: (.raw | size), gzip: (.gzip | size) + }] as $head + | ($base | try fromjson catch {}) as $before + | (["App", "Chunks", "Raw bytes", "Gzip bytes", "Added gzip B"] + + if $before == null then [] else ["Δ raw bytes", "Δ gzip bytes"] end) as $headers + | env.MARKER, + "### Bundle size", + "", + "Production build, gzip level 9 per chunk. Effect-only client: \($baseline) B gzip. [Full report](\(env.RUN_URL))", + "", + ($headers | row), + ($headers | map("---") | row), + ($head[] | . as $app + | [.name, .chunks, .raw, .gzip, .gzip - $baseline] + + if $before == null then [] else + (first($before | objects | select(.development == $development) | .apps | arrays | .[] | objects + | select(.name == $app.name and .variant == $app.variant)) // null) as $old + | if ($old.raw | valid) and ($old.gzip | valid) + then [($app.raw - $old.raw | signed), ($app.gzip - $old.gzip | signed)] + else ["n/a", "n/a"] end + end + | row) + ' sizes/head.json > body.md + ids=$(gh api --paginate "repos/$GH_REPO/issues/$pr/comments?per_page=100" \ + --jq '.[] | select(.user.login == "github-actions[bot]" and (.body | startswith(env.MARKER))) | .id') + if [[ -n "$ids" ]]; then + gh api --method PATCH "repos/$GH_REPO/issues/comments/${ids%%$'\n'*}" -F body=@body.md --silent + else + gh api "repos/$GH_REPO/issues/$pr/comments" -F body=@body.md --silent + fi diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 0a6fa21db..e66fdae57 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -70,19 +70,28 @@ jobs: # A base that cannot be measured leaves no file, and the report shows absolute sizes only. continue-on-error: true run: | + mkdir -p "$RUNNER_TEMP/bundle-size" git worktree add --detach "$RUNNER_TEMP/bundle-base" HEAD^1 cd "$RUNNER_TEMP/bundle-base" pnpm install pnpm build - env -u GITHUB_STEP_SUMMARY pnpm bundle-size --json "$RUNNER_TEMP/base-size.json" + env -u GITHUB_STEP_SUMMARY pnpm bundle-size --json "$RUNNER_TEMP/bundle-size/base.json" - name: Report bundle size run: | - base="$RUNNER_TEMP/base-size.json" - if [[ -f "$base" ]]; then - pnpm bundle-size --base "$base" - else - pnpm bundle-size - fi + dir="$RUNNER_TEMP/bundle-size" + mkdir -p "$dir" + args=(--json "$dir/head.json") + if [[ -f "$dir/base.json" ]]; then args+=(--base "$dir/base.json"); fi + pnpm bundle-size "${args[@]}" + # Fork PRs get a read-only token here, so the Bundle size comment workflow posts the sizes. + - name: Upload sizes + if: github.event_name == 'pull_request' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: bundle-size + path: ${{ runner.temp }}/bundle-size + retention-days: 7 + if-no-files-found: error test: name: Test (${{ matrix.runtime }})