Skip to content

sync-build-deploy #2950

sync-build-deploy

sync-build-deploy #2950

# 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