Skip to content

Commit bc15d35

Browse files
[workflow] Add reports preview concurrency and cleanup (#68) (#88)
* Add reports preview concurrency cleanup * Update wiki submodule pointer for PR #88 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent f01339c commit bc15d35

6 files changed

Lines changed: 111 additions & 6 deletions

File tree

.github/wiki

Submodule wiki updated from a95f045 to fd4afe1

.github/workflows/reports.yml

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@ name: Generate Reports and Deploy to GitHub Pages
22

33
on:
44
workflow_call:
5+
inputs:
6+
cleanup-previews:
7+
description: Remove stale pull request previews without publishing production reports.
8+
required: false
9+
type: boolean
10+
default: false
511
workflow_dispatch:
12+
inputs:
13+
cleanup-previews:
14+
description: Remove stale pull request previews without publishing production reports.
15+
required: false
16+
type: boolean
17+
default: false
18+
schedule:
19+
- cron: '41 3 * * *'
620
pull_request:
721
types: [ "opened", "synchronize", "reopened", "closed" ]
822
push:
@@ -13,12 +27,12 @@ permissions:
1327
pull-requests: write
1428

1529
concurrency:
16-
group: "pages"
17-
cancel-in-progress: false
30+
group: ${{ github.event_name == 'pull_request' && format('reports-preview-pr-{0}', github.event.pull_request.number) || 'reports-pages' }}
31+
cancel-in-progress: ${{ github.event_name == 'pull_request' && github.event.action != 'closed' }}
1832

1933
jobs:
2034
reports:
21-
if: github.event_name != 'pull_request' || github.event.action != 'closed'
35+
if: github.event_name != 'schedule' && !(github.event_name == 'workflow_dispatch' && inputs.cleanup-previews) && (github.event_name != 'pull_request' || github.event.action != 'closed')
2236
name: Generate Reports
2337
runs-on: ubuntu-latest
2438

@@ -143,3 +157,72 @@ jobs:
143157
run: |
144158
cd gh-pages
145159
git push
160+
161+
cleanup_orphaned_previews:
162+
if: github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.cleanup-previews)
163+
name: Cleanup Orphaned Pull Request Previews
164+
runs-on: ubuntu-latest
165+
166+
steps:
167+
- name: Checkout gh-pages
168+
uses: actions/checkout@v6
169+
with:
170+
ref: gh-pages
171+
path: gh-pages
172+
173+
- name: Remove previews for closed pull requests
174+
env:
175+
GH_TOKEN: ${{ github.token }}
176+
run: |
177+
cd gh-pages
178+
179+
deleted=0
180+
skipped=0
181+
unresolved=0
182+
183+
if [ ! -d previews ]; then
184+
echo "No previews directory exists. Nothing to clean."
185+
exit 0
186+
fi
187+
188+
while read -r preview_dir; do
189+
branch="${preview_dir#previews/}"
190+
pull_request_number="${branch#pr-}"
191+
192+
if ! [[ "${pull_request_number}" =~ ^[0-9]+$ ]]; then
193+
echo "Skipping preview directory ${preview_dir}: name does not match pr-<number>."
194+
skipped=$((skipped + 1))
195+
continue
196+
fi
197+
198+
state="$(gh pr view "${pull_request_number}" --repo "${GITHUB_REPOSITORY}" --json state --jq '.state' 2>/dev/null || echo UNKNOWN)"
199+
200+
case "${state}" in
201+
CLOSED|MERGED)
202+
echo "Deleting preview directory ${preview_dir} for ${state} pull request #${pull_request_number}."
203+
rm -rf "${preview_dir}"
204+
deleted=$((deleted + 1))
205+
;;
206+
OPEN)
207+
echo "Keeping preview directory ${preview_dir} for open pull request #${pull_request_number}."
208+
skipped=$((skipped + 1))
209+
;;
210+
*)
211+
echo "Could not resolve pull request #${pull_request_number} for preview directory ${preview_dir}. Keeping it."
212+
unresolved=$((unresolved + 1))
213+
;;
214+
esac
215+
done < <(find previews -mindepth 1 -maxdepth 1 -type d -name 'pr-*' | sort)
216+
217+
echo "Preview cleanup summary: deleted=${deleted}, skipped=${skipped}, unresolved=${unresolved}."
218+
219+
touch .nojekyll
220+
git config user.name "github-actions[bot]"
221+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
222+
git add -A
223+
git diff --cached --quiet || git commit -m "chore: remove orphaned pull request previews"
224+
225+
- name: Push changes
226+
run: |
227+
cd gh-pages
228+
git push

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ On pull requests, the reports workflow:
5555
- generates docs, coverage, and report assets from the pull request code;
5656
- publishes them under ``previews/pr-<number>/`` in the Pages branch;
5757
- comments on the pull request with preview links when possible;
58+
- cancels older in-progress preview runs when a newer push updates the same
59+
pull request;
5860
- keeps workflow artifacts available as a fallback when Pages publishing or the
5961
comment update is unavailable.
6062

@@ -64,7 +66,9 @@ open pull requests can have independent previews without overwriting each other.
6466

6567
When a pull request is closed, the workflow SHOULD remove its preview directory.
6668
This prevents stale documentation and coverage reports from looking like active
67-
review artifacts.
69+
review artifacts. A scheduled cleanup also scans ``previews/pr-<number>/``
70+
directories and removes preview directories whose pull requests are already
71+
closed.
6872

6973
Branch Protection Interactions
7074
------------------------------
@@ -120,7 +124,8 @@ Operational Checklist
120124
- Merge only through the protected ``main`` flow.
121125
- Let the post-merge wiki job publish to ``master``.
122126
- Let closed pull requests and scheduled cleanup remove wiki preview branches.
123-
- Let closed pull requests clean up their report preview directories.
127+
- Let closed pull requests and scheduled cleanup remove report preview
128+
directories.
124129

125130
See :doc:`consumer-automation` for how reusable workflows and consumer stubs fit
126131
into this model, and :doc:`../usage/github-actions` for the workflow summary.

docs/troubleshooting.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ Likely causes:
221221
- the cleanup workflow did not run on the pull request close event;
222222
- the workflow token lacks permission to update Pages or the wiki repository;
223223
- a preview was removed after the comment was posted.
224+
- a reports preview cleanup run skipped a preview because the pull request
225+
number could not be resolved.
224226
- the wiki publish validation detected that remote ``master`` does not match
225227
the preview branch SHA.
226228

@@ -231,6 +233,9 @@ Recovery:
231233
request is closed or merged;
232234
- use the scheduled wiki cleanup workflow to remove leftover ``pr-<number>``
233235
branches for pull requests that are already closed;
236+
- use the scheduled reports cleanup workflow to remove leftover
237+
``previews/pr-<number>/`` directories for pull requests that are already
238+
closed;
234239
- keep the wiki preview branch until the publish validation log shows matching
235240
expected and actual SHAs;
236241
- check the reports and wiki workflow logs before deleting artifacts manually.

docs/usage/github-actions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ The ``reports.yml`` workflow is responsible for generating technical documentati
3838
* Deploys the preview to ``gh-pages`` under ``previews/pr-{number}/``.
3939
* Posts a **Sticky Comment** on the PR with links to the live preview and coverage report.
4040
* **Cleanup**: When a PR is closed, the workflow automatically removes the preview directory from the ``gh-pages`` branch to keep the repository clean.
41+
* **Concurrency**: New pushes to the same PR cancel older in-progress preview runs without affecting other PRs.
42+
* **Scheduled Cleanup**: A scheduled/manual cleanup removes stale ``previews/pr-{number}/`` directories for already closed pull requests.
4143

4244
Fast Forward Wiki
4345
-----------------

resources/github-actions/reports.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ on:
66
push:
77
branches:
88
- main
9+
schedule:
10+
- cron: '41 3 * * *'
911
workflow_dispatch:
12+
inputs:
13+
cleanup-previews:
14+
description: Remove stale pull request previews without publishing production reports.
15+
required: false
16+
type: boolean
17+
default: false
1018

1119
permissions:
1220
contents: write
@@ -16,3 +24,5 @@ jobs:
1624
reports:
1725
uses: php-fast-forward/dev-tools/.github/workflows/reports.yml@main
1826
secrets: inherit
27+
with:
28+
cleanup-previews: ${{ inputs.cleanup-previews || false }}

0 commit comments

Comments
 (0)