ci: run wiki badge update after wiki sync (#60) #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Wiki Badge Count | |
| on: | |
| workflow_run: | |
| workflows: ["Sync GitHub Wiki"] | |
| types: [completed] | |
| push: | |
| branches: [main] | |
| paths: | |
| - ".github/workflows/wiki-page-count-badge.yml" | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| update-wiki-badge: | |
| if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Clone GitHub Wiki repository | |
| run: | | |
| git clone "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.wiki.git" wiki-repo | |
| - name: Count wiki pages | |
| id: count | |
| run: | | |
| COUNT="$(find wiki-repo -maxdepth 1 -type f -name '*.md' | wc -l | tr -d ' ')" | |
| echo "count=$COUNT" >> "$GITHUB_OUTPUT" | |
| - name: Update README wiki badge | |
| env: | |
| WIKI_COUNT: ${{ steps.count.outputs.count }} | |
| run: | | |
| python - <<'PY' | |
| import os | |
| import re | |
| from pathlib import Path | |
| count = os.environ["WIKI_COUNT"].strip() | |
| readme_path = Path("README.md") | |
| text = readme_path.read_text(encoding="utf-8") | |
| pattern = re.compile( | |
| r"(\[!\[Wiki - )\d+( pages\]\(https://img\.shields\.io/badge/wiki-)\d+(%20pages-[^)]+\)\]\(https://github\.com/[^/]+/[^/]+/wiki\))" | |
| ) | |
| updated = pattern.sub(rf"\\g<1>{count}\\g<2>{count}\\g<3>", text) | |
| if updated != text: | |
| readme_path.write_text(updated, encoding="utf-8") | |
| print(f"Updated wiki badge count to {count}") | |
| else: | |
| print("README badge already up to date") | |
| PY | |
| - name: Create pull request for badge update | |
| id: cpr | |
| uses: peter-evans/create-pull-request@v7 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| commit-message: "chore(readme): update wiki badge page count to ${{ steps.count.outputs.count }}" | |
| title: "chore(readme): update wiki badge page count to ${{ steps.count.outputs.count }}" | |
| body: "Automated update of the wiki page count badge in README.md." | |
| labels: "automation" | |
| branch: "automation/wiki-page-count-badge" | |
| delete-branch: true | |
| - name: Enable auto-merge for badge PR | |
| if: steps.cpr.outputs.pull-request-number != '' | |
| uses: peter-evans/enable-pull-request-automerge@v3 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| pull-request-number: ${{ steps.cpr.outputs.pull-request-number }} | |
| merge-method: squash |