-
Notifications
You must be signed in to change notification settings - Fork 1
139 lines (122 loc) · 4.89 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
name: Unit Test & Lighthouse CI
on:
push:
branches: [dev]
pull_request:
branches: [dev]
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- name: Install dependencies
run: yarn install
- name: Run tests
run: yarn test
lighthouse:
needs: test
runs-on: ubuntu-latest
if: |
github.event_name == 'push' ||
(github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, '🎱 lighthouse'))
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- name: Install dependencies
run: yarn install
- name: Build project
run: yarn build
- name: Create lighthouse config
run: |
cat > lighthouserc.js << EOF
module.exports = {
ci: {
collect: {
startServerCommand: 'yarn preview --port 3000',
url: ['http://localhost:3000'],
numberOfRuns: 3,
settings: {
preset: 'desktop'
},
startServerReadyPattern: 'Local:',
startServerReadyTimeout: 60000
},
assert: {
assertions: {
'categories:performance': ['warn', {minScore: 0.9}],
'categories:accessibility': ['warn', {minScore: 0.9}],
'categories:best-practices': ['warn', {minScore: 0.9}],
'categories:seo': ['warn', {minScore: 0.9}]
}
},
upload: {
target: 'temporary-public-storage',
outputDir: '.lighthouseci'
}
}
};
EOF
- name: Run Lighthouse CI
run: |
yarn global add @lhci/cli
lhci autorun || echo "Fail to Run Lighthouse CI!"
- name: Format lighthouse score
id: format_lighthouse_score
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs');
const results = JSON.parse(fs.readFileSync("./.lighthouseci/manifest.json"));
let comments = "";
results.forEach((result) => {
const { summary, url, jsonPath } = result;
const details = JSON.parse(fs.readFileSync(jsonPath));
const { audits } = details;
const formatResult = (res) => Math.round(res * 100);
Object.keys(summary).forEach(
(key) => (summary[key] = formatResult(summary[key]))
);
const score = (res) => (res >= 90 ? "🟢" : res >= 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 |`,
`| --- | --- |`,
`| ${score(audits["first-contentful-paint"].score * 100)} First Contentful Paint | ${audits["first-contentful-paint"].displayValue} |`,
`| ${score(audits["largest-contentful-paint"].score * 100)} Largest Contentful Paint | ${audits["largest-contentful-paint"].displayValue} |`,
`| ${score(audits["total-blocking-time"].score * 100)} Total Blocking Time | ${audits["total-blocking-time"].displayValue} |`,
`| ${score(audits["cumulative-layout-shift"].score * 100)} Cumulative Layout Shift | ${audits["cumulative-layout-shift"].displayValue} |`,
`| ${score(audits["speed-index"].score * 100)} Speed Index | ${audits["speed-index"].displayValue} |`
].join("\n");
comments += comment + "\n" + detail + "\n\n---\n\n";
});
core.setOutput('comments', comments)
- name: Comment PR
if: github.event_name == 'pull_request'
uses: unsplash/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
msg: ${{ steps.format_lighthouse_score.outputs.comments}}