Update GitHub Wiki #9
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 | |
| on: | |
| push: | |
| branches: [ main ] | |
| paths: [ 'docs/**' ] | |
| workflow_dispatch: {} | |
| jobs: | |
| update-wiki: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| issues: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Print event info | |
| run: | | |
| echo "GITHUB_EVENT_NAME=$GITHUB_EVENT_NAME" | |
| echo "GITHUB_REF=$GITHUB_REF" | |
| echo "GITHUB_SHA=$GITHUB_SHA" | |
| echo "Event payload path: $GITHUB_EVENT_PATH" | |
| if [ -f "$GITHUB_EVENT_PATH" ]; then echo "--- event payload ---"; cat "$GITHUB_EVENT_PATH"; echo "--- end payload ---"; fi | |
| - name: List changed files (best-effort) | |
| id: list_changed | |
| run: | | |
| git fetch --no-tags --prune --depth=5 origin "${{ github.ref }}" || true | |
| echo "Files changed in this commit/PR:" | |
| git --no-pager show --name-only --pretty="" "${{ github.sha }}" || git ls-files | sed -n '1,200p' | |
| - name: Extract docs markdown files | |
| id: docs_files | |
| run: | | |
| files=$(git --no-pager show --name-only --pretty="" "${{ github.sha }}" || git ls-files) | |
| docs_md=$(echo "$files" | grep -iE '^docs/.*\.md$' || true) | |
| if [ -z "$docs_md" ]; then | |
| echo "docs_files=[]" >> "$GITHUB_OUTPUT" | |
| else | |
| # convert lines to JSON array using jq | |
| arr=$(echo "$docs_md" | jq -R -s -c 'split("\n")[:-1]') | |
| echo "docs_files=$arr" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Add changed files to job summary | |
| if: always() | |
| run: | | |
| echo "### Changed files" >> "$GITHUB_STEP_SUMMARY" | |
| git --no-pager show --name-only --pretty="" "${{ github.sha }}" >> "$GITHUB_STEP_SUMMARY" || git ls-files | sed -n '1,200p' >> "$GITHUB_STEP_SUMMARY" | |
| - name: Fail early if DEPLOY_WIKI_TOKEN is missing | |
| env: | |
| DEPLOY_WIKI_TOKEN: ${{ secrets.DEPLOY_WIKI_TOKEN }} | |
| run: | | |
| if [ -z "$DEPLOY_WIKI_TOKEN" ]; then | |
| echo "ERROR: DEPLOY_WIKI_TOKEN is not set; the workflow will not be able to push to the wiki." | |
| echo "Create a repository secret named DEPLOY_WIKI_TOKEN containing a PAT with 'Contents: write' or use GITHUB_TOKEN with write permissions." | |
| exit 1 | |
| fi | |
| - name: Echo docs trigger | |
| run: echo "Update Wiki workflow triggered for docs changes" | |
| - name: Fail early if DEPLOY_WIKI_TOKEN is missing | |
| env: | |
| DEPLOY_WIKI_TOKEN: ${{ secrets.DEPLOY_WIKI_TOKEN }} | |
| run: | | |
| if [ -z "$DEPLOY_WIKI_TOKEN" ]; then | |
| echo "WARNING: DEPLOY_WIKI_TOKEN is not set; the workflow will not be able to push to the wiki." | |
| echo "Set the DEPLOY_WIKI_TOKEN secret or provide GITHUB_TOKEN permissions." | |
| exit 1 | |
| fi | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| - name: Run wiki init and push | |
| env: | |
| DEPLOY_WIKI_TOKEN: ${{ secrets.DEPLOY_WIKI_TOKEN }} | |
| GITHUB_TOKEN: ${{ secrets.DEPLOY_WIKI_TOKEN }} | |
| run: | | |
| node scripts/init-wiki.js | |
| - name: Comment on PR with changed docs files | |
| if: github.event_name == 'pull_request' && steps.docs_files.outputs.docs_files != '[]' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.DEPLOY_WIKI_TOKEN }} | |
| DOCS_FILES: ${{ steps.docs_files.outputs.docs_files }} | |
| run: | | |
| # Extract PR number and head SHA from the event payload at runtime | |
| pr_number=$(jq -r '.pull_request.number // empty' "$GITHUB_EVENT_PATH") | |
| head_sha=$(jq -r '.pull_request.head.sha // empty' "$GITHUB_EVENT_PATH") | |
| if [ -z "$pr_number" ] || [ -z "$head_sha" ]; then | |
| echo "No PR number or head SHA found in event payload; skipping comment." | |
| exit 0 | |
| fi | |
| owner=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f1) | |
| repo=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2) | |
| files_json="$DOCS_FILES" | |
| body="### Docs files changed\n\n" | |
| # Build markdown links that point to the file at the PR head SHA | |
| body+=$(echo "$files_json" | jq -r --arg repo "$owner/$repo" --arg sha "$head_sha" '.[] | "- [\(. )](https://github.com/" + $repo + "/blob/" + $sha + "/" + .)') | |
| payload=$(jq -n --arg body "$body" '{body: $body}') | |
| curl -s -S -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" \ | |
| -d "$payload" "https://api.github.com/repos/$owner/$repo/issues/$pr_number/comments" | |
| - name: Post final publish comment | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.DEPLOY_WIKI_TOKEN }} | |
| DOCS_FILES: ${{ steps.docs_files.outputs.docs_files }} | |
| run: | | |
| # Compose the final message with links, timestamp, and run URL | |
| owner=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f1) | |
| repo=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2) | |
| branch_url="https://github.com/$owner/$repo/tree/wiki" | |
| run_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
| timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
| files_json="$DOCS_FILES" | |
| if [ "$files_json" = "[]" ] || [ -z "$files_json" ]; then | |
| files_section="(no markdown docs changed)" | |
| else | |
| # Build markdown links pointing to files on the wiki branch | |
| files_section=$(echo "$files_json" | jq -r --arg repo "$owner/$repo" '.[] | "- [\(. )](https://github.com/" + $repo + "/blob/wiki/" + .)') | |
| fi | |
| message="### Docs published to wiki branch\n\n$branch_url\n\n**Changed files:**\n$files_section\n\nPublished at: $timestamp\nRun: $run_url" | |
| # If this was a PR, comment on the PR; otherwise just echo | |
| pr_number=$(jq -r '.pull_request.number // empty' "$GITHUB_EVENT_PATH") | |
| if [ -n "$pr_number" ]; then | |
| payload=$(jq -n --arg body "$message" '{body: $body}') | |
| curl -s -S -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" \ | |
| -d "$payload" "https://api.github.com/repos/$owner/$repo/issues/$pr_number/comments" | |
| else | |
| echo "$message" | |
| fi |