Prune Old Allure Attachments #23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Prune Old Allure Attachments | |
| on: | |
| schedule: | |
| - cron: '0 16 * * 0' # Weekly, 16:00 UTC Sunday | |
| workflow_dispatch: | |
| inputs: | |
| retention-days: | |
| description: 'Number of days to retain attachments' | |
| required: false | |
| default: '14' | |
| type: string | |
| dry-run: | |
| description: 'If true, only list files that would be deleted (no deletion or commit)' | |
| required: false | |
| default: 'false' | |
| type: boolean | |
| jobs: | |
| prune: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: gh-pages | |
| lfs: true | |
| fetch-depth: 0 # Get full history to check file ages | |
| env: | |
| GIT_LFS_SKIP_SMUDGE: 1 # Don't actually download LFS content | |
| - name: Set retention days | |
| id: retention | |
| run: | | |
| DAYS="${{ github.event.inputs.retention-days || '14' }}" | |
| [[ "$DAYS" =~ ^[1-9][0-9]*$ ]] || { echo "Error: retention-days must be a positive integer"; exit 1; } | |
| echo "days=$DAYS" >> $GITHUB_OUTPUT | |
| - name: Prune Allure attachments | |
| env: | |
| DRY_RUN: ${{ github.event.inputs.dry-run || 'false' }} | |
| RETENTION_DAYS: ${{ steps.retention.outputs.days }} | |
| run: | | |
| CUTOFF_DATE=$(date -d "$RETENTION_DAYS days ago" +%s) | |
| PREVIEW_LIMIT=20 | |
| DELETE_LIST="files_to_delete.txt" | |
| echo "Retention: $RETENTION_DAYS days | Dry run: $DRY_RUN" >> $GITHUB_STEP_SUMMARY | |
| # Find old attachment files | |
| > "$DELETE_LIST" | |
| for pattern in '*/data/attachments/*.png' '*/data/attachments/*.txt' '*/data/attachments/*.imagediff'; do | |
| while IFS= read -r -d '' file; do | |
| COMMIT_DATE=$(git log -1 --format="%ct" -- "$file" 2>/dev/null || echo "0") | |
| [ "$COMMIT_DATE" -lt "$CUTOFF_DATE" ] && [ "$COMMIT_DATE" -ne "0" ] && echo "$file" >> "$DELETE_LIST" | |
| done < <(git ls-files -z "$pattern") | |
| done | |
| COUNT=$(wc -l < "$DELETE_LIST" 2>/dev/null || echo 0) | |
| echo "Found $COUNT old files" >> $GITHUB_STEP_SUMMARY | |
| if [ "$COUNT" -gt 0 ]; then | |
| if [ "$DRY_RUN" == "true" ]; then | |
| echo "Files to delete:" >> $GITHUB_STEP_SUMMARY | |
| if [ -s "$DELETE_LIST" ]; then | |
| head -$PREVIEW_LIMIT "$DELETE_LIST" >> $GITHUB_STEP_SUMMARY | |
| [ "$COUNT" -gt $PREVIEW_LIMIT ] && echo "...(+$((COUNT-PREVIEW_LIMIT)) more)" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| else | |
| xargs rm -f < "$DELETE_LIST" | |
| echo "Deleted $COUNT files" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| fi | |
| rm -f "$DELETE_LIST" | |
| - name: Commit changes | |
| if: github.event.inputs.dry-run != 'true' | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| if [ -n "$(git status --porcelain)" ]; then | |
| git add -A | |
| git commit -m "ci: Prune attachments older than ${{ steps.retention.outputs.days }} days" | |
| git push | |
| fi | |
| - name: Prune LFS objects | |
| if: github.event.inputs.dry-run != 'true' | |
| run: | | |
| git lfs prune --verify-remote || echo "LFS prune failed" |