Skip to content

Commit 3e82231

Browse files
authored
Create nextjs_bundle_analysis.yml
1 parent b300c02 commit 3e82231

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: 'Next.js Bundle Analysis'
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- next # change this if your default branch is named differently
8+
workflow_dispatch:
9+
10+
defaults:
11+
run:
12+
# change this if your nextjs app does not live at the root of the repo
13+
working-directory: .
14+
15+
jobs:
16+
analyze:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
21+
- name: Use Node.js 14.x
22+
uses: actions/setup-node@v1
23+
with:
24+
node-version: 14.x
25+
26+
- name: Cache node modules
27+
uses: actions/cache@v1
28+
with:
29+
path: node_modules
30+
key: yarn-deps-${{ hashFiles('yarn.lock') }}
31+
restore-keys: |
32+
yarn-deps-${{ hashFiles('yarn.lock') }}
33+
34+
- name: Build library
35+
run: |
36+
yarn install --frozen-lockfile 2>&1 | grep -v '^[warning|info]'
37+
yarn build:lib
38+
39+
- name: Change dir to next app
40+
run: cd examples/bundle-test
41+
42+
- name: Restore next build
43+
uses: actions/cache@v2
44+
id: restore-build-cache
45+
env:
46+
cache-name: cache-next-build
47+
with:
48+
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
49+
# ex: if your app builds to `dist`, replace `.next` with `dist`
50+
path: .next/cache
51+
# change this if you prefer a more strict cache
52+
key: ${{ runner.os }}-build-${{ env.cache-name }}
53+
54+
- name: Build next.js app
55+
# change this if your site requires a custom build command
56+
run: yarn build
57+
58+
# Here's the first place where next-bundle-analysis' own script is used
59+
# This step pulls the raw bundle stats for the current bundle
60+
- name: Analyze bundle
61+
run: npx -p nextjs-bundle-analysis report
62+
63+
- name: Upload bundle
64+
uses: actions/upload-artifact@v2
65+
with:
66+
name: bundle
67+
path: .next/analyze/__bundle_analysis.json
68+
69+
- name: Download base branch bundle stats
70+
uses: dawidd6/action-download-artifact@v2
71+
if: success() && github.event.number
72+
with:
73+
workflow: nextjs_bundle_analysis.yml
74+
branch: ${{ github.event.pull_request.base.ref }}
75+
path: .next/analyze/base
76+
77+
# And here's the second place - this runs after we have both the current and
78+
# base branch bundle stats, and will compare them to determine what changed.
79+
# There are two configurable arguments that come from package.json:
80+
#
81+
# - budget: optional, set a budget (bytes) against which size changes are measured
82+
# it's set to 350kb here by default, as informed by the following piece:
83+
# https://infrequently.org/2021/03/the-performance-inequality-gap/
84+
#
85+
# - red-status-percentage: sets the percent size increase where you get a red
86+
# status indicator, defaults to 20%
87+
#
88+
# Either of these arguments can be changed or removed by editing the `nextBundleAnalysis`
89+
# entry in your package.json file.
90+
- name: Compare with base branch bundle
91+
if: success() && github.event.number
92+
run: ls -laR .next/analyze/base && npx -p nextjs-bundle-analysis compare
93+
94+
- name: Get comment body
95+
id: get-comment-body
96+
if: success() && github.event.number
97+
run: |
98+
body=$(cat .next/analyze/__bundle_analysis_comment.txt)
99+
body="${body//'%'/'%25'}"
100+
body="${body//$'\n'/'%0A'}"
101+
body="${body//$'\r'/'%0D'}"
102+
echo ::set-output name=body::$body
103+
104+
- name: Find Comment
105+
uses: peter-evans/find-comment@v1
106+
if: success() && github.event.number
107+
id: fc
108+
with:
109+
issue-number: ${{ github.event.number }}
110+
body-includes: '<!-- __NEXTJS_BUNDLE -->'
111+
112+
- name: Create Comment
113+
uses: peter-evans/[email protected]
114+
if: success() && github.event.number && steps.fc.outputs.comment-id == 0
115+
with:
116+
issue-number: ${{ github.event.number }}
117+
body: ${{ steps.get-comment-body.outputs.body }}
118+
119+
- name: Update Comment
120+
uses: peter-evans/[email protected]
121+
if: success() && github.event.number && steps.fc.outputs.comment-id != 0
122+
with:
123+
issue-number: ${{ github.event.number }}
124+
body: ${{ steps.get-comment-body.outputs.body }}
125+
comment-id: ${{ steps.fc.outputs.comment-id }}
126+
edit-mode: replace

0 commit comments

Comments
 (0)