From b4ddbc7dc9322c4074ed9c45e5eb62beae50ab39 Mon Sep 17 00:00:00 2001 From: Raunak Madan Date: Tue, 14 Apr 2026 02:50:31 +0530 Subject: [PATCH] Prune old PR previews Signed-off-by: Raunak Madan --- .github/workflows/build-and-preview-docs.yml | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/.github/workflows/build-and-preview-docs.yml b/.github/workflows/build-and-preview-docs.yml index a809779f2aaa9..9fd40febcc713 100644 --- a/.github/workflows/build-and-preview-docs.yml +++ b/.github/workflows/build-and-preview-docs.yml @@ -93,6 +93,47 @@ jobs: action: auto comment: false + - name: Checkout gh-pages for preview retention + if: github.event.action != 'closed' + uses: actions/checkout@v6 + with: + ref: gh-pages + fetch-depth: 0 + path: gh-pages-maintenance + + - name: Prune old PR previews + if: github.event.action != 'closed' + run: | + cd gh-pages-maintenance + mkdir -p pr-preview + + mapfile -t previews < <( + while IFS= read -r preview; do + timestamp="$(git log -1 --format=%ct -- "pr-preview/$preview" 2>/dev/null || echo 0)" + printf '%s %s\n' "$timestamp" "$preview" + done < <(find pr-preview -mindepth 1 -maxdepth 1 -type d -name 'pr-*' -printf '%f\n') \ + | sort -nr \ + | awk '{print $2}' + ) + + if (( ${#previews[@]} <= 6 )); then + exit 0 + fi + + for preview in "${previews[@]:6}"; do + rm -rf "pr-preview/$preview" + done + + if git diff --quiet -- pr-preview; then + exit 0 + fi + + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add pr-preview + git commit -m "Prune old PR previews" + git push + - name: Comment PR with Preview URL if: github.event.action != 'closed' uses: marocchino/sticky-pull-request-comment@v2