forked from ZGUATION-PROJECTS/ZG-R
-
Notifications
You must be signed in to change notification settings - Fork 0
224 lines (185 loc) · 7.45 KB
/
sync_build_deploy.yml
File metadata and controls
224 lines (185 loc) · 7.45 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
# Credits goes to https://github.com/misak10
name: sync-build-deploy
on:
schedule:
- cron: '0 */4 * * *'
workflow_dispatch:
inputs:
run_sync:
description: "Run Sync"
type: choice
required: true
default: 'No'
options:
- 'Yes'
- 'No'
permissions:
contents: write
issues: write
pull-requests: write
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
env:
IS_SYNC: ${{ inputs.run_sync == 'Yes' || github.event_name == 'schedule' }}
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Set up Git
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mmrl-util
- name: Sync
if: ${{ env.IS_SYNC == 'true' }}
run: |
mmrl-util sync --diff versions_diff.md
- name: Clean old versions
run: |
# 清理旧版本,只保留最新的两个版本
for module_dir in modules/*/; do
if [ -d "$module_dir" ]; then
module_name=$(basename "$module_dir")
echo "Processing module: $module_name"
# 从 update.json 读取版本历史
if [ -f "${module_dir}update.json" ]; then
# 创建临时文件存储版本信息
temp_file=$(mktemp)
# 提取版本信息到临时文件并按时间戳排序
jq -r '.versions | sort_by(.timestamp) | reverse | .[] | "\(.timestamp) \(.version)"' "${module_dir}update.json" > "$temp_file"
# 获取版本总数
total_versions=$(wc -l < "$temp_file")
if [ "$total_versions" -gt 2 ]; then
echo "Found $total_versions versions for $module_name"
# 获取要保留的版本
keep_v1=$(sed -n '1p' "$temp_file" | cut -d' ' -f2)
keep_v2=$(sed -n '2p' "$temp_file" | cut -d' ' -f2)
echo "Keeping versions: $keep_v1, $keep_v2"
# 获取要删除的版本
while read -r timestamp version; do
if [ "$version" != "$keep_v1" ] && [ "$version" != "$keep_v2" ]; then
echo "Removing old version: $version"
# 删除版本目录(如果存在)
if [ -d "${module_dir}${version}" ]; then
rm -rf "${module_dir}${version}"
git add -u "${module_dir}${version}" || true
fi
# 删除 zip 和 md 文件(尝试所有可能的格式)
if [[ "$version" == v* ]]; then
# 如果版本号带v前缀
pure_version="${version#v}"
[ -f "${module_dir}${pure_version}.zip" ] && rm -f "${module_dir}${pure_version}.zip" && git add -u "${module_dir}${pure_version}.zip" || true
[ -f "${module_dir}${pure_version}.md" ] && rm -f "${module_dir}${pure_version}.md" && git add -u "${module_dir}${pure_version}.md" || true
else
# 如果版本号不带v前缀
[ -f "${module_dir}${version}.zip" ] && rm -f "${module_dir}${version}.zip" && git add -u "${module_dir}${version}.zip" || true
[ -f "${module_dir}${version}.md" ] && rm -f "${module_dir}${version}.md" && git add -u "${module_dir}${version}.md" || true
fi
# 从 update.json 中移除旧版本
jq --arg ver "$version" '.versions |= map(select(.version != $ver))' "${module_dir}update.json" > "${module_dir}update.json.tmp"
mv "${module_dir}update.json.tmp" "${module_dir}update.json"
git add "${module_dir}update.json"
fi
done < "$temp_file"
else
echo "No old versions to clean for $module_name (found $total_versions versions)"
fi
# 清理临时文件
rm -f "$temp_file"
else
echo "Warning: update.json not found for $module_name"
fi
fi
done
# 提交更改
if git diff --staged --quiet; then
echo "No changes to commit"
else
current_time=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
git commit -m "chore: Clean old module versions
🤖 Auto-generated by GitHub Actions
🕒 Time: $current_time
- Remove old module versions
- Keep latest two versions for each module
- Update update.json files"
# 推送更改
git push
fi
- name: Write versions diff to summary
if: ${{ env.IS_SYNC == 'true' }}
run: |
if [ -f versions_diff.md ]; then
echo "## Versions Diff" >> $GITHUB_STEP_SUMMARY
echo "$(cat versions_diff.md)" >> $GITHUB_STEP_SUMMARY
rm versions_diff.md
fi
- name: Write latest versions to summary
if: ${{ env.IS_SYNC != 'true' }}
run: |
mmrl-util index --list > latest_versions.md
echo "## Latest Versions" >> $GITHUB_STEP_SUMMARY
echo "$(cat latest_versions.md)" >> $GITHUB_STEP_SUMMARY
rm latest_versions.md
- name: Index and Push
if: ${{ env.IS_SYNC == 'true' }}
run: |
current_time=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
mmrl-util index --push
git diff --quiet && git diff --staged --quiet || git commit -m "chore(sync): Update repository data
🤖 Auto-generated by GitHub Actions
🕒 Time: $current_time
- Sync repository data
- Update module index
- Generate static pages
"
git push
- name: Upload logs
uses: actions/upload-artifact@v4
with:
name: logs
path: log/*.log
- name: Waiting till first deploy ends
if: ${{ env.IS_SYNC == 'true' }}
run: |
sleep 30
build:
runs-on: ubuntu-latest
needs: sync
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Build with Jekyll
uses: actions/jekyll-build-pages@v1
with:
source: ./
destination: ./_site
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4