-
Notifications
You must be signed in to change notification settings - Fork 0
232 lines (203 loc) · 8.94 KB
/
Copy pathversion-monitor.yml
File metadata and controls
232 lines (203 loc) · 8.94 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
name: Version Monitor
# Trigger the workflow every 30 minutes and allow manual triggering
on:
push:
branches:
- main
schedule:
- cron: "*/30 * * * *" # Run every 30 minutes
workflow_dispatch: # Allow manual trigger from GitHub Actions UI
# Set permissions for creating branches, commits, and pull requests
permissions:
contents: write
pull-requests: write
jobs:
monitor:
name: Monitor Version Changes
runs-on: ubuntu-latest
outputs:
update_needed: ${{ steps.monitor.outputs.update_needed }}
new_version: ${{ steps.monitor.outputs.new_version }}
pr_created: ${{ steps.pr.outputs.pr_created }}
pr_url: ${{ steps.pr.outputs.pr_url }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Display configuration
run: |
echo "Version Monitor Configuration"
echo "=========================="
echo "Source URL: ${{ vars.VERSION_SOURCE_URL || 'https://desktop.dl.hagicode.com/index.json' }}"
echo "Request Timeout: 30000ms"
echo "Max Retries: 3"
echo "Repository: ${{ github.repository }}"
- name: Run version monitor
id: monitor
run: node scripts/version-monitor.js
env:
VERSION_SOURCE_URL: ${{ vars.VERSION_SOURCE_URL || 'https://desktop.dl.hagicode.com/index.json' }}
REQUEST_TIMEOUT: "30000"
MAX_RETRIES: "3"
- name: Configure Git
if: steps.monitor.outputs.update_needed == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Check existing branch
id: check_branch
if: steps.monitor.outputs.update_needed == 'true'
run: |
BRANCH_NAME="version-update-${{ steps.monitor.outputs.new_version }}"
echo "Checking for existing branch: $BRANCH_NAME"
if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then
echo "branch_exists=true" >> $GITHUB_OUTPUT
echo "Branch $BRANCH_NAME already exists on remote"
else
echo "branch_exists=false" >> $GITHUB_OUTPUT
echo "Branch $BRANCH_NAME does not exist on remote"
fi
- name: Create feature branch
if: steps.monitor.outputs.update_needed == 'true' && steps.check_branch.outputs.branch_exists != 'true'
run: |
BRANCH_NAME="version-update-${{ steps.monitor.outputs.new_version }}"
git checkout -b "$BRANCH_NAME"
echo "Created branch: $BRANCH_NAME"
- name: Commit version changes
if: steps.monitor.outputs.update_needed == 'true' && steps.check_branch.outputs.branch_exists != 'true'
run: |
git add public/version-index.json
git commit -m "chore: update version to ${{ steps.monitor.outputs.new_version }}"
- name: Push branch
if: steps.monitor.outputs.update_needed == 'true' && steps.check_branch.outputs.branch_exists != 'true'
run: |
BRANCH_NAME="version-update-${{ steps.monitor.outputs.new_version }}"
git push origin "$BRANCH_NAME"
- name: Check existing PR
id: check_pr
if: steps.monitor.outputs.update_needed == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CURRENT_VERSION: ${{ steps.monitor.outputs.new_version }}
run: |
echo "Checking for existing PR for version: ${CURRENT_VERSION}"
EXISTING_PR=$(gh pr list \
--search "chore: update version to ${CURRENT_VERSION} in:head" \
--state open \
--json number,title \
--jq '.[0]')
if [ -n "$EXISTING_PR" ]; then
PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number')
PR_TITLE=$(echo "$EXISTING_PR" | jq -r '.title')
echo "pr_exists=true" >> $GITHUB_OUTPUT
echo "existing_pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
echo "existing_pr_url=$(gh pr view $PR_NUMBER --json url -q .url)" >> $GITHUB_OUTPUT
echo "Found existing PR: #$PR_NUMBER - $PR_TITLE"
else
echo "pr_exists=false" >> $GITHUB_OUTPUT
echo "No existing PR found for version ${CURRENT_VERSION}"
fi
- name: Close previous version PRs
id: close_prs
if: steps.monitor.outputs.update_needed == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CURRENT_VERSION: ${{ steps.monitor.outputs.new_version }}
run: |
echo "Closing previous version PRs (excluding current version: ${CURRENT_VERSION})..."
PREVIOUS_PRS=$(gh pr list \
--search "chore: update version to in:head" \
--state open \
--json number,title \
--jq '.[] | select(.title != "chore: update version to '"${CURRENT_VERSION}"'") | .number')
if [ -n "$PREVIOUS_PRS" ]; then
echo "$PREVIOUS_PRS" | while read -r pr_number; do
echo "Closing PR #$pr_number"
gh pr close "$pr_number" --comment "由于新的版本更新 PR 自动关闭"
done
else
echo "No previous version PRs to close"
fi
- name: Create Pull Request
id: pr
if: steps.monitor.outputs.update_needed == 'true' && steps.check_pr.outputs.pr_exists != 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
NEW_VERSION="${{ steps.monitor.outputs.new_version }}"
BRANCH_NAME="version-update-${NEW_VERSION}"
PR_BODY="## Version Update
This PR updates the version index to reflect the new version detected from the official website.
- **New Version**: ${NEW_VERSION}
- **Source**: ${{ vars.VERSION_SOURCE_URL || 'https://desktop.dl.hagicode.com/index.json' }}
- **Checked At**: $(date -u +"%Y-%m-%dT%H:%M:%SZ")
### Changes
- Updated \`public/version-index.json\` with the latest version data from online API
### Next Steps
After merging this PR, the CI/CD pipeline will automatically rebuild and deploy the documentation site with the updated version information.
---
_This PR was automatically created by the [Version Monitor](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) workflow._"
gh pr create \
--title "chore: update version to ${NEW_VERSION}" \
--body "$PR_BODY" \
--base main \
--head "$BRANCH_NAME" \
--label "automation,version"
echo "pr_created=true" >> $GITHUB_OUTPUT
echo "pr_url=$(gh pr view --json url -q .url)" >> $GITHUB_OUTPUT
- name: Display result
if: always()
run: |
echo "Version Monitor Result"
echo "======================"
echo "Update Needed: ${{ steps.monitor.outputs.update_needed }}"
echo "New Version: ${{ steps.monitor.outputs.new_version }}"
echo "Branch Exists: ${{ steps.check_branch.outputs.branch_exists }}"
echo "PR Exists: ${{ steps.check_pr.outputs.pr_exists }}"
echo "PR Created: ${{ steps.pr.outputs.pr_created }}"
if [ "${{ steps.check_pr.outputs.pr_exists }}" == "true" ]; then
echo "Existing PR: #${{ steps.check_pr.outputs.existing_pr_number }}"
echo "Existing PR URL: ${{ steps.check_pr.outputs.existing_pr_url }}"
else
echo "PR URL: ${{ steps.pr.outputs.pr_url }}"
fi
notify-version-update:
needs: monitor
if: needs.monitor.outputs.pr_created == 'true'
runs-on: ubuntu-latest
steps:
- uses: HagiCode-org/haginotifier@v1
with:
msg_type: 'post'
title: 'Version Monitor'
message: |
## Version Monitor
**状态**: 🔄 发现新版本
**新版本**: ${{ needs.monitor.outputs.new_version }}
**仓库**: ${{ github.repository }}
**PR 链接**: ${{ needs.monitor.outputs.pr_url }}
**运行详情**: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
env:
FEISHU_WEBHOOK_URL: ${{ secrets.FEISHU_WEBHOOK_URL }}
notify-failure:
needs: monitor
if: failure()
runs-on: ubuntu-latest
steps:
- uses: HagiCode-org/haginotifier@v1
with:
msg_type: 'post'
title: 'Version Monitor'
message: |
## Version Monitor
**状态**: ❌ 失败
**仓库**: ${{ github.repository }}
**触发者**: ${{ github.actor }}
**运行详情**: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
env:
FEISHU_WEBHOOK_URL: ${{ secrets.FEISHU_WEBHOOK_URL }}