@@ -3,29 +3,62 @@ name: Cleanup Preview Documentation
33on :
44 pull_request :
55 types : [closed]
6+ schedule :
7+ - cron : " 0 0 * * 0" # Run weekly on Sunday at midnight
8+ workflow_dispatch :
69
710permissions :
811 contents : write
9- deployments : write
1012
1113jobs :
1214 cleanup-preview-doc :
1315 runs-on : ubuntu-latest
1416 steps :
1517 - name : Checkout gh-pages branch
16- uses : actions/checkout@v5
18+ uses : actions/checkout@v4
1719 with :
1820 ref : gh-pages
19- - name : Delete preview and history + push changes
21+ - name : Delete preview(s) and reset history
2022 run : |
21- if [ -d "previews/PR$PRNUM" ]; then
23+ # If triggered by PR close, delete that specific preview
24+ if [ "${{ github.event_name }}" = "pull_request" ]; then
25+ PRNUM=${{ github.event.number }}
26+ if [ -d "previews/PR$PRNUM" ]; then
27+ echo "Deleting preview for PR #$PRNUM"
28+ git rm -rf "previews/PR$PRNUM"
29+ fi
30+ else
31+ # Scheduled/manual run: check all previews against open PRs
32+ echo "Checking all preview folders for stale PRs..."
33+
34+ # Get list of all PR preview folders
35+ if [ -d "previews" ]; then
36+ for dir in previews/PR*; do
37+ [ -d "$dir" ] || continue
38+ PRNUM=$(echo "$dir" | grep -oP 'PR\K\d+')
39+
40+ # Check if PR is still open via GitHub API
41+ STATUS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
42+ "https://api.github.com/repos/${{ github.repository }}/pulls/$PRNUM" \
43+ | jq -r '.state // "closed"')
44+
45+ if [ "$STATUS" != "open" ]; then
46+ echo "Deleting stale preview for closed PR #$PRNUM"
47+ git rm -rf "$dir"
48+ fi
49+ done
50+ fi
51+ fi
52+
53+ # Only commit and push if there are changes
54+ if ! git diff --cached --quiet; then
2255 git config user.name "Documenter.jl"
2356 git config user.email "[email protected] " 24- git rm -rf "previews/PR$PRNUM"
25- git commit -m "delete preview"
57+ git commit -m "delete preview(s)"
2658 git branch gh-pages-new $(echo "delete history" | git commit-tree HEAD^{tree})
2759 git push --force origin gh-pages-new:gh-pages
60+ else
61+ echo "No stale previews found"
2862 fi
2963 env :
3064 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
31- PRNUM : ${{ github.event.number }}
0 commit comments