forked from beehive-lab/GPULlama3.java
-
Notifications
You must be signed in to change notification settings - Fork 0
182 lines (158 loc) · 7.05 KB
/
rerun-workflow.yml
File metadata and controls
182 lines (158 loc) · 7.05 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
name: Rerun Workflows
on:
issue_comment:
types: [created]
jobs:
rerun:
name: Rerun CI Workflows
# Only run on PR comments (not issue comments) with /rerun command
if: |
github.repository == 'beehive-lab/GPULlama3.java' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, '/rerun')
runs-on: ubuntu-latest
permissions:
actions: write
pull-requests: write
contents: read
steps:
- name: Check for help command
id: help
uses: actions/github-script@v7
with:
script: |
const comment = context.payload.comment.body;
if (comment.match(/\/rerun\s+help/i)) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `## 🔄 Rerun Workflow Commands
| Command | Description |
|---------|-------------|
| \`/rerun\` | Rerun only **failed/cancelled/timed-out** workflows |
| \`/rerun all\` | Rerun **all** workflows for this PR |
| \`/rerun failed\` | Same as \`/rerun\` |
| \`/rerun <name>\` | Rerun workflows matching \`<name>\` (e.g. \`/rerun ci\`, \`/rerun build\`) |
| \`/rerun help\` | Show this help message |
**Note:** Only completed workflows can be rerun. In-progress workflows are skipped.`
});
core.setOutput('is_help', 'true');
} else {
core.setOutput('is_help', 'false');
}
- name: Get PR SHA
if: steps.help.outputs.is_help != 'true'
id: pr
uses: actions/github-script@v7
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
core.setOutput('sha', pr.head.sha);
core.setOutput('head_ref', pr.head.ref);
console.log(`PR #${context.issue.number} SHA: ${pr.head.sha}`);
console.log(`PR head ref: ${pr.head.ref}`);
- name: Add reaction to comment
if: steps.help.outputs.is_help != 'true'
uses: actions/github-script@v7
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket'
});
- name: Post start comment
if: steps.help.outputs.is_help != 'true'
uses: actions/github-script@v7
with:
script: |
const comment = context.payload.comment.body;
const rerunMatch = comment.match(/\/rerun\s*(\S+)?/);
const rerunArg = rerunMatch && rerunMatch[1] ? rerunMatch[1] : 'failed';
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `🚀 **Workflow rerun started**\n\nMode: \`${rerunArg}\`\nTriggered by: @${context.payload.comment.user.login}\n\n[View Actions](https://github.com/${context.repo.owner}/${context.repo.repo}/actions)`
});
- name: Rerun failed workflows
if: steps.help.outputs.is_help != 'true'
uses: actions/github-script@v7
with:
script: |
const sha = '${{ steps.pr.outputs.sha }}';
const headRef = '${{ steps.pr.outputs.head_ref }}';
// Get all workflow runs for this PR's head SHA
const { data: runs } = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
head_sha: sha,
per_page: 100
});
console.log(`Found ${runs.total_count} workflow runs for SHA ${sha}`);
if (runs.total_count === 0) {
console.log('No workflow runs found for this PR');
return;
}
// Parse command for specific workflow filter
// Supports: /rerun, /rerun all, /rerun failed, /rerun <workflow-name>
const comment = context.payload.comment.body;
const rerunMatch = comment.match(/\/rerun\s*(\S+)?/);
const rerunArg = rerunMatch && rerunMatch[1] ? rerunMatch[1].toLowerCase() : 'failed';
console.log(`Rerun mode: ${rerunArg}`);
let rerunCount = 0;
for (const run of runs.workflow_runs) {
const shouldRerun =
rerunArg === 'all' ||
(rerunArg === 'failed' && ['failure', 'cancelled', 'timed_out'].includes(run.conclusion)) ||
run.name.toLowerCase().includes(rerunArg);
if (!shouldRerun) {
console.log(`Skipping ${run.name} (status: ${run.status}, conclusion: ${run.conclusion})`);
continue;
}
// Only rerun completed workflows
if (run.status !== 'completed') {
console.log(`Skipping ${run.name} - still ${run.status}`);
continue;
}
try {
console.log(`Rerunning workflow: ${run.name} (ID: ${run.id})`);
// Use rerun-failed-jobs if available and workflow failed, otherwise full rerun
if (['failure', 'cancelled', 'timed_out'].includes(run.conclusion)) {
await github.rest.actions.reRunWorkflowFailedJobs({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id
});
} else {
await github.rest.actions.reRunWorkflow({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id
});
}
rerunCount++;
} catch (error) {
console.log(`Failed to rerun ${run.name}: ${error.message}`);
}
}
console.log(`Reran ${rerunCount} workflow(s)`);
- name: Post completion comment
if: always() && steps.help.outputs.is_help != 'true'
uses: actions/github-script@v7
with:
script: |
const status = '${{ job.status }}';
const emoji = status === 'success' ? '✅' : '❌';
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `${emoji} **Workflow rerun ${status}**\n\n[View Actions](https://github.com/${context.repo.owner}/${context.repo.repo}/actions)`
});