-
-
Notifications
You must be signed in to change notification settings - Fork 7
103 lines (87 loc) · 3.31 KB
/
Copy pathupdate_release_json.yml
File metadata and controls
103 lines (87 loc) · 3.31 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
name: 自动更新 update.json
on:
release:
types: [published]
workflow_dispatch:
inputs:
dry_run:
description: '只测试解析,不修改文件'
type: boolean
default: true
permissions:
contents: write
jobs:
update-json:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
ref: main
- name: Fetch latest release (manual trigger only)
if: github.event_name == 'workflow_dispatch'
id: fetch_release
env:
GH_TOKEN: ${{ github.token }}
run: |
# 从 GitHub API 获取最新 Release
release=$(gh api repos/${{ github.repository }}/releases/latest)
echo "tag=$(echo "$release" | jq -r '.tag_name')" >> $GITHUB_OUTPUT
echo "$release" | jq -r '.body' > /tmp/release_body.txt
- name: Parse release and update JSON
env:
RELEASE_TAG: ${{ github.event.release.tag_name || steps.fetch_release.outputs.tag }}
IS_DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
run: |
# 获取 body
if [ -f /tmp/release_body.txt ]; then
RELEASE_BODY=$(cat /tmp/release_body.txt)
else
RELEASE_BODY="${{ github.event.release.body }}"
fi
set -e
echo "Version: $RELEASE_TAG"
echo "Body:"
echo "$RELEASE_BODY"
echo "---"
# Parse EN changelog (between ## EN and ## ZH)
en_lines=$(echo "$RELEASE_BODY" | tr -d '\r' | sed -n '/^##* *EN/,/^##* *ZH/p' | grep '^- ' | sed 's/^- //' || true)
# Parse ZH changelog (after ## ZH)
zh_lines=$(echo "$RELEASE_BODY" | tr -d '\r' | sed -n '/^##* *ZH/,$p' | grep '^- ' | sed 's/^- //' || true)
echo "ZH changelog:"
echo "$zh_lines"
echo "EN changelog:"
echo "$en_lines"
# Convert to JSON arrays
zh_json=$(echo "$zh_lines" | jq -R -s 'split("\n") | map(select(length > 0))')
en_json=$(echo "$en_lines" | jq -R -s 'split("\n") | map(select(length > 0))')
# Create update.json
jq -n \
--arg version "$RELEASE_TAG" \
--argjson cn "$zh_json" \
--argjson en "$en_json" \
'{
version: $version,
changelog: {
CN: $cn,
EN: $en
}
}' > update/update.json
echo "Generated update.json:"
cat update/update.json
# Dry run 模式不保存文件
if [ "$IS_DRY_RUN" = "true" ]; then
echo ""
echo "=== DRY RUN MODE - 不修改文件 ==="
git checkout update/update.json
fi
- name: Commit and push
if: github.event.inputs.dry_run != 'true'
run: |
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add update/update.json
git diff-index --quiet HEAD || git commit -m "工作流:自动更新 update.json 至 ${{ github.event.release.tag_name }}"
git fetch origin main
git rebase origin/main || echo "Rebase completed"
git push origin main