Skip to content

Commit 7a2b5e1

Browse files
atinuxampagent
andcommitted
ci(bundle): use nuxt-bundle-analyzer like nuxt.com
Analyze base and PR with nuxt-bundle-analyzer, upload them to bundle.nuxt.dev for the PR comment, and record the bundle history of main. Removes the custom reporter script. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c01d-29e6-7499-b036-cbaf5eb02423 Co-authored-by: Amp <[email protected]>
1 parent 86c6b24 commit 7a2b5e1

6 files changed

Lines changed: 129 additions & 1030 deletions

File tree

Lines changed: 29 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
name: bundle-size-comment
22

3+
# Runs from the default branch once `bundle-size` completes, also for fork PRs. The runs it reads come from the PR's
4+
# build and are only parsed by nuxt-bundle-analyzer, never executed.
35
on:
46
workflow_run:
57
workflows:
@@ -10,221 +12,46 @@ on:
1012
permissions:
1113
actions: read
1214
contents: read
13-
issues: write
14-
pull-requests: read
15+
# Proves to bundle.nuxt.dev that the upload comes from this workflow on the default branch.
16+
id-token: write
17+
18+
env:
19+
NUXT_BUNDLE_ANALYZER_VERSION: 0.1.7
1520

1621
jobs:
1722
comment:
1823
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request'
19-
runs-on: ubuntu-latest
24+
runs-on: ubuntu-slim
2025
timeout-minutes: 10
2126
steps:
22-
- name: Validate triggering workflow
23-
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
24-
with:
25-
script: |
26-
const run = context.payload.workflow_run
27-
const shaPattern = /^[0-9a-f]{40}$/
28-
29-
if (run.name !== 'bundle-size') {
30-
core.setFailed('The triggering workflow name is invalid.')
31-
return
32-
}
33-
34-
if (!Number.isSafeInteger(run.id) || run.id <= 0 || !shaPattern.test(run.head_sha) || !Number.isSafeInteger(run.head_repository?.id) || run.head_repository.id <= 0) {
35-
core.setFailed('The workflow payload contains invalid head repository metadata.')
36-
return
37-
}
38-
- name: Resolve bundle artifact
39-
id: artifact
40-
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
27+
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
4128
with:
42-
script: |
43-
const run = context.payload.workflow_run
44-
const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, {
45-
owner: context.repo.owner,
46-
repo: context.repo.repo,
47-
run_id: run.id,
48-
per_page: 100
49-
})
50-
const matches = artifacts.filter(artifact => artifact.name === 'bundle-size-report' && !artifact.expired)
51-
52-
if (matches.length !== 1) {
53-
core.setFailed('Expected exactly one bundle report artifact from the triggering run.')
54-
return
55-
}
56-
57-
const artifact = matches[0]
58-
if (!Number.isSafeInteger(artifact.id) || artifact.id <= 0 || !Number.isSafeInteger(artifact.size_in_bytes) || artifact.size_in_bytes <= 0 || artifact.size_in_bytes > 12 * 1024 * 1024) {
59-
core.setFailed('The bundle report artifact metadata is invalid or too large.')
60-
return
61-
}
62-
63-
core.setOutput('id', String(artifact.id))
64-
- name: Download untrusted bundle snapshots
65-
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
29+
node-version: lts/-1
30+
- name: Download the runs
31+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
6632
with:
67-
artifact-ids: ${{ steps.artifact.outputs.id }}
68-
path: ${{ runner.temp }}/bundle-size-report
69-
merge-multiple: true
70-
github-token: ${{ secrets.GITHUB_TOKEN }}
33+
path: runs
7134
run-id: ${{ github.event.workflow_run.id }}
72-
- name: Resolve triggering pull request
73-
id: pull
74-
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
35+
github-token: ${{ github.token }}
36+
# The comment is still posted without the comparison link when this fails.
37+
- name: Upload the runs to bundle.nuxt.dev
38+
id: upload
39+
continue-on-error: true
40+
run: npx --yes "nuxt-bundle-analyzer@$NUXT_BUNDLE_ANALYZER_VERSION" upload --runs runs --url https://bundle.nuxt.dev
7541
env:
76-
BUNDLE_CONTEXT_PATH: ${{ runner.temp }}/bundle-size-report/context.json
77-
with:
78-
script: |
79-
const fs = require('node:fs')
80-
const path = require('node:path')
81-
const run = context.payload.workflow_run
82-
const repository = context.payload.repository
83-
const candidates = run.pull_requests || []
84-
const contextPath = process.env.BUNDLE_CONTEXT_PATH
85-
const shaPattern = /^[0-9a-f]{40}$/
86-
87-
core.setOutput('should-comment', 'false')
88-
89-
const artifactDirectory = path.dirname(contextPath)
90-
const files = fs.readdirSync(artifactDirectory).sort()
91-
if (files.length !== 3 || files[0] !== 'base.json' || files[1] !== 'context.json' || files[2] !== 'pr.json') {
92-
core.setFailed('The bundle artifact contains unexpected files.')
93-
return
94-
}
95-
96-
const contextStats = fs.lstatSync(contextPath)
97-
const baseStats = fs.lstatSync(path.join(artifactDirectory, 'base.json'))
98-
const headStats = fs.lstatSync(path.join(artifactDirectory, 'pr.json'))
99-
if (!contextStats.isFile() || contextStats.isSymbolicLink() || contextStats.size > 1_024 ||
100-
!baseStats.isFile() || baseStats.isSymbolicLink() || baseStats.size > 5 * 1024 * 1024 ||
101-
!headStats.isFile() || headStats.isSymbolicLink() || headStats.size > 5 * 1024 * 1024) {
102-
core.setFailed('The bundle artifact does not contain bounded regular files.')
103-
return
104-
}
105-
106-
const bundleContext = JSON.parse(fs.readFileSync(contextPath, 'utf8'))
107-
const contextKeys = Object.keys(bundleContext)
108-
if (contextKeys.length !== 1 || contextKeys[0] !== 'prNumber' || !Number.isSafeInteger(bundleContext.prNumber) || bundleContext.prNumber <= 0) {
109-
core.setFailed('The bundle context contains an invalid pull request number.')
110-
return
111-
}
112-
113-
if (candidates.length > 1 || (candidates.length === 1 && candidates[0].number !== bundleContext.prNumber)) {
114-
core.setFailed('The bundle context does not match the workflow payload.')
115-
return
116-
}
117-
118-
const { data: pull } = await github.rest.pulls.get({
119-
owner: context.repo.owner,
120-
repo: context.repo.repo,
121-
pull_number: bundleContext.prNumber
122-
})
123-
124-
if (pull.state !== 'open' || pull.head.sha !== run.head_sha) {
125-
core.info(`Skipping stale or closed PR #${bundleContext.prNumber}.`)
126-
return
127-
}
128-
129-
if (pull.head.repo?.id !== run.head_repository.id) {
130-
core.setFailed('The pull request head repository does not match the triggering workflow.')
131-
return
132-
}
133-
134-
if (pull.base.repo.id !== repository.id || pull.base.ref !== repository.default_branch || !shaPattern.test(pull.base.sha)) {
135-
core.setFailed('The pull request does not target the expected default branch.')
136-
return
137-
}
138-
139-
core.setOutput('should-comment', 'true')
140-
core.setOutput('number', String(bundleContext.prNumber))
141-
core.setOutput('base-sha', pull.base.sha)
142-
core.setOutput('head-sha', run.head_sha)
143-
- name: Check out the trusted reporter
144-
if: steps.pull.outputs.should-comment == 'true'
145-
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
146-
with:
147-
persist-credentials: false
148-
ref: ${{ github.sha }}
149-
- if: steps.pull.outputs.should-comment == 'true'
150-
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
151-
with:
152-
node-version: lts/-1
153-
- name: Validate snapshots and render report
154-
if: steps.pull.outputs.should-comment == 'true'
155-
env:
156-
BASE_SHA: ${{ steps.pull.outputs.base-sha }}
157-
BASE_SNAPSHOT: ${{ runner.temp }}/bundle-size-report/base.json
158-
HEAD_SHA: ${{ steps.pull.outputs.head-sha }}
159-
HEAD_SNAPSHOT: ${{ runner.temp }}/bundle-size-report/pr.json
160-
REPORT_PATH: ${{ runner.temp }}/bundle-size-comment.md
161-
run: |
162-
node scripts/bundle-size/report.mjs compare \
163-
--base "$BASE_SNAPSHOT" \
164-
--head "$HEAD_SNAPSHOT" \
165-
--base-sha "$BASE_SHA" \
166-
--head-sha "$HEAD_SHA" \
167-
--output "$REPORT_PATH"
42+
GITHUB_TOKEN: ${{ github.token }}
43+
# GitHub makes `GITHUB_TOKEN` read-only in run chains that start from a fork's PR, so the app comments.
16844
- name: Mint bundle report app token
169-
if: steps.pull.outputs.should-comment == 'true'
17045
id: app-token
171-
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
46+
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
17247
with:
17348
app-id: ${{ secrets.BUNDLE_REPORT_APP_ID }}
17449
private-key: ${{ secrets.BUNDLE_REPORT_APP_PRIVATE_KEY }}
175-
- name: Update PR comment
176-
if: steps.pull.outputs.should-comment == 'true'
177-
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
50+
# What `comment --post` calls: it lists the open PRs, then creates or updates its comment.
51+
permission-issues: write
52+
permission-pull-requests: read
53+
- name: Comment on the PR
54+
run: npx --yes "nuxt-bundle-analyzer@$NUXT_BUNDLE_ANALYZER_VERSION" comment --runs runs --post --link "$LINK"
17855
env:
179-
BUNDLE_REPORT_PATH: ${{ runner.temp }}/bundle-size-comment.md
180-
PR_NUMBER: ${{ steps.pull.outputs.number }}
181-
with:
182-
github-token: ${{ steps.app-token.outputs.token }}
183-
script: |
184-
const fs = require('node:fs')
185-
const marker = '<!-- nuxt.com-bundle-size-report -->'
186-
const run = context.payload.workflow_run
187-
const prNumber = Number(process.env.PR_NUMBER)
188-
189-
if (!Number.isSafeInteger(prNumber) || prNumber <= 0) {
190-
core.setFailed('The validated pull request number is invalid.')
191-
return
192-
}
193-
194-
const report = fs.readFileSync(process.env.BUNDLE_REPORT_PATH, 'utf8')
195-
if (Buffer.byteLength(report, 'utf8') > 60_000) {
196-
core.setFailed('The bundle report is too large to post safely.')
197-
return
198-
}
199-
200-
const body = `${marker}\n${report}\n[Workflow run](${run.html_url})`
201-
202-
try {
203-
const comments = await github.paginate(github.rest.issues.listComments, {
204-
owner: context.repo.owner,
205-
repo: context.repo.repo,
206-
issue_number: prNumber,
207-
per_page: 100
208-
})
209-
const previous = comments.find(comment => comment.user?.login?.endsWith('[bot]') && comment.body?.includes(marker))
210-
211-
if (previous) {
212-
await github.rest.issues.updateComment({
213-
owner: context.repo.owner,
214-
repo: context.repo.repo,
215-
comment_id: previous.id,
216-
body
217-
})
218-
} else {
219-
await github.rest.issues.createComment({
220-
owner: context.repo.owner,
221-
repo: context.repo.repo,
222-
issue_number: prNumber,
223-
body
224-
})
225-
}
226-
} catch (error) {
227-
core.warning(`Unable to post the bundle report comment: ${error.message}`)
228-
await core.summary.addRaw(report).write()
229-
core.info('The bundle report was written to the workflow run summary instead.')
230-
}
56+
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
57+
LINK: ${{ steps.upload.outputs.url }}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: bundle-size-history
2+
3+
# Records the bundle of each commit pushed to main at https://bundle.nuxt.dev/nuxt-content/docus/tree/main,
4+
# which charts the size after each commit and compares any commit with the one before it.
5+
on:
6+
push:
7+
branches:
8+
- main
9+
10+
permissions:
11+
contents: read
12+
13+
env:
14+
NUXT_BUNDLE_ANALYZER_VERSION: 0.1.7
15+
16+
jobs:
17+
analyze:
18+
name: analyze
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 60
21+
steps:
22+
- name: Check out the commit
23+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
24+
with:
25+
persist-credentials: false
26+
- uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0
27+
with:
28+
cache: true
29+
cache-dependency-path: pnpm-lock.yaml
30+
runtime: node@24
31+
- name: Prepare the commit
32+
run: pnpm run dev:prepare
33+
# With npx, so the analyzer needs no entry in the lockfile.
34+
- name: Analyze the bundle
35+
run: npx --yes "nuxt-bundle-analyzer@$NUXT_BUNDLE_ANALYZER_VERSION" docs --extends ../layer --name main --no-serve
36+
env:
37+
NODE_OPTIONS: --max-old-space-size=8192
38+
NUXT_TELEMETRY_DISABLED: 1
39+
- name: Upload the bundle analysis
40+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
41+
with:
42+
name: bundle-history
43+
path: docs/node_modules/.cache/nuxt-bundle-analyzer/main
44+
if-no-files-found: error
45+
retention-days: 1
46+
47+
# Apart from the build, so the dependencies it installs never run in a job that can prove to
48+
# bundle.nuxt.dev that it is this workflow.
49+
upload:
50+
name: upload
51+
needs: analyze
52+
runs-on: ubuntu-latest
53+
timeout-minutes: 10
54+
permissions:
55+
contents: read
56+
# Proves to bundle.nuxt.dev that the upload comes from this workflow on the default branch.
57+
id-token: write
58+
steps:
59+
# The upload reads the commit's message and time from git.
60+
- name: Check out the commit
61+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
62+
with:
63+
persist-credentials: false
64+
sparse-checkout: package.json
65+
sparse-checkout-cone-mode: false
66+
- name: Download the bundle analysis
67+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
68+
with:
69+
name: bundle-history
70+
path: ${{ runner.temp }}/runs/main
71+
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
72+
with:
73+
node-version: lts/-1
74+
- name: Upload to bundle.nuxt.dev
75+
run: >-
76+
npx --yes "nuxt-bundle-analyzer@$NUXT_BUNDLE_ANALYZER_VERSION" upload .
77+
--runs "$RUNNER_TEMP/runs"
78+
--name main
79+
--branch main
80+
--sha "$GITHUB_SHA"
81+
--url https://bundle.nuxt.dev

0 commit comments

Comments
 (0)