|
| 1 | +name: Version Monitor |
| 2 | + |
| 3 | +# Trigger the workflow every 30 minutes and allow manual triggering |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + schedule: |
| 9 | + - cron: "*/30 * * * *" # Run every 30 minutes |
| 10 | + workflow_dispatch: # Allow manual trigger from GitHub Actions UI |
| 11 | + |
| 12 | +# Set permissions for creating branches, commits, and pull requests |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + monitor: |
| 19 | + name: Monitor Version Changes |
| 20 | + runs-on: ubuntu-latest |
| 21 | + outputs: |
| 22 | + update_needed: ${{ steps.monitor.outputs.update_needed }} |
| 23 | + new_version: ${{ steps.monitor.outputs.new_version }} |
| 24 | + pr_created: ${{ steps.pr.outputs.pr_created }} |
| 25 | + pr_url: ${{ steps.pr.outputs.pr_url }} |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + |
| 33 | + - name: Setup Node.js |
| 34 | + uses: actions/setup-node@v4 |
| 35 | + with: |
| 36 | + node-version: "20" |
| 37 | + |
| 38 | + - name: Display configuration |
| 39 | + run: | |
| 40 | + echo "Version Monitor Configuration" |
| 41 | + echo "==========================" |
| 42 | + echo "Source URL: ${{ vars.VERSION_SOURCE_URL || 'https://desktop.dl.hagicode.com/index.json' }}" |
| 43 | + echo "Request Timeout: 30000ms" |
| 44 | + echo "Max Retries: 3" |
| 45 | + echo "Repository: ${{ github.repository }}" |
| 46 | +
|
| 47 | + - name: Run version monitor |
| 48 | + id: monitor |
| 49 | + run: node scripts/version-monitor.js |
| 50 | + env: |
| 51 | + VERSION_SOURCE_URL: ${{ vars.VERSION_SOURCE_URL || 'https://desktop.dl.hagicode.com/index.json' }} |
| 52 | + REQUEST_TIMEOUT: "30000" |
| 53 | + MAX_RETRIES: "3" |
| 54 | + |
| 55 | + - name: Configure Git |
| 56 | + if: steps.monitor.outputs.update_needed == 'true' |
| 57 | + run: | |
| 58 | + git config --global user.name "github-actions[bot]" |
| 59 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 60 | +
|
| 61 | + - name: Check existing branch |
| 62 | + id: check_branch |
| 63 | + if: steps.monitor.outputs.update_needed == 'true' |
| 64 | + run: | |
| 65 | + BRANCH_NAME="version-update-${{ steps.monitor.outputs.new_version }}" |
| 66 | + echo "Checking for existing branch: $BRANCH_NAME" |
| 67 | + if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then |
| 68 | + echo "branch_exists=true" >> $GITHUB_OUTPUT |
| 69 | + echo "Branch $BRANCH_NAME already exists on remote" |
| 70 | + else |
| 71 | + echo "branch_exists=false" >> $GITHUB_OUTPUT |
| 72 | + echo "Branch $BRANCH_NAME does not exist on remote" |
| 73 | + fi |
| 74 | +
|
| 75 | + - name: Create feature branch |
| 76 | + if: steps.monitor.outputs.update_needed == 'true' && steps.check_branch.outputs.branch_exists != 'true' |
| 77 | + run: | |
| 78 | + BRANCH_NAME="version-update-${{ steps.monitor.outputs.new_version }}" |
| 79 | + git checkout -b "$BRANCH_NAME" |
| 80 | + echo "Created branch: $BRANCH_NAME" |
| 81 | +
|
| 82 | + - name: Commit version changes |
| 83 | + if: steps.monitor.outputs.update_needed == 'true' && steps.check_branch.outputs.branch_exists != 'true' |
| 84 | + run: | |
| 85 | + git add public/version-index.json |
| 86 | + git commit -m "chore: update version to ${{ steps.monitor.outputs.new_version }}" |
| 87 | +
|
| 88 | + - name: Push branch |
| 89 | + if: steps.monitor.outputs.update_needed == 'true' && steps.check_branch.outputs.branch_exists != 'true' |
| 90 | + run: | |
| 91 | + BRANCH_NAME="version-update-${{ steps.monitor.outputs.new_version }}" |
| 92 | + git push origin "$BRANCH_NAME" |
| 93 | +
|
| 94 | + - name: Check existing PR |
| 95 | + id: check_pr |
| 96 | + if: steps.monitor.outputs.update_needed == 'true' |
| 97 | + env: |
| 98 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + CURRENT_VERSION: ${{ steps.monitor.outputs.new_version }} |
| 100 | + run: | |
| 101 | + echo "Checking for existing PR for version: ${CURRENT_VERSION}" |
| 102 | + EXISTING_PR=$(gh pr list \ |
| 103 | + --search "chore: update version to ${CURRENT_VERSION} in:head" \ |
| 104 | + --state open \ |
| 105 | + --json number,title \ |
| 106 | + --jq '.[0]') |
| 107 | +
|
| 108 | + if [ -n "$EXISTING_PR" ]; then |
| 109 | + PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number') |
| 110 | + PR_TITLE=$(echo "$EXISTING_PR" | jq -r '.title') |
| 111 | + echo "pr_exists=true" >> $GITHUB_OUTPUT |
| 112 | + echo "existing_pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 113 | + echo "existing_pr_url=$(gh pr view $PR_NUMBER --json url -q .url)" >> $GITHUB_OUTPUT |
| 114 | + echo "Found existing PR: #$PR_NUMBER - $PR_TITLE" |
| 115 | + else |
| 116 | + echo "pr_exists=false" >> $GITHUB_OUTPUT |
| 117 | + echo "No existing PR found for version ${CURRENT_VERSION}" |
| 118 | + fi |
| 119 | +
|
| 120 | + - name: Close previous version PRs |
| 121 | + id: close_prs |
| 122 | + if: steps.monitor.outputs.update_needed == 'true' |
| 123 | + env: |
| 124 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 125 | + CURRENT_VERSION: ${{ steps.monitor.outputs.new_version }} |
| 126 | + run: | |
| 127 | + echo "Closing previous version PRs (excluding current version: ${CURRENT_VERSION})..." |
| 128 | + PREVIOUS_PRS=$(gh pr list \ |
| 129 | + --search "chore: update version to in:head" \ |
| 130 | + --state open \ |
| 131 | + --json number,title \ |
| 132 | + --jq '.[] | select(.title != "chore: update version to '"${CURRENT_VERSION}"'") | .number') |
| 133 | +
|
| 134 | + if [ -n "$PREVIOUS_PRS" ]; then |
| 135 | + echo "$PREVIOUS_PRS" | while read -r pr_number; do |
| 136 | + echo "Closing PR #$pr_number" |
| 137 | + gh pr close "$pr_number" --comment "由于新的版本更新 PR 自动关闭" |
| 138 | + done |
| 139 | + else |
| 140 | + echo "No previous version PRs to close" |
| 141 | + fi |
| 142 | +
|
| 143 | + - name: Create Pull Request |
| 144 | + id: pr |
| 145 | + if: steps.monitor.outputs.update_needed == 'true' && steps.check_pr.outputs.pr_exists != 'true' |
| 146 | + env: |
| 147 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 148 | + run: | |
| 149 | + NEW_VERSION="${{ steps.monitor.outputs.new_version }}" |
| 150 | + BRANCH_NAME="version-update-${NEW_VERSION}" |
| 151 | + PR_BODY="## Version Update |
| 152 | +
|
| 153 | + This PR updates the version index to reflect the new version detected from the official website. |
| 154 | +
|
| 155 | + - **New Version**: ${NEW_VERSION} |
| 156 | + - **Source**: ${{ vars.VERSION_SOURCE_URL || 'https://desktop.dl.hagicode.com/index.json' }} |
| 157 | + - **Checked At**: $(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 158 | +
|
| 159 | + ### Changes |
| 160 | + - Updated \`public/version-index.json\` with the latest version data from online API |
| 161 | +
|
| 162 | + ### Next Steps |
| 163 | + After merging this PR, the CI/CD pipeline will automatically rebuild and deploy the documentation site with the updated version information. |
| 164 | +
|
| 165 | + --- |
| 166 | + _This PR was automatically created by the [Version Monitor](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) workflow._" |
| 167 | +
|
| 168 | + gh pr create \ |
| 169 | + --title "chore: update version to ${NEW_VERSION}" \ |
| 170 | + --body "$PR_BODY" \ |
| 171 | + --base main \ |
| 172 | + --head "$BRANCH_NAME" \ |
| 173 | + --label "automation,version" |
| 174 | +
|
| 175 | + echo "pr_created=true" >> $GITHUB_OUTPUT |
| 176 | + echo "pr_url=$(gh pr view --json url -q .url)" >> $GITHUB_OUTPUT |
| 177 | +
|
| 178 | + - name: Display result |
| 179 | + if: always() |
| 180 | + run: | |
| 181 | + echo "Version Monitor Result" |
| 182 | + echo "======================" |
| 183 | + echo "Update Needed: ${{ steps.monitor.outputs.update_needed }}" |
| 184 | + echo "New Version: ${{ steps.monitor.outputs.new_version }}" |
| 185 | + echo "Branch Exists: ${{ steps.check_branch.outputs.branch_exists }}" |
| 186 | + echo "PR Exists: ${{ steps.check_pr.outputs.pr_exists }}" |
| 187 | + echo "PR Created: ${{ steps.pr.outputs.pr_created }}" |
| 188 | + if [ "${{ steps.check_pr.outputs.pr_exists }}" == "true" ]; then |
| 189 | + echo "Existing PR: #${{ steps.check_pr.outputs.existing_pr_number }}" |
| 190 | + echo "Existing PR URL: ${{ steps.check_pr.outputs.existing_pr_url }}" |
| 191 | + else |
| 192 | + echo "PR URL: ${{ steps.pr.outputs.pr_url }}" |
| 193 | + fi |
| 194 | +
|
| 195 | + notify-version-update: |
| 196 | + needs: monitor |
| 197 | + if: needs.monitor.outputs.pr_created == 'true' |
| 198 | + runs-on: ubuntu-latest |
| 199 | + steps: |
| 200 | + - uses: HagiCode-org/haginotifier@v1 |
| 201 | + with: |
| 202 | + msg_type: 'post' |
| 203 | + title: 'Version Monitor' |
| 204 | + message: | |
| 205 | + ## Version Monitor |
| 206 | +
|
| 207 | + **状态**: 🔄 发现新版本 |
| 208 | + **新版本**: ${{ needs.monitor.outputs.new_version }} |
| 209 | + **仓库**: ${{ github.repository }} |
| 210 | + **PR 链接**: ${{ needs.monitor.outputs.pr_url }} |
| 211 | + **运行详情**: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 212 | + env: |
| 213 | + FEISHU_WEBHOOK_URL: ${{ secrets.FEISHU_WEBHOOK_URL }} |
| 214 | + |
| 215 | + notify-failure: |
| 216 | + needs: monitor |
| 217 | + if: failure() |
| 218 | + runs-on: ubuntu-latest |
| 219 | + steps: |
| 220 | + - uses: HagiCode-org/haginotifier@v1 |
| 221 | + with: |
| 222 | + msg_type: 'post' |
| 223 | + title: 'Version Monitor' |
| 224 | + message: | |
| 225 | + ## Version Monitor |
| 226 | +
|
| 227 | + **状态**: ❌ 失败 |
| 228 | + **仓库**: ${{ github.repository }} |
| 229 | + **触发者**: ${{ github.actor }} |
| 230 | + **运行详情**: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 231 | + env: |
| 232 | + FEISHU_WEBHOOK_URL: ${{ secrets.FEISHU_WEBHOOK_URL }} |
0 commit comments