forked from northsea4/mdcx-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
203 lines (175 loc) · 6.9 KB
/
watch-mdcx.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
name: Watch MDCx Release
on:
push:
branches:
- none
schedule:
# 每30分钟
- cron: '*/30 * * * *'
workflow_dispatch:
inputs:
isDev:
description: Is Dev
type: boolean
env:
RELEASE_URL: 'https://api.github.com/repos/sqzw-x/mdcx/releases/latest'
ENABLE_WATCH: ${{ secrets.ENABLE_WATCH_MDCX_RELEASE }}
GITHUB_CURRENT_REPO: ${{ github.event.repository.name }}
GITHUB_API_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
GITHUB_OWNER: ${{ github.repository_owner }}
ENABLE_TG_NOTIFICATION: ${{ secrets.ENABLE_TG_NOTIFICATION }}
ENABLE_TG_VERBOSE_NOTIFICATION: ${{ secrets.ENABLE_TG_VERBOSE_NOTIFICATION }}
SCHEDULE_INTERVAL: 30
jobs:
# 检查是否有新版本
# TODO 清理N天前的runs
watch:
runs-on: ubuntu-latest
steps:
-
run: |
# TODO 在job上貌似不能读取env或者secrets
if [[ "${{ env.ENABLE_WATCH }}" != "true" && "${{ github.event.inputs.isDev }}" != "true" ]]; then
exit 1
fi
-
name: Set timezone
uses: szenius/[email protected]
with:
timezoneLinux: "Asia/Shanghai"
-
name: Checkout
uses: actions/checkout@v3
-
name: Install apt packages
run: sudo apt-get install -y jq
-
name: Check if there is a new MDCx release
id: check
env:
GITHUB_CURRENT_REPO: ${{ env.GITHUB_CURRENT_REPO }}
GITHUB_API_TOKEN: ${{ env.GITHUB_API_TOKEN }}
CURL_VERBOSE: '-s'
run: |
source scripts/base.sh
source scripts/github.sh
source scripts/release-utils.sh
VAR_VERSION="MDCX_LATEST_VERSION"
VAR_TIME="MDCX_LATEST_TIME"
REPO="sqzw-x/mdcx"
TAG_NAME="daily_release"
REPO="sqzw-x/mdcx"
TAG_NAME="daily_release"
info=$(get_release_info "$REPO" "$TAG_NAME")
if [[ $? -ne 0 ]]; then
echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!"
exit 1
else
echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!"
fi
echo $info | jq
# 发布时间
published_at=$(printf '%s' $info | jq -r ".published_at")
echo "📅 发布时间: $published_at"
# 版本号
release_version=$(printf '%s' $info | jq -r ".release_version")
echo "🔢 版本号: $release_version"
newVersion=""
newTime=""
hasNewVersion=false
# 获取最新版本信息
newVersion="$release_version"
newTime="$published_at"
# 获取已记录的版本信息
latestVersion=$(getVariable $VAR_VERSION)
latestTime=$(getVariable $VAR_TIME)
echo "📦 已记录的最新版本:$latestVersion"
echo "📅 已记录的更新时间:$latestTime"
# 任意一个变量为空,都不视为新版本
if [[ -z "$latestVersion" || -z "$latestTime" ]]; then
hasNewVersion=false
else
# 如果版本号或者更新时间不同,视为新版本
if [[ "$newVersion" != "$latestVersion" || "$newTime" != "$latestTime" ]]; then
hasNewVersion=true
else
hasNewVersion=false
fi
fi
# 输出到GITHUB_OUTPUT
echo "newVersion=$newVersion" >> $GITHUB_OUTPUT
echo "newTime=$newTime" >> $GITHUB_OUTPUT
echo "hasNewVersion=$hasNewVersion" >> $GITHUB_OUTPUT
echo "latestVersion=$latestVersion" >> $GITHUB_OUTPUT
echo "latestTime=$latestTime" >> $GITHUB_OUTPUT
# 当前时间
echo "currentTime=$(date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
# 下次检查时间
echo "nextCheckTime=$(date -d "+${{ env.SCHEDULE_INTERVAL }} minutes" '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
if [[ "$hasNewVersion" == "true" ]]; then
echo "🎉 发现新版本!"
echo "📦 新版本号:$newVersion"
echo "📅 更新时间:$newTime"
updateVariable $VAR_TIME "$newTime"
updateVariable $VAR_VERSION "$newVersion"
exit 0
else
echo "🔎 没有新版本,等待下一次检查..."
fi
-
name: TG Notification - For new version
uses: appleboy/telegram-action@master
if: ${{ steps.check.outputs.hasNewVersion == 'true' && env.ENABLE_TG_NOTIFICATION == 'true' }}
with:
to: ${{ secrets.TELE_CHAT_ID }}
token: ${{ secrets.TELE_BOT_TOKEN }}
message: |
🎉 MDCx新版本!
📦 新版本号:${{ steps.check.outputs.newVersion }}
📅 更新时间:${{ steps.check.outputs.newTime }}
🔗 发布链接:https://github.com/sqzw-x/mdcx/releases/tag/latest
-
name: TG Notification - For no new version
uses: appleboy/telegram-action@master
if: ${{ steps.check.outputs.hasNewVersion == 'false' && env.ENABLE_TG_VERBOSE_NOTIFICATION == 'true' }}
with:
to: ${{ secrets.TELE_CHAT_ID }}
token: ${{ secrets.TELE_BOT_TOKEN }}
message: |
🔔 MDCx新版本检查通知 - 没有新版本
📦 最新版本:${{ steps.check.outputs.latestVersion }}
📅 发布时间:${{ steps.check.outputs.latestTime }}
⏰ 检查时间:${{ steps.check.outputs.currentTime }}
⏰ 下次检查:${{ steps.check.outputs.nextCheckTime }}
-
name: Trigger "Image build-mdcx CI"
if: ${{ steps.check.outputs.hasNewVersion == 'true' }}
run: |
# Personal Access Token
TOKEN="${{ env.GITHUB_API_TOKEN }}"
# GitHub Repo名称
REPO="${{ env.GITHUB_OWNER }}/${{ env.GITHUB_CURRENT_REPO }}"
# Workflow Name
WORKFLOW_NAME="Image build-mdcx CI"
# 根据名称获取workflow id
WORKFLOW_ID=$(curl -s -H "Authorization: Bearer ${TOKEN}" \
"https://api.github.com/repos/${REPO}/actions/workflows" \
| jq -r ".workflows | .[] | select(.name==\"${WORKFLOW_NAME}\") | .id")
if [[ -z "$WORKFLOW_ID" ]]; then
echo "❌ 获取workflow id失败!"
exit 1
else
echo "📦 workflow id:$WORKFLOW_ID"
fi
echo "🚀 触发构建..."
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${TOKEN}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${REPO}/actions/workflows/${WORKFLOW_ID}/dispatches \
-d '{"ref":"main","inputs":{"stage":"prod","baseImage":"latest"}}'
if [[ $? -ne 0 ]]; then
echo "❌ 触发构建失败!"
exit 1
fi