-
Notifications
You must be signed in to change notification settings - Fork 134
143 lines (120 loc) · 4.86 KB
/
nextjs-bundle-analysis.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
#
# Based on https://github.com/hashicorp/nextjs-bundle-analysis/blob/main/template.yml
name: 'Next.js Bundle Analysis'
on:
pull_request:
branches-ignore:
- 'main'
push:
branches:
- 'canary'
workflow_dispatch:
permissions:
contents: read # for checkout repository
actions: read # for fetching base branch bundle stats
pull-requests: write # for comments
jobs:
analyze:
strategy:
matrix:
next-dir: [
'examples/next/faustwp-getting-started',
]
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install bundle analyzer dependency globally
run: npm install -g @next/bundle-analyzer
- name: Install dependencies
run: npm ci
- name: Build packages
run: npm run build
- name: Create "faust" cli bin
run: npm ci
- name: Create example project .env.local file
working-directory: ${{ matrix.next-dir }}
run: |
cp .env.local.sample .env.local
- name: Add bundle analyzer to next.config.js
working-directory: ${{ matrix.next-dir }}
run: |
echo "const withBundleAnalyzer = require('@next/bundle-analyzer')({enabled: true});" >> next.config.js
echo "module.exports = withBundleAnalyzer(module.exports);" >> next.config.js
- name: Add bundle analyzer config to package.json
working-directory: ${{ matrix.next-dir }}
run: |
npm pkg set nextBundleAnalysis.budget=358400 nextBundleAnalysis.budgetPercentIncreaseRed=20 nextBundleAnalysis.showDetails=true --json
- name: Restore next build
uses: actions/cache@v4
id: restore-build-cache
env:
cache-name: cache-next-build
with:
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
# ex: if your app builds to `dist`, replace `.next` with `dist`
path: ${{ matrix.next-dir }}/.next/cache
# change this if you prefer a more strict cache
key: ${{ runner.os }}-build-${{ env.cache-name }}
- name: Build Next.js example app
working-directory: ${{ matrix.next-dir }}
run: npm run build
# Here's the first place where next-bundle-analysis' own script is used
# This step pulls the raw bundle stats for the current bundle
- name: Create bundle analysis report for this PR
working-directory: ${{ matrix.next-dir }}
run: npx -p nextjs-bundle-analysis report
- name: Upload bundle analysis report for this PR
uses: actions/upload-artifact@v4
with:
name: bundle
path: ${{ matrix.next-dir }}/.next/analyze/__bundle_analysis.json
- name: Download bundle analysis report for base branch
uses: dawidd6/action-download-artifact@v3
if: success() && github.event.number
with:
workflow: nextjs-bundle-analysis.yml
branch: ${{ github.event.pull_request.base.ref }}
path: ${{ matrix.next-dir }}/.next/analyze/base
- name: Compare analysis reports from base branch and PR branch
if: success() && github.event.number
working-directory: ${{ matrix.next-dir }}
run: |
npx -p nextjs-bundle-analysis compare
- name: Get Comment Body
id: get-comment-body
if: success() && github.event.number
working-directory: ${{ matrix.next-dir }}
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
run: |
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "$(cat .next/analyze/__bundle_analysis_comment.txt)" >> $GITHUB_OUTPUT
echo EOF >> $GITHUB_OUTPUT
- name: Find Comment
uses: peter-evans/find-comment@v3
if: success() && github.event.number
id: fc
with:
issue-number: ${{ github.event.number }}
body-includes: '<!-- __NEXTJS_BUNDLE_@faustwp/getting-started-example -->'
- name: Create Comment
uses: peter-evans/create-or-update-comment@v4
continue-on-error: true
if: success() && github.event.number && steps.fc.outputs.comment-id == 0
with:
issue-number: ${{ github.event.number }}
body: ${{ steps.get-comment-body.outputs.body }}
- name: Update Comment
uses: peter-evans/create-or-update-comment@v4
continue-on-error: true
if: success() && github.event.number && steps.fc.outputs.comment-id != 0
with:
issue-number: ${{ github.event.number }}
body: ${{ steps.get-comment-body.outputs.body }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace