Skip to content

Reap Orphaned PR Previews #2

Reap Orphaned PR Previews

Reap Orphaned PR Previews #2

Workflow file for this run

name: Reap Orphaned PR Previews
on:
schedule:
# Weekly, Monday 04:00 UTC — after any weekend merge activity has settled.
- cron: '0 4 * * 1'
workflow_dispatch:
# ci.yml deletes a preview when its PR closes, but that cleanup can be lost:
# GitHub evicts an already-PENDING run when a newer one queues into the shared
# gh-pages concurrency group, and cleanup only ever fires on the close event,
# so nothing retries it. QuantEcon.manual has no equivalent to this job and
# consequently serves preview directories for PRs merged months ago. This
# reconciles gh-pages against the set of open PRs and deletes the strays.
jobs:
reap:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: read
concurrency:
group: gh-pages
cancel-in-progress: false
steps:
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 1
- name: Remove previews for PRs that are no longer open
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -eo pipefail
open_prs=$(gh pr list --repo "$GITHUB_REPOSITORY" \
--state open --limit 500 --json number --jq '.[].number')
echo "Open PRs: ${open_prs:-none}"
reaped=0
while IFS= read -r dir; do
[ -n "$dir" ] || continue
number="${dir#./pr-}"
if grep -qx "$number" <<<"$open_prs"; then
echo "keep $dir (PR #$number is open)"
else
echo "reap $dir (PR #$number is not open)"
rm -rf "$dir"
reaped=$((reaped + 1))
fi
done < <(find . -maxdepth 1 -type d -name 'pr-*')
if [ "$reaped" -eq 0 ]; then
echo "No orphaned previews found"
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email \
"41898282+github-actions[bot]@users.noreply.github.com"
git add .
if git diff --staged --quiet; then
echo "Nothing staged after reaping $reaped director(ies)"
exit 0
fi
git commit -m "Reap $reaped orphaned PR preview(s)"
for i in 1 2 3; do
if git push; then
echo "Reaped $reaped orphaned preview(s)"
exit 0
fi
echo "Push attempt $i failed, rebasing and retrying..."
git pull --rebase origin gh-pages
sleep 5
done
echo "Failed to push after 3 attempts"
exit 1