From b8578b853ef48629bd42c3c77e66e1c08948edce Mon Sep 17 00:00:00 2001 From: Alberto Mercurio Date: Fri, 10 Oct 2025 11:50:51 +0200 Subject: [PATCH] Improve preview cleanup workflow with scheduled cleanup for stale PRs --- .github/workflows/CleanPreviewDoc.yml | 47 +++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/.github/workflows/CleanPreviewDoc.yml b/.github/workflows/CleanPreviewDoc.yml index dad91e5b5..aef17e3bb 100644 --- a/.github/workflows/CleanPreviewDoc.yml +++ b/.github/workflows/CleanPreviewDoc.yml @@ -3,29 +3,62 @@ 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 - deployments: write jobs: cleanup-preview-doc: runs-on: ubuntu-latest steps: - name: Checkout gh-pages branch - uses: actions/checkout@v5 + uses: actions/checkout@v4 with: ref: gh-pages - - name: Delete preview and history + push changes + - name: Delete preview(s) and reset history run: | - if [ -d "previews/PR$PRNUM" ]; then + # 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 "documenter@juliadocs.github.io" - git rm -rf "previews/PR$PRNUM" - git commit -m "delete preview" + 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 }} - PRNUM: ${{ github.event.number }}