From 42ffb518f129f4e6e46797598d0ac1b206575380 Mon Sep 17 00:00:00 2001 From: tac0turtle Date: Mon, 26 Jan 2026 14:42:51 +0100 Subject: [PATCH 1/3] chore: add workflow to cleanup old PR preview folders Runs weekly on Sunday at midnight UTC (and on manual trigger). Deletes pr-* folders older than 7 days based on git history. Co-Authored-By: Claude Opus 4.5 --- .github/workflows/cleanup-old-previews.yml | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 .github/workflows/cleanup-old-previews.yml diff --git a/.github/workflows/cleanup-old-previews.yml b/.github/workflows/cleanup-old-previews.yml new file mode 100644 index 000000000..f250c4635 --- /dev/null +++ b/.github/workflows/cleanup-old-previews.yml @@ -0,0 +1,73 @@ +name: Cleanup Old PR Previews + +on: + schedule: + # Run weekly on Sunday at midnight UTC + - cron: '0 0 * * 0' + workflow_dispatch: # Allow manual trigger + +permissions: + contents: write + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Need full history to determine folder age + + - name: Find and delete old preview folders + run: | + set -euo pipefail + + DAYS_OLD=7 + CUTOFF_DATE=$(date -d "$DAYS_OLD days ago" +%s) + DELETED_FOLDERS="" + + echo "Cutoff date: $(date -d "@$CUTOFF_DATE" --iso-8601=seconds)" + echo "Looking for pr-* folders older than $DAYS_OLD days..." + echo "" + + for dir in pr-*/; do + [ -d "$dir" ] || continue + dir="${dir%/}" + + # Get the date of the first commit that added this folder + FIRST_COMMIT_DATE=$(git log --diff-filter=A --format=%ct --reverse -- "$dir" | head -1) + + if [ -z "$FIRST_COMMIT_DATE" ]; then + echo "Warning: Could not find creation date for $dir, skipping" + continue + fi + + FOLDER_AGE_DAYS=$(( ($(date +%s) - FIRST_COMMIT_DATE) / 86400 )) + + if [ "$FIRST_COMMIT_DATE" -lt "$CUTOFF_DATE" ]; then + echo "Deleting $dir (created $(date -d "@$FIRST_COMMIT_DATE" --iso-8601=seconds), $FOLDER_AGE_DAYS days old)" + rm -rf "$dir" + DELETED_FOLDERS="$DELETED_FOLDERS $dir" + else + echo "Keeping $dir (created $(date -d "@$FIRST_COMMIT_DATE" --iso-8601=seconds), $FOLDER_AGE_DAYS days old)" + fi + done + + echo "" + echo "DELETED_FOLDERS=$DELETED_FOLDERS" >> "$GITHUB_ENV" + + - name: Commit and push changes + run: | + if [ -z "$DELETED_FOLDERS" ]; then + echo "No folders to delete" + exit 0 + fi + + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + git add -A + git commit -m "chore: cleanup old PR preview folders + + Deleted folders:$DELETED_FOLDERS" + git push From 4541b3e3b8e44cf971683540027631e314b6705c Mon Sep 17 00:00:00 2001 From: tac0turtle Date: Mon, 26 Jan 2026 14:45:49 +0100 Subject: [PATCH 2/3] fix: handle SIGPIPE in cleanup workflow Disable pipefail in subshell when piping git log to head to avoid exit code 141 when head closes the pipe early. Co-Authored-By: Claude Opus 4.5 --- .github/workflows/cleanup-old-previews.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cleanup-old-previews.yml b/.github/workflows/cleanup-old-previews.yml index f250c4635..6359909f8 100644 --- a/.github/workflows/cleanup-old-previews.yml +++ b/.github/workflows/cleanup-old-previews.yml @@ -35,7 +35,8 @@ jobs: dir="${dir%/}" # Get the date of the first commit that added this folder - FIRST_COMMIT_DATE=$(git log --diff-filter=A --format=%ct --reverse -- "$dir" | head -1) + # Disable pipefail in subshell to avoid SIGPIPE (exit 141) when head closes early + FIRST_COMMIT_DATE=$(set +o pipefail; git log --diff-filter=A --format=%ct --reverse -- "$dir" | head -1) if [ -z "$FIRST_COMMIT_DATE" ]; then echo "Warning: Could not find creation date for $dir, skipping" From f0f58a5784b91486dbcce75ede070a78ac8a0eb3 Mon Sep 17 00:00:00 2001 From: tac0turtle Date: Mon, 26 Jan 2026 14:46:57 +0100 Subject: [PATCH 3/3] feat: add pull_request trigger for testing Runs as dry-run on PRs (no commit/push), shows what would be deleted. Co-Authored-By: Claude Opus 4.5 --- .github/workflows/cleanup-old-previews.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/cleanup-old-previews.yml b/.github/workflows/cleanup-old-previews.yml index 6359909f8..24a8ac111 100644 --- a/.github/workflows/cleanup-old-previews.yml +++ b/.github/workflows/cleanup-old-previews.yml @@ -5,6 +5,7 @@ on: # Run weekly on Sunday at midnight UTC - cron: '0 0 * * 0' workflow_dispatch: # Allow manual trigger + pull_request: # For testing permissions: contents: write @@ -57,7 +58,17 @@ jobs: echo "" echo "DELETED_FOLDERS=$DELETED_FOLDERS" >> "$GITHUB_ENV" + - name: Summary (dry-run on PR) + if: github.event_name == 'pull_request' + run: | + if [ -z "$DELETED_FOLDERS" ]; then + echo "Dry-run: No folders would be deleted" + else + echo "Dry-run: Would delete:$DELETED_FOLDERS" + fi + - name: Commit and push changes + if: github.event_name != 'pull_request' run: | if [ -z "$DELETED_FOLDERS" ]; then echo "No folders to delete"