|
| 1 | +name: Cleanup Old PR Previews |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run weekly on Sunday at midnight UTC |
| 6 | + - cron: '0 0 * * 0' |
| 7 | + workflow_dispatch: # Allow manual trigger |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + cleanup: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 # Need full history to determine folder age |
| 20 | + |
| 21 | + - name: Find and delete old preview folders |
| 22 | + run: | |
| 23 | + set -euo pipefail |
| 24 | +
|
| 25 | + DAYS_OLD=7 |
| 26 | + CUTOFF_DATE=$(date -d "$DAYS_OLD days ago" +%s) |
| 27 | + DELETED_FOLDERS="" |
| 28 | +
|
| 29 | + echo "Cutoff date: $(date -d "@$CUTOFF_DATE" --iso-8601=seconds)" |
| 30 | + echo "Looking for pr-* folders older than $DAYS_OLD days..." |
| 31 | + echo "" |
| 32 | +
|
| 33 | + for dir in pr-*/; do |
| 34 | + [ -d "$dir" ] || continue |
| 35 | + dir="${dir%/}" |
| 36 | +
|
| 37 | + # Get the date of the first commit that added this folder |
| 38 | + FIRST_COMMIT_DATE=$(git log --diff-filter=A --format=%ct --reverse -- "$dir" | head -1) |
| 39 | +
|
| 40 | + if [ -z "$FIRST_COMMIT_DATE" ]; then |
| 41 | + echo "Warning: Could not find creation date for $dir, skipping" |
| 42 | + continue |
| 43 | + fi |
| 44 | +
|
| 45 | + FOLDER_AGE_DAYS=$(( ($(date +%s) - FIRST_COMMIT_DATE) / 86400 )) |
| 46 | +
|
| 47 | + if [ "$FIRST_COMMIT_DATE" -lt "$CUTOFF_DATE" ]; then |
| 48 | + echo "Deleting $dir (created $(date -d "@$FIRST_COMMIT_DATE" --iso-8601=seconds), $FOLDER_AGE_DAYS days old)" |
| 49 | + rm -rf "$dir" |
| 50 | + DELETED_FOLDERS="$DELETED_FOLDERS $dir" |
| 51 | + else |
| 52 | + echo "Keeping $dir (created $(date -d "@$FIRST_COMMIT_DATE" --iso-8601=seconds), $FOLDER_AGE_DAYS days old)" |
| 53 | + fi |
| 54 | + done |
| 55 | +
|
| 56 | + echo "" |
| 57 | + echo "DELETED_FOLDERS=$DELETED_FOLDERS" >> "$GITHUB_ENV" |
| 58 | +
|
| 59 | + - name: Commit and push changes |
| 60 | + run: | |
| 61 | + if [ -z "$DELETED_FOLDERS" ]; then |
| 62 | + echo "No folders to delete" |
| 63 | + exit 0 |
| 64 | + fi |
| 65 | +
|
| 66 | + git config user.name "github-actions[bot]" |
| 67 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 68 | +
|
| 69 | + git add -A |
| 70 | + git commit -m "chore: cleanup old PR preview folders |
| 71 | +
|
| 72 | + Deleted folders:$DELETED_FOLDERS" |
| 73 | + git push |
0 commit comments