Skip to content

Commit b11c6f4

Browse files
[workflow] Clean up wiki preview branches (#83) (#86)
* Clean up wiki preview branches * Update wiki submodule pointer for PR #86 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent c59f317 commit b11c6f4

6 files changed

Lines changed: 100 additions & 3 deletions

File tree

.github/wiki

Submodule wiki updated from fc18a8d to 97d9cd1

.github/workflows/wiki.yml

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: Update Wiki
33
on:
44
workflow_call:
55
workflow_dispatch:
6+
schedule:
7+
- cron: '17 3 * * *'
68
pull_request:
79
types: [opened, synchronize, reopened]
810
pull_request_target:
@@ -148,4 +150,87 @@ jobs:
148150

149151
- name: Delete wiki preview branch
150152
working-directory: .github/wiki
151-
run: git push origin --delete "${WIKI_PREVIEW_BRANCH}"
153+
run: |
154+
if git ls-remote --exit-code --heads origin "${WIKI_PREVIEW_BRANCH}" >/dev/null 2>&1; then
155+
git push origin --delete "${WIKI_PREVIEW_BRANCH}"
156+
else
157+
echo "Wiki preview branch ${WIKI_PREVIEW_BRANCH} does not exist. Nothing to delete."
158+
fi
159+
160+
cleanup_closed_preview:
161+
name: Delete Closed PR Wiki Preview
162+
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == false
163+
runs-on: ubuntu-latest
164+
165+
env:
166+
WIKI_PREVIEW_BRANCH: pr-${{ github.event.pull_request.number }}
167+
168+
steps:
169+
- name: Checkout main branch
170+
uses: actions/checkout@v6
171+
with:
172+
token: ${{ github.token }}
173+
ref: main
174+
submodules: recursive
175+
fetch-depth: 0
176+
177+
- name: Mark wiki workspace as safe for git
178+
run: git config --global --add safe.directory "$GITHUB_WORKSPACE/.github/wiki"
179+
180+
- name: Delete wiki preview branch
181+
working-directory: .github/wiki
182+
run: |
183+
if git ls-remote --exit-code --heads origin "${WIKI_PREVIEW_BRANCH}" >/dev/null 2>&1; then
184+
git push origin --delete "${WIKI_PREVIEW_BRANCH}"
185+
else
186+
echo "Wiki preview branch ${WIKI_PREVIEW_BRANCH} does not exist. Nothing to delete."
187+
fi
188+
189+
cleanup_orphaned_previews:
190+
name: Delete Orphaned Wiki Previews
191+
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
192+
runs-on: ubuntu-latest
193+
194+
steps:
195+
- name: Checkout main branch
196+
uses: actions/checkout@v6
197+
with:
198+
token: ${{ github.token }}
199+
ref: main
200+
submodules: recursive
201+
fetch-depth: 0
202+
203+
- name: Mark wiki workspace as safe for git
204+
run: git config --global --add safe.directory "$GITHUB_WORKSPACE/.github/wiki"
205+
206+
- name: Delete wiki branches for closed pull requests
207+
working-directory: .github/wiki
208+
env:
209+
GH_TOKEN: ${{ github.token }}
210+
run: |
211+
git fetch origin '+refs/heads/pr-*:refs/remotes/origin/pr-*' || true
212+
213+
git for-each-ref --format='%(refname:short)' refs/remotes/origin/pr-* | while read -r remote_branch; do
214+
branch="${remote_branch#origin/}"
215+
pull_request_number="${branch#pr-}"
216+
217+
if ! [[ "${pull_request_number}" =~ ^[0-9]+$ ]]; then
218+
echo "Skipping non-PR wiki preview branch ${branch}."
219+
continue
220+
fi
221+
222+
state="$(gh pr view "${pull_request_number}" --repo "${GITHUB_REPOSITORY}" --json state --jq '.state' 2>/dev/null || echo UNKNOWN)"
223+
224+
case "${state}" in
225+
CLOSED|MERGED)
226+
echo "Deleting wiki preview branch ${branch} for ${state} pull request #${pull_request_number}."
227+
git push origin --delete "${branch}" || true
228+
;;
229+
OPEN)
230+
echo "Keeping wiki preview branch ${branch} for open pull request #${pull_request_number}."
231+
;;
232+
*)
233+
echo "Could not resolve pull request #${pull_request_number} for wiki preview branch ${branch}. Keeping it."
234+
;;
235+
esac
236+
done

docs/advanced/branch-protection-and-bot-commits.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ reject direct commits.
3434
After the pull request is merged into ``main``, the publish job copies the
3535
content from the wiki preview branch, such as ``pr-123``, to the wiki
3636
``master`` branch. That makes the reviewed wiki content live only after the
37-
source code merge is complete.
37+
source code merge is complete. The workflow then deletes the ``pr-123`` branch
38+
because it is no longer needed.
39+
40+
If the pull request is closed without merge, the workflow deletes the matching
41+
wiki preview branch without promoting it to ``master``. A scheduled cleanup also
42+
scans existing ``pr-<number>`` wiki branches and removes branches whose pull
43+
requests are already closed.
3844

3945
Reports Preview Lifecycle
4046
-------------------------
@@ -111,6 +117,7 @@ Operational Checklist
111117
- Review PR preview links before merging documentation-heavy changes.
112118
- Merge only through the protected ``main`` flow.
113119
- Let the post-merge wiki job publish to ``master``.
120+
- Let closed pull requests and scheduled cleanup remove wiki preview branches.
114121
- Let closed pull requests clean up their report preview directories.
115122

116123
See :doc:`consumer-automation` for how reusable workflows and consumer stubs fit

docs/troubleshooting.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ Recovery:
227227
- rerun the cleanup workflow for the closed pull request when available;
228228
- remove stale preview directories or branches only after confirming the pull
229229
request is closed or merged;
230+
- use the scheduled wiki cleanup workflow to remove leftover ``pr-<number>``
231+
branches for pull requests that are already closed;
230232
- check the reports and wiki workflow logs before deleting artifacts manually.
231233

232234
Related References

docs/usage/github-actions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The ``wiki.yml`` workflow synchronizes the documentation generated by the ``dev-
4848
* **Submodule Management**: It manages a submodule at ``.github/wiki`` that points to the actual wiki repository.
4949
* **Pull Requests**: Pushes documentation changes to a dedicated branch (e.g., ``pr-123``) in the wiki repository for review.
5050
* **Merge**: When a PR is merged into ``main``, it pushes the changes to the ``master`` branch of the wiki, making them live.
51+
* **Cleanup**: When a PR is closed, the workflow deletes the matching wiki preview branch. A scheduled cleanup also removes stale ``pr-{number}`` branches for already closed pull requests.
5152

5253
.. note::
5354
See :doc:`../configuration/repository-setup` for mandatory initial setup required for the Wiki workflow to function.

resources/github-actions/wiki.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
types: [opened, synchronize, reopened]
66
pull_request_target:
77
types: [closed]
8+
schedule:
9+
- cron: '17 3 * * *'
810
workflow_dispatch:
911

1012
permissions:

0 commit comments

Comments
 (0)