forked from buhonghao/Redpill_CustomBuild
-
Notifications
You must be signed in to change notification settings - Fork 0
387 lines (340 loc) · 15.6 KB
/
schedule.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#
# Copyright (C) 2022 Ing <https://github.com/wjz304>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# 默认情况下,使用存储库的GITHUB_TOKEN执行任务时,GITHUB_TOKEN触发的事件不会创建新的工作流运行。这可以防止意外创建递归工作流运行。
# 可以通过个人TOKEN在工作流运行中触发工作流。
# 1. 在 https://github.com/settings/tokens 创建 一个 token
# 2. 在 https://github.com/${{ github.repository }}/settings/secrets/actions 加入上一步创建的 token
name: Custom Redpill(schedule)
on:
issue_comment:
types: [created, edited, deleted]
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
# dump:
# runs-on: ubuntu-latest
# steps:
# - name: Dump GitHub context
# id: github_context_step
# run: echo '${{ toJSON(github) }}'
# - name: Dump job context
# run: echo '${{ toJSON(job) }}'
# - name: Dump steps context
# run: echo '${{ toJSON(steps) }}'
# - name: Dump runner context
# run: echo '${{ toJSON(runner) }}'
# - name: Dump strategy context
# run: echo '${{ toJSON(strategy) }}'
# - name: Dump matrix context
# run: echo '${{ toJSON(matrix) }}'
# https://docs.github.com/cn/actions/learn-github-actions/expressions
transfer:
runs-on: ubuntu-latest
if: github.event_name == 'issue_comment' && contains('transfer', github.event.comment.body)
steps:
- name: Find runs comments
id: find-runs
uses: actions-cool/issues-helper@v3
with:
actions: 'find-comments'
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }}
comment-auth: 'github-actions[bot]'
body-includes: '${{ github.server_url }}/${{ github.repository }}/actions/runs'
- name: Downloads Artifacts
id: dl-artifacts
uses: actions/github-script@v6
with:
script: |
var fs = require('fs');
async function main() {
var runscomments = ${{ steps.find-runs.outputs.comments }};
console.log("runscomments: " + runscomments);
var runids = [];
for (i = 0; i < runscomments.length; i++) {
bodylines = runscomments.at(i)["body"].split("\n")
for (j = 0; j < bodylines.length; j++) {
if (bodylines.at(j).indexOf("runs/") != -1) {
let runid = bodylines.at(j).substring(bodylines.at(j).lastIndexOf('/') + 1).replace(/^\s+|\s+$/g, '');
runids.push({"id": runscomments.at(i)["id"], "runid": runid});
}
}
}
console.log("runids: " + runids);
// https://docs.github.com/cn/rest/actions/workflow-runs#delete-workflow-run-logs
// https://github.com/octokit/plugin-rest-endpoint-methods.js/blob/master/src/generated/endpoints.ts
var downloadeds = [];
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
if (runids.length > 0) {
for (index = 0; index < runids.length; index++) {
try {
if (false == downloadeds.includes(runids.at(index)["runid"])) {
// 获取附件
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: runids.at(index)["runid"],
per_page: 100,
});
console.log("artifacts: %s", artifacts.data);
if (artifacts.data.total_count > 0) {
// 下载附件 (正常只有一个)
for (const artifact of artifacts.data.artifacts) {
console.log(`artifact id ${artifact.id} of ${artifact.name}`)
const dlartifact = await github.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
console.log(`GET artifacts(${artifact.id}): ${dlartifact}`)
fs.mkdirSync(`${runids.at(index)["runid"]}`, {recursive:true}, (err)=>{ if(err){ throw err; }});
fs.writeFileSync(`${runids.at(index)["runid"]}/${artifact.name}.zip`, Buffer.from(dlartifact.data), (err)=>{ if(err){ throw err; }});
}
downloadeds.push(runids.at(index)["runid"]);
}
}
} catch (error) {
core.setFailed(error.message);
}
}
}
var dirs = '';
dirs = downloadeds.join(",");
core.setOutput("downloadeds", dirs);
}
main();
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Transfer Artifacts
run: |
# curl -sL https://git.io/file-transfer | sh
# ./transfer trs *.zip
echo 'info<<EOF' >> $GITHUB_ENV
if [ -n "${{ steps.dl-artifacts.outputs.downloadeds }}" ]; then
dls=${{ steps.dl-artifacts.outputs.downloadeds }}
ids=(${dls//,/ })
for item in ${ids[@]}
do
files=""
if [ -d ${item} ]; then
for file in `ls ${item}`
do
files="${files} -F filedata=@${item}/${file}"
done
fi
echo "### ${item}: " >> $GITHUB_ENV
if [ -n "${files}" ]; then
url=$(curl -w '\n' -i ${files} "https://transfer.sh" | sed 's/\r//g; /^$/d')
echo "- ${url} " >> $GITHUB_ENV
else
echo "- delete " >> $GITHUB_ENV
fi
done
else
echo '未找到编译成功的附件, 如果编译失败或者已删除(delete builds)请重新编译后再执行本操作. ' >> $GITHUB_ENV
fi
echo 'EOF' >> $GITHUB_ENV
- name: Create Issues Comment
uses: actions-cool/issues-helper@v3
with:
actions: 'create-comment'
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }}
body: '${{ env.info }}'
emoji: rocket
delete_builds:
runs-on: ubuntu-latest
if: github.event_name == 'issue_comment' && contains('delete builds', github.event.comment.body)
steps:
- name: Find bots comments
id: find-bots
uses: actions-cool/issues-helper@v3
with:
actions: 'find-comments'
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }}
comment-auth: 'github-actions[bot]'
- name: Find runs comments
id: find-runs
uses: actions-cool/issues-helper@v3
with:
actions: 'find-comments'
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }}
comment-auth: 'github-actions[bot]'
body-includes: '${{ github.server_url }}/${{ github.repository }}/actions/runs'
- name: Delete build Data
id: delete-build
uses: actions/github-script@v6
with:
script: |
async function main() {
var botscomments = ${{ steps.find-bots.outputs.comments }};
console.log("botscomments: " + botscomments);
var runscomments = ${{ steps.find-runs.outputs.comments }};
console.log("runscomments: " + runscomments);
var botids = [];
for (i = 0; i < botscomments.length; i++) {
botids.push(botscomments.at(i)["id"]);
}
console.log("botids: " + botids);
var runids = [];
for (i = 0; i < runscomments.length; i++) {
bodylines = runscomments.at(i)["body"].split("\n")
for (j = 0; j < bodylines.length; j++) {
if (bodylines.at(j).indexOf("runs/") != -1) {
let runid = bodylines.at(j).substring(bodylines.at(j).lastIndexOf('/') + 1).replace(/^\s+|\s+$/g, '');
runids.push({"id": runscomments.at(i)["id"], "runid": runid});
}
}
}
console.log("runids: " + runids);
// https://docs.github.com/cn/rest/actions/workflow-runs#delete-workflow-run-logs
// https://github.com/octokit/plugin-rest-endpoint-methods.js/blob/master/src/generated/endpoints.ts
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
if (runids.length > 0) {
var deleted = [];
for (index = 0; index < runids.length; index++) {
try {
if (false == deleted.includes(runids.at(index)["runid"])) {
// 删除日志
const dellogs = await github.rest.actions.deleteWorkflowRunLogs({
owner,
repo,
run_id: runids.at(index)["runid"],
});
console.log("DELETE logs: %s", dellogs);
// 获取附件
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: runids.at(index)["runid"],
per_page: 100,
});
console.log("artifacts: %s", artifacts.data);
// 删除附件
for (const artifact of artifacts.data.artifacts) {
console.log(`artifact id ${artifact.id} of ${artifact.name}`)
const delartifacts = await github.rest.actions.deleteArtifact({
owner,
repo,
artifact_id: artifact.id,
});
console.log(`DELETE artifacts(${artifact.id}): ${delartifacts}`)
}
deleted.push(runids.at(index)["runid"]);
}
} catch (error) {
core.setFailed(error.message);
}
}
}
if (botids.length > 0) {
for (index = 0; index < botids.length; index++) {
try {
// 更新评论
const retcomments = await github.rest.issues.updateComment({
owner,
repo,
comment_id: botids.at(index),
body: '`delete`'
})
console.log("PATCH emoji: %s", retcomments);
} catch (error) {
core.setFailed(error.message);
}
}
}
}
main();
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
set_schedule:
runs-on: ubuntu-latest
if: github.event_name == 'issue_comment' && contains(fromJson('["add schedule", "del schedule"]'), github.event.comment.body)
steps:
- name: Checkout
uses: actions/checkout@main
- name: Check Labels
id: check-comments
uses: actions/github-script@v6
with:
script: |
var labels = [];
var issuelabels = ${{ toJSON(github.event.issue.labels) }};
console.log("issuelabels: " + issuelabels);
for (i in issuelabels) {
labels.push(issuelabels[i]["name"]);
}
console.log("labels: " + labels);
var isSchedule = "false";
if (${{ toJSON(github.event.comment.body) }}.toLowerCase() == "add schedule") {
isSchedule = "true";
}
console.log("isSchedule: " + isSchedule);
if (isSchedule == "true") {
labels.push("schedule");
} else {
var index = labels.indexOf("schedule");
if (index > -1) labels.splice(index, 1);
}
core.setOutput("isSchedule", isSchedule);
core.setOutput("labels", labels.join(","));
- name: Set labels
uses: actions-cool/issues-helper@v3
with:
actions: 'set-labels'
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }}
labels: ${{ steps.check-comments.outputs.labels }}
checklabels:
runs-on: ubuntu-latest
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
steps:
- name: Checkout
uses: actions/checkout@main
- name: Find schedule issues
id: find-issues
uses: actions-cool/issues-helper@v3
with:
actions: 'find-issues'
token: ${{ secrets.GITHUB_TOKEN }}
issue-state: 'all'
#title-includes: 'custom'
labels: 'schedule'
- name: Get Issues Info
id: get-issues
uses: actions/github-script@v6
with:
script: |
var issues = ${{ toJSON(steps.find-issues.outputs.issues) }};
// Backspace,Tab,换行符,垂直制表符,换页,回车,不间断空格,行分隔符,段落分隔符,字节顺序标记
issues = issues.replace(/\u0008|\u0009|\u000A|\u000B|\u000C|\u000D|\u00A0|\u2028|\u2029|\uFEFF/g, "");
core.setOutput("issues", JSON.stringify(issues));
- name: Reopen Issues
shell: python
run: |
import os, json, requests
if __name__ == '__main__':
try:
issues = {}
issues = json.loads(${{ steps.get-issues.outputs.issues }})
if len(issues) > 0:
headers = {'Accept': 'application/vnd.github+json', 'Authorization': 'token ${{ secrets.ISSUES }}'}
data = '{"state": "open"}'
for item in issues:
try:
url='https://api.github.com/repos/{}/issues/{}'.format('${{ github.repository }}', item['number'])
res = requests.post(url=url, data=data, headers=headers)
print(url)
print(res.json())
except:
pass
except Exception as err:
print(err)