Merge pull request #68 from Sendspin/chrisuthe/task/restore-direct-ad… #19
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: Wiki | |
| # Mirrors docs/wiki/ to the GitHub wiki, one-way: wiki-tab edits are overwritten on the next push. | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'docs/wiki/**' | |
| - '.github/workflows/wiki.yml' | |
| # For the first sync after enabling the wiki. | |
| workflow_dispatch: | |
| # Queued, not cancelled, so the last push's tree always wins. | |
| concurrency: | |
| group: wiki | |
| cancel-in-progress: false | |
| permissions: | |
| contents: read | |
| defaults: | |
| run: | |
| # Explicit, since the default is bash -e without pipefail. | |
| shell: bash | |
| jobs: | |
| sync: | |
| name: sync | |
| runs-on: ubuntu-24.04 | |
| timeout-minutes: 5 | |
| permissions: | |
| contents: write | |
| steps: | |
| # Pinned to a commit: this job has write access. | |
| - name: Check out | |
| uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | |
| # Skips with a notice when the repository has no wiki; anything else fails. | |
| - name: Is there a wiki to publish to | |
| id: wiki | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| has_wiki="$(gh api "repos/$GITHUB_REPOSITORY" --jq '.has_wiki')" | |
| echo "has_wiki=$has_wiki" >>"$GITHUB_OUTPUT" | |
| # Only `false` skips; anything else is unanswered and must not skip silently. | |
| case "$has_wiki" in | |
| true) ;; | |
| false) | |
| echo "::notice::Wikis are turned off for $GITHUB_REPOSITORY, so docs/wiki was not published. Enable it under Settings -> General -> Features -> Wikis, then re-run this workflow." | |
| ;; | |
| *) | |
| echo "::error::The GitHub API answered '$has_wiki' for .has_wiki, which is neither true nor false. Refusing to guess whether there is a wiki to publish to." | |
| exit 1 | |
| ;; | |
| esac | |
| # A never-initialised wiki has no git repository; recognised by git's message, so other clone errors fail. | |
| - name: Clone the wiki | |
| id: clone | |
| if: steps.wiki.outputs.has_wiki == 'true' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set +e | |
| git clone --depth 1 \ | |
| "https://x-access-token:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY.wiki.git" \ | |
| wiki 2>clone.err | |
| status=$? | |
| set -e | |
| # git prints the URL without credentials. | |
| cat clone.err | |
| if [ "$status" -eq 0 ]; then | |
| echo 'cloned=true' >>"$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| if grep -qiE 'repository .* not found|not found: .*\.wiki' clone.err; then | |
| echo 'cloned=false' >>"$GITHUB_OUTPUT" | |
| echo "::notice::$GITHUB_REPOSITORY has wikis enabled but no wiki repository yet, so docs/wiki was not published. Create any page once in the wiki tab — that is what GitHub creates the repository on — then re-run this workflow." | |
| exit 0 | |
| fi | |
| echo '::error::Could not clone the wiki, and not because it is missing. See the git output above.' | |
| exit 1 | |
| # Emptied and refilled so removals propagate; `! -name .git` keeps the clone's history. | |
| - name: Mirror docs/wiki into it | |
| if: steps.clone.outputs.cloned == 'true' | |
| run: | | |
| find wiki -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} + | |
| cp -R docs/wiki/. wiki/ | |
| echo 'What the wiki holds that differs from docs/wiki:' | |
| git -C wiki status --porcelain | |
| # No empty commits, and never force: a rejected push means someone edited the wiki tab. | |
| - name: Commit and push what changed | |
| if: steps.clone.outputs.cloned == 'true' | |
| run: | | |
| cd wiki | |
| if [ -z "$(git status --porcelain)" ]; then | |
| echo "::notice::The wiki already matches docs/wiki at $GITHUB_SHA; nothing to push." | |
| exit 0 | |
| fi | |
| git config user.name 'github-actions[bot]' | |
| git config user.email '41898282+github-actions[bot]@users.noreply.github.com' | |
| git add -A | |
| git commit -m "Sync docs/wiki from $GITHUB_SHA" | |
| git push 2>push.err || { | |
| cat push.err | |
| echo '::error::Pushing to the wiki was rejected. The usual cause is a page edited in the wiki tab, which this mirror overwrites rather than merges — copy the edit into docs/wiki/ and open a pull request for it.' | |
| exit 1 | |
| } | |
| echo "::notice::Published docs/wiki to the wiki at $GITHUB_SHA." |