-
Notifications
You must be signed in to change notification settings - Fork 1
215 lines (180 loc) · 7.13 KB
/
lighthouse.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
name: Lighthouse CI
on:
push:
branches: [dev]
pull_request:
branches: [dev]
jobs:
lighthouse:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Build
run: yarn build
- name: Run server and Lighthouse CI
run: |
yarn dev & # 백그라운드에서 서버 시작
sleep 10 # 서버가 완전히 시작될 때까지 대기
- name: Run Lighthouse CI
uses: treosh/lighthouse-ci-action@v9
with:
urls: |
http://localhost:3000
uploadArtifacts: true
temporaryPublicStorage: true
- name: Format lighthouse score
if: success()
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
try {
if (!fs.existsSync('.lighthouseci/manifest.json')) {
console.error('Manifest file not found');
core.setFailed('manifest.json not found');
return;
}
let rawData;
try {
rawData = fs.readFileSync('.lighthouseci/manifest.json', 'utf8');
} catch (readError) {
console.error('Error reading manifest:', readError);
core.setFailed('Failed to read manifest.json');
return;
}
if (!rawData || rawData.trim() === '') {
console.error('Manifest is empty');
core.setFailed('manifest.json is empty');
return;
}
let results;
try {
results = JSON.parse(rawData);
} catch (parseError) {
console.error('Error parsing manifest:', parseError);
core.setFailed('Failed to parse manifest.json');
return;
}
if (!Array.isArray(results)) {
console.error('Results is not an array');
core.setFailed('Invalid manifest format');
return;
}
let comments = "";
results.forEach((result) => {
if (!result.summary || !result.url || !result.jsonPath) {
console.warn('Missing required properties in result');
return;
}
let details;
try {
details = JSON.parse(fs.readFileSync(result.jsonPath, 'utf8'));
} catch (error) {
console.warn(`Failed to read or parse ${result.jsonPath}`, error);
return;
}
if (!details.audits) {
console.warn('No audits found in details');
return;
}
const { summary, url } = result;
const { audits } = details;
const formatResult = (res) => {
if (typeof res !== 'number') return 0;
return Math.round(Math.max(0, Math.min(100, res * 100)));
};
Object.keys(summary).forEach(
(key) => (summary[key] = formatResult(summary[key]))
);
const score = (res) => {
const numRes = Number(res);
return isNaN(numRes) ? "🔴" : (numRes >= 90 ? "🟢" : numRes >= 50 ? "🟠" : "🔴");
};
const comment = [
`⚡ Lighthouse report for ${url}`,
`| Category | Score |`,
`| --- | --- |`,
`| ${score(summary.performance)} Performance | ${summary.performance} |`,
`| ${score(summary.accessibility)} Accessibility | ${summary.accessibility} |`,
`| ${score(summary['best-practices'])} Best Practices | ${summary['best-practices']} |`,
`| ${score(summary.seo)} SEO | ${summary.seo} |`
].join("\n");
const detail = [
`\n### Detailed Metrics`,
`| Metric | Value |`,
`| --- | --- |`
];
const metrics = [
["first-contentful-paint", "First Contentful Paint"],
["largest-contentful-paint", "Largest Contentful Paint"],
["total-blocking-time", "Total Blocking Time"],
["cumulative-layout-shift", "Cumulative Layout Shift"],
["speed-index", "Speed Index"]
];
metrics.forEach(([key, label]) => {
if (audits[key] && typeof audits[key].score === 'number') {
detail.push(
`| ${score(audits[key].score * 100)} ${label} | ${audits[key].displayValue || 'N/A'} |`
);
}
});
comments += comment + "\n" + detail.join("\n") + "\n\n---\n\n";
});
const owner = context.repo.owner;
const repo = context.repo.repo;
if (!owner || !repo) {
core.setFailed('Missing repository information');
return;
}
try {
if (context.payload.pull_request) {
const prNumber = context.payload.pull_request.number;
if (!prNumber) {
throw new Error('Missing pull request number');
}
await github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: comments
});
} else {
const sha = context.sha;
if (!sha) {
throw new Error('Missing commit SHA');
}
await github.rest.repos.createCommitComment({
owner,
repo,
commit_sha: sha,
body: comments
});
}
core.setOutput('comments', comments);
} catch (apiError) {
console.error('API call failed:', apiError);
core.setFailed(`Failed to create comment: ${apiError.message}`);
return;
}
} catch (error) {
console.error('Unexpected error:', error);
core.setFailed(`Unexpected error: ${error.message}`);
return;
}