-
Notifications
You must be signed in to change notification settings - Fork 0
268 lines (234 loc) · 9.41 KB
/
check-version.yml
File metadata and controls
268 lines (234 loc) · 9.41 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
name: Check Replay Version
on:
# 定时运行: 每4小时运行一次
schedule:
- cron: '0 */4 * * *'
# 支持手动触发
workflow_dispatch:
# 每次推送到 main 分支时也运行(可选)
push:
branches:
- main
paths:
- 'check_all_platforms.py'
- '.github/workflows/check-version.yml'
jobs:
check-version:
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# 需要获取完整历史以便提交更改
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run version check
id: version_check
run: |
python check_all_platforms.py
- name: Commit version file if changed
id: commit_check
continue-on-error: true
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# 检查是否有文件变化
if [[ -f all_platforms_versions.json ]]; then
# 先保存当前生成的版本文件
cp all_platforms_versions.json /tmp/new_version.json
# 拉取最新代码
echo "Pulling latest changes..."
git fetch origin main || {
echo "Fetch failed, skipping commit"
echo "version_updated=false" >> $GITHUB_OUTPUT
exit 0
}
# 重置到远程最新版本
git reset --hard origin/main
# 检查新生成的版本文件与远程是否有差异
if diff -q all_platforms_versions.json /tmp/new_version.json > /dev/null 2>&1; then
echo "No changes detected (files are identical)"
echo "version_updated=false" >> $GITHUB_OUTPUT
exit 0
fi
# 恢复新生成的版本文件
cp /tmp/new_version.json all_platforms_versions.json
git add all_platforms_versions.json
# 提交变化
git commit -m "chore: update Replay version info [skip ci]" || {
echo "Commit failed, skipping"
echo "version_updated=false" >> $GITHUB_OUTPUT
exit 0
}
# 推送到远程,如果失败就放弃(避免邮件通知)
if ! git push origin main 2>/dev/null; then
echo "Push failed (likely another workflow pushed first). This is expected, skipping."
echo "version_updated=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "Successfully pushed version update"
echo "version_updated=true" >> $GITHUB_OUTPUT
else
echo "version_updated=false" >> $GITHUB_OUTPUT
fi
- name: Create Issue on new version
if: steps.version_check.outputs.has_updates == 'true' && steps.commit_check.outputs.version_updated == 'true'
uses: actions/github-script@v7
with:
script: |
const updatesJson = process.env.UPDATES_JSON;
const updates = JSON.parse(updatesJson || '[]');
if (updates.length === 0) {
console.log('No updates to report');
return;
}
for (const update of updates) {
const title = `🚀 ${update.platform} 新版本 ${update.new_version}`;
let body = `## 🎉 检测到 ${update.platform} 新版本\n\n`;
body += `- 旧版本: ${update.old_version}\n`;
body += `- 新版本: ${update.new_version}\n`;
body += `- 官网下载: https://www.weights.com/replay?platform=${update.download_param}\n`;
if (update.download_url) {
body += `- 直接下载: ${update.download_url}\n`;
}
body += `\n---\n\n`;
body += `检测时间: ${new Date().toISOString()}\n\n`;
body += `此 Issue 由 GitHub Actions 自动创建。`;
const issues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all',
labels: 'version-update',
per_page: 100
});
const existingIssue = issues.data.find(issue =>
issue.title.includes(update.platform) && issue.title.includes(update.new_version)
);
if (!existingIssue) {
await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
body: body,
labels: ['version-update', 'automated', update.platform_key]
});
console.log(`✅ Issue created for ${update.platform}: ${update.new_version}`);
} else {
console.log(`ℹ️ Issue already exists for ${update.platform} ${update.new_version}`);
}
}
env:
UPDATES_JSON: ${{ steps.version_check.outputs.updates_json }}
- name: Send Feishu notification
if: steps.version_check.outputs.has_updates == 'true' && steps.commit_check.outputs.version_updated == 'true'
env:
FEISHU_WEBHOOK_URL: ${{ secrets.FEISHU_WEBHOOK_URL }}
UPDATES_JSON: ${{ steps.version_check.outputs.updates_json }}
run: |
if [ -z "$FEISHU_WEBHOOK_URL" ]; then
echo "⚠️ FEISHU_WEBHOOK_URL 未配置,跳过飞书通知"
exit 0
fi
# 构建飞书消息卡片
TIMESTAMP=$(TZ='Asia/Shanghai' date "+%Y-%m-%d %H:%M:%S")
# 生成每个平台的更新元素
PLATFORM_ELEMENTS=$(echo "$UPDATES_JSON" | jq -c '.[]' | while IFS= read -r update; do
platform=$(echo "$update" | jq -r '.platform')
old_version=$(echo "$update" | jq -r '.old_version')
new_version=$(echo "$update" | jq -r '.new_version')
download_param=$(echo "$update" | jq -r '.download_param')
download_url=$(echo "$update" | jq -r '.download_url // ""')
# 构建下载链接部分
if [ -n "$download_url" ]; then
download_links="[官网下载](https://www.weights.com/replay?platform=${download_param}) | [直接下载](${download_url})"
else
download_links="[官网下载](https://www.weights.com/replay?platform=${download_param})"
fi
# 输出平台卡片元素 (使用 jq 构建 JSON)
jq -n --arg platform "$platform" \
--arg old_ver "$old_version" \
--arg new_ver "$new_version" \
--arg links "$download_links" \
'{
tag: "div",
text: {
tag: "lark_md",
content: ("**" + $platform + "**\n🔄 " + $old_ver + " → " + $new_ver + "\n📥 " + $links)
}
}'
echo '{"tag":"hr"}'
done | jq -s '.') # 将所有元素合并为数组
# 构建完整消息 (使用 jq)
GITHUB_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
echo "$PLATFORM_ELEMENTS" | jq --arg ts "$TIMESTAMP" --arg url "$GITHUB_URL" '{
msg_type: "interactive",
card: {
header: {
title: {
tag: "plain_text",
content: "🎉 Replay 新版本更新通知"
},
template: "blue"
},
elements: ([
{
tag: "div",
text: {
tag: "lark_md",
content: "检测到以下平台有新版本发布:"
}
},
{
tag: "hr"
}
] + (.[0:length-1]) + [
{
tag: "note",
elements: [
{
tag: "plain_text",
content: ("检测时间: " + $ts)
}
]
},
{
tag: "action",
actions: [
{
tag: "button",
text: {
tag: "plain_text",
content: "查看详情"
},
type: "primary",
url: $url
}
]
}
])
}
}' > /tmp/feishu_message.json
# 发送到飞书
HTTP_CODE=$(curl -s -o /tmp/feishu_response.txt -w "%{http_code}" \
-X POST "$FEISHU_WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d @/tmp/feishu_message.json)
if [ "$HTTP_CODE" = "200" ]; then
echo "✅ 飞书通知发送成功"
cat /tmp/feishu_response.txt
else
echo "❌ 飞书通知发送失败 (HTTP $HTTP_CODE)"
cat /tmp/feishu_response.txt
exit 1
fi