-
Notifications
You must be signed in to change notification settings - Fork 156
170 lines (141 loc) · 6.29 KB
/
bundle-size.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
name: Measure bundle size
on:
pull_request:
branches:
- main
permissions:
statuses: write
jobs:
measure:
runs-on: ubuntu-latest
steps:
# ------- Setup -----------------------------------------------------
- uses: actions/github-script@v6
name: Create a pending status on the commit
with:
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.payload.pull_request.head.sha,
context: "Bundle size",
state: 'pending',
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
})
- name: Checkout the PR branch
uses: actions/checkout@v3
with:
path: 'components'
- name: Setup the bundle measurement project
run: |
cp -r components/.github/workflows/bundle-size bundle-size
cd bundle-size
npm i --force
# ------- Baseline size measurement ---------------------------------
- name: Restore the cache for the baseline size measurement
uses: actions/cache/restore@v3
id: restore-cache-baseline
with:
path: bundle-size/output-baseline.txt
key: bundlesize-${{ hashFiles('bundle-size/build.js','bundle-size/index.html','bundle-size/main.jsx','bundle-size/package-lock.json','bundle-size/package.json') }}
- name: Measure the baseline size of the Vite project
if: steps.restore-cache-baseline.outputs.cache-hit != 'true'
run: |
cd bundle-size
npm run build
mv output.txt output-baseline.txt
cat output-baseline.txt
- name: Cache the baseline size
uses: actions/cache/save@v3
if: steps.restore-cache-baseline.outputs.cache-hit != 'true'
with:
path: bundle-size/output-baseline.txt
key: ${{ steps.restore-cache-baseline.outputs.cache-primary-key }}
# ------- Base branch size measurement ------------------------------
- name: Checkout the base branch
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.base.sha }}
path: 'components'
- name: Restore the cache for the base branch size measurement
uses: actions/cache/restore@v3
id: restore-cache-basebranch
with:
path: bundle-size/output-basebranch.txt
key: bundlesize-${{ hashFiles('bundle-size/build.js','bundle-size/index.html','bundle-size/main-with-cloudscape.jsx','bundle-size/package-lock.json','bundle-size/package.json') }}-${{ github.event.pull_request.base.sha }}
- name: Measure the bundle size of the base branch
if: steps.restore-cache-basebranch.outputs.cache-hit != 'true'
run: |
cd components
npm i --force
npm run quick-build
cd ../bundle-size
rm -rf node_modules/@cloudscape-design/components
cp -r $GITHUB_WORKSPACE/components/lib/components node_modules/@cloudscape-design/components
cp main-with-cloudscape.jsx main.jsx
npm run build
mv output.txt output-basebranch.txt
cat output-basebranch.txt
- name: Cache the base branch size
uses: actions/cache/save@v3
if: steps.restore-cache-basebranch.outputs.cache-hit != 'true'
with:
path: bundle-size/output-basebranch.txt
key: ${{ steps.restore-cache-basebranch.outputs.cache-primary-key }}
# ------- Pull request size measurement -----------------------------
- name: Checkout the PR branch
uses: actions/checkout@v3
with:
path: 'components'
- name: Measure the bundle size of this PR
run: |
cd components
npm i --force
npm run quick-build
cd ../bundle-size
rm -rf node_modules/@cloudscape-design/components
cp -r $GITHUB_WORKSPACE/components/lib/components node_modules/@cloudscape-design/components
cp main-with-cloudscape.jsx main.jsx
npm run build
mv output.txt output-pr.txt
cat output-pr.txt
- name: Update the commit status with calculated results
uses: actions/github-script@v6
with:
script: |
const readFileSync = require('node:fs').readFileSync;
const baseline = Number(readFileSync("./bundle-size/output-baseline.txt", "utf8"));
const basebranch = Number(readFileSync("./bundle-size/output-basebranch.txt", "utf8")) - baseline;
const pr = Number(readFileSync("./bundle-size/output-pr.txt", "utf8")) - baseline;
console.log("Baseline:", baseline)
console.log("Base branch (vs baseline):", basebranch);
console.log("This PR (vs baseline):", pr);
const increasePercent = (((pr - basebranch) / basebranch) * 100).toFixed(2)
const increaseKb = ((pr - basebranch) / 1000).toFixed(1);
const sign = pr > basebranch ? "+" : "";
const message = `${(pr / 1000).toFixed(1)} KB (${sign}${increasePercent} % / ${sign}${increaseKb} KB in this pull request)`
const name = "Bundle size";
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.payload.pull_request.head.sha,
context: name,
state: 'success',
description: message,
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
})
# ------- Error reporting -------------------------------------------
- name: Report failure
if: failure()
uses: actions/github-script@v6
with:
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.payload.pull_request.head.sha,
context: name,
state: 'error',
description: "The workflow encountered an error.",
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
})