Refactor wiki sync workflow for clarity and efficiency #33
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: Sync Wiki | |
| on: | |
| push: | |
| branches: [ main ] | |
| paths: | |
| - 'docs/wiki/**' | |
| - '.github/workflows/wiki-sync.yml' | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| sync: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| - name: Build wiki copy (strip .md only in copy) | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| build_dir="_wikibuild" | |
| rm -rf "$build_dir"; mkdir -p "$build_dir" | |
| cp -R docs/wiki/. "$build_dir"/ || true | |
| [ -f "$build_dir/Home.md" ] || printf '# Home\n\n' > "$build_dir/Home.md" | |
| echo "== BEFORE ==" | |
| grep -R --line-number -E '\]\([^)]+\.md(#[^)]*)?\)' "$build_dir" || true | |
| # Remove .md ONLY for local links (no scheme, no slash); keep ../README.md etc. | |
| find "$build_dir" -type f -name '*.md' -print0 | xargs -0 -I{} \ | |
| perl -0777 -i -pe 's/\((?!https?:\/\/)([^)\/\s#?]+)\.md(\#[^)]+|\))/(\1\2/g' {} | |
| echo "== AFTER ==" | |
| grep -R --line-number -E '\]\([^)]+\.md(#[^)]*)?\)' "$build_dir" || true | |
| - name: Push to GitHub Wiki | |
| shell: bash | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| set -euo pipefail | |
| cd _wikibuild | |
| git init | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git add -A | |
| if git diff --cached --quiet; then | |
| echo "No wiki updates." | |
| exit 0 | |
| fi | |
| git commit -m "Sync wiki from docs/wiki at $GITHUB_SHA" | |
| git remote add origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPO}.wiki.git" | |
| # Detect the wiki repo's default branch (main/master/etc.) | |
| DEFAULT_BRANCH=$(git ls-remote --symref origin HEAD 2>/dev/null | awk '/^ref:/ {sub("refs/heads/","",$2); print $2}') | |
| [ -n "$DEFAULT_BRANCH" ] || DEFAULT_BRANCH=main | |
| echo "Wiki default branch: $DEFAULT_BRANCH" | |
| # Force push to the detected default (lease disabled) | |
| git -c push.forceWithLease=false push --force --no-force-with-lease origin HEAD:"$DEFAULT_BRANCH" |