Skip to content

Cleanup Preview Documentation #212

Cleanup Preview Documentation

Cleanup Preview Documentation #212

name: Cleanup Preview Documentation
on:
pull_request:
types: [closed]
schedule:
- cron: "0 0 * * 0" # Run weekly on Sunday at midnight
workflow_dispatch:
permissions:
contents: write
jobs:
cleanup-preview-doc:
runs-on: ubuntu-latest
steps:
- name: Checkout gh-pages branch
uses: actions/checkout@v5
with:
ref: gh-pages
- name: Delete preview(s) and reset history
run: |
# If triggered by PR close, delete that specific preview
if [ "${{ github.event_name }}" = "pull_request" ]; then
PRNUM=${{ github.event.number }}
if [ -d "previews/PR$PRNUM" ]; then
echo "Deleting preview for PR #$PRNUM"
git rm -rf "previews/PR$PRNUM"
fi
else
# Scheduled/manual run: check all previews against open PRs
echo "Checking all preview folders for stale PRs..."
# Get list of all PR preview folders
if [ -d "previews" ]; then
for dir in previews/PR*; do
[ -d "$dir" ] || continue
PRNUM=$(echo "$dir" | grep -oP 'PR\K\d+')
# Check if PR is still open via GitHub API
STATUS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$PRNUM" \
| jq -r '.state // "closed"')
if [ "$STATUS" != "open" ]; then
echo "Deleting stale preview for closed PR #$PRNUM"
git rm -rf "$dir"
fi
done
fi
fi
# Only commit and push if there are changes
if ! git diff --cached --quiet; then
git config user.name "Documenter.jl"
git config user.email "[email protected]"
git commit -m "delete preview(s)"
git branch gh-pages-new $(echo "delete history" | git commit-tree HEAD^{tree})
git push --force origin gh-pages-new:gh-pages
else
echo "No stale previews found"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}