Skip to content

Version Monitor

Version Monitor #552

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 }}