-
Notifications
You must be signed in to change notification settings - Fork 293
249 lines (205 loc) · 8.93 KB
/
Copy pathci-metrics.yml
File metadata and controls
249 lines (205 loc) · 8.93 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
name: CI Metrics and Monitoring
# This workflow tracks CI pass rates, job durations, and failure rates
# to help maintain and improve CI reliability over time.
on:
workflow_run:
workflows: ["CI"]
types:
- completed
schedule:
# Run daily at 00:00 UTC to generate summary reports
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
collect-metrics:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion != 'skipped'
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '20'
- name: Collect CI workflow metrics
id: metrics
uses: actions/github-script@v9
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
// Get the last 30 CI workflow runs
const { data: runs } = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: 'ci.yml',
per_page: 30,
status: 'completed'
});
// Calculate metrics
const totalRuns = runs.workflow_runs.length;
const successfulRuns = runs.workflow_runs.filter(r => r.conclusion === 'success').length;
const failedRuns = runs.workflow_runs.filter(r => r.conclusion === 'failure').length;
const cancelledRuns = runs.workflow_runs.filter(r => r.conclusion === 'cancelled').length;
const passRate = totalRuns > 0 ? ((successfulRuns / totalRuns) * 100).toFixed(2) : 0;
// Calculate average duration (in minutes)
const durations = runs.workflow_runs
.filter(r => r.run_started_at && r.updated_at)
.map(r => {
const start = new Date(r.run_started_at);
const end = new Date(r.updated_at);
return (end - start) / 1000 / 60; // Convert to minutes
});
const avgDuration = durations.length > 0
? (durations.reduce((a, b) => a + b, 0) / durations.length).toFixed(2)
: 0;
const maxDuration = durations.length > 0
? Math.max(...durations).toFixed(2)
: 0;
// Count runs over 7 minutes
const slowRuns = durations.filter(d => d > 7).length;
// Store metrics
core.setOutput('total_runs', totalRuns);
core.setOutput('pass_rate', passRate);
core.setOutput('successful_runs', successfulRuns);
core.setOutput('failed_runs', failedRuns);
core.setOutput('cancelled_runs', cancelledRuns);
core.setOutput('avg_duration', avgDuration);
core.setOutput('max_duration', maxDuration);
core.setOutput('slow_runs', slowRuns);
// Generate summary
const summary = `
## CI Metrics (Last 30 Runs)
| Metric | Value |
|--------|-------|
| Total Runs | ${totalRuns} |
| Pass Rate | ${passRate}% |
| Successful | ${successfulRuns} |
| Failed | ${failedRuns} |
| Cancelled | ${cancelledRuns} |
| Avg Duration | ${avgDuration} min |
| Max Duration | ${maxDuration} min |
| Runs >7 min | ${slowRuns} |
`;
await core.summary
.addRaw(summary)
.write();
console.log(summary);
return {
total_runs: totalRuns,
pass_rate: passRate,
successful_runs: successfulRuns,
failed_runs: failedRuns,
cancelled_runs: cancelledRuns,
avg_duration: avgDuration,
max_duration: maxDuration,
slow_runs: slowRuns
};
- name: Check for performance degradation
uses: actions/github-script@v9
with:
script: |
const passRate = parseFloat('${{ steps.metrics.outputs.pass_rate }}');
const slowRuns = parseInt('${{ steps.metrics.outputs.slow_runs }}');
const avgDuration = parseFloat('${{ steps.metrics.outputs.avg_duration }}');
let alerts = [];
// Alert if pass rate is below 80%
if (passRate < 80) {
alerts.push(`⚠️ **Low pass rate**: ${passRate}% (threshold: 80%)`);
}
// Alert if more than 30% of runs take >7 minutes
const totalRuns = parseInt('${{ steps.metrics.outputs.total_runs }}');
const slowRunPercentage = totalRuns > 0 ? (slowRuns / totalRuns) * 100 : 0;
if (slowRunPercentage > 30) {
alerts.push(`⚠️ **High slow run rate**: ${slowRuns}/${totalRuns} runs (${slowRunPercentage.toFixed(1)}%) took >7 minutes`);
}
// Alert if average duration is >10 minutes
if (avgDuration > 10) {
alerts.push(`⚠️ **High average duration**: ${avgDuration} minutes (threshold: 10 min)`);
}
if (alerts.length > 0) {
const alertSummary = `
## ⚠️ CI Performance Alerts
${alerts.map(a => `- ${a}`).join('\n')}
**Action Required**: Review recent CI runs and consider:
- Investigating slow jobs
- Checking for resource contention
- Reviewing timeout settings
- Looking for flaky tests
`;
await core.summary
.addRaw(alertSummary)
.write();
console.log(alertSummary);
// Set output for potential notifications
core.setOutput('has_alerts', 'true');
core.setOutput('alert_message', alertSummary);
} else {
console.log('✅ No performance alerts - CI is running healthy');
core.setOutput('has_alerts', 'false');
}
collect-job-metrics:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion != 'skipped' || github.event_name == 'workflow_dispatch'
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Collect individual job metrics
uses: actions/github-script@v9
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
// Get the most recent CI run
const { data: runs } = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: 'ci.yml',
per_page: 1,
status: 'completed'
});
if (runs.workflow_runs.length === 0) {
console.log('No completed runs found');
return;
}
const latestRun = runs.workflow_runs[0];
// Get jobs for this run
const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({
owner,
repo,
run_id: latestRun.id
});
// Calculate job durations
const jobMetrics = jobs.jobs.map(job => {
const start = new Date(job.started_at);
const end = new Date(job.completed_at);
const duration = (end - start) / 1000 / 60; // minutes
return {
name: job.name,
conclusion: job.conclusion,
duration: duration.toFixed(2),
is_slow: duration > 7
};
});
// Generate job summary
const jobSummary = `
## Job Metrics (Run #${latestRun.run_number})
| Job Name | Status | Duration (min) | Alert |
|----------|--------|----------------|-------|
${jobMetrics.map(j =>
`| ${j.name} | ${j.conclusion} | ${j.duration} | ${j.is_slow ? '⚠️ Slow' : '✅'} |`
).join('\n')}
**Slow jobs** (>7 min): ${jobMetrics.filter(j => j.is_slow).length}
`;
await core.summary
.addRaw(jobSummary)
.write();
console.log(jobSummary);
// Alert on specific slow jobs
const slowJobs = jobMetrics.filter(j => j.is_slow);
if (slowJobs.length > 0) {
console.log('\n⚠️ Slow jobs detected:');
slowJobs.forEach(j => {
console.log(` - ${j.name}: ${j.duration} minutes`);
});
}