From 7ff3adcc5fbe117c7dd1caed09cb636d10094244 Mon Sep 17 00:00:00 2001 From: Jack Amadeo Date: Thu, 18 Dec 2025 16:22:57 -0500 Subject: [PATCH] Clean PR preview sites from gh-pages branch history (#6161) --- .github/workflows/pr-website-preview.yml | 28 ++++++++++++++--- scripts/clean-gh-pages.sh | 39 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100755 scripts/clean-gh-pages.sh diff --git a/.github/workflows/pr-website-preview.yml b/.github/workflows/pr-website-preview.yml index 526fdb5baa..f93032a44c 100644 --- a/.github/workflows/pr-website-preview.yml +++ b/.github/workflows/pr-website-preview.yml @@ -13,20 +13,20 @@ on: branches-ignore: - 'dependabot/**' -concurrency: preview-${{ github.ref }} +concurrency: pr-preview jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout the branch - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # pin@v3 + uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3 - name: Setup Node.js if: github.event.action != 'closed' - uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # pin@v3 + uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610 # v3 with: - node-version: 20 + node-version: 20 - name: Install dependencies and build docs working-directory: ./documentation @@ -41,7 +41,25 @@ jobs: npm run build - name: Deploy preview - uses: rossjrw/pr-preview-action@df22037db54ab6ee34d3c1e2b8810ac040a530c6 # pin@v1 + uses: rossjrw/pr-preview-action@8ff09e486b4c23709012eedd3b42e9f0b95dd0c5 # v1 if: ${{ github.event.pull_request.head.repo.full_name == 'block/goose' }} with: source-dir: documentation/build + + cleanup: + runs-on: ubuntu-latest + needs: deploy + if: github.event.action == 'closed' + permissions: + contents: write + steps: + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5 + with: + fetch-depth: 0 + + - uses: astral-sh/setup-uv@38f3f104447c67c051c4a08e39b64a148898af3a # v4 + + - name: Clean up gh-pages branch + run: | + bash scripts/clean-gh-pages.sh + git push origin $(git rev-parse origin/gh-pages):gh-pages --force diff --git a/scripts/clean-gh-pages.sh b/scripts/clean-gh-pages.sh new file mode 100755 index 0000000000..b19f76e8b8 --- /dev/null +++ b/scripts/clean-gh-pages.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +set -e # Exit on error + +echo "Starting gh-pages branch cleanup..." + +REMOVE_PATHS_FILE="/tmp/remove-paths.txt" +> "$REMOVE_PATHS_FILE" + +dirs_with_visible_files=$(git ls-tree -r origin/gh-pages:pr-preview --name-only 2>/dev/null | \ + grep -v '/\.' | \ + cut -d/ -f1 | \ + sort -u || true) + +all_dirs=$(git ls-tree -d origin/gh-pages:pr-preview --name-only 2>/dev/null || true) + +if [ -z "$all_dirs" ]; then + echo "No directories found in pr-preview or pr-preview does not exist" + rm "$REMOVE_PATHS_FILE" + exit 0 +fi + +while IFS= read -r dir; do + if [ -n "$dir" ]; then + if ! echo "$dirs_with_visible_files" | grep -q "^${dir}$"; then + dir_path="pr-preview/$dir" + echo "Found directory to remove: $dir_path" + echo "$dir_path" >> "$REMOVE_PATHS_FILE" + fi + fi +done <<< "$all_dirs" + +if [ ! -s "$REMOVE_PATHS_FILE" ]; then + echo "No empty or hidden-file-only directories found. Nothing to clean up." + rm "$REMOVE_PATHS_FILE" + exit 0 +fi + +uvx git-filter-repo@2.47.0 --paths-from-file "$REMOVE_PATHS_FILE" --invert-paths --refs origin/gh-pages