1
0

cleanup_caches.yml 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. name: Cleanup Caches
  2. on:
  3. schedule:
  4. - cron: '0 3 * * 0' # every Sunday
  5. workflow_dispatch:
  6. jobs:
  7. cleanup:
  8. runs-on: ubuntu-latest
  9. permissions:
  10. actions: write
  11. steps:
  12. - name: Delete caches older than 3 days
  13. env:
  14. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  15. run: |
  16. CUTOFF_DATE=$(date -d "3 days ago" -Ins --utc | sed 's/+0000/Z/')
  17. echo "Deleting caches older than: $CUTOFF_DATE"
  18. CACHE_IDS=$(gh api --paginate repos/${{ github.repository }}/actions/caches \
  19. --jq ".actions_caches[] | select(.last_accessed_at < \"$CUTOFF_DATE\") | .id" 2>/dev/null)
  20. if [ -z "$CACHE_IDS" ]; then
  21. echo "No old caches found to delete."
  22. else
  23. echo "$CACHE_IDS" | while read CACHE_ID; do
  24. echo "Deleting cache: $CACHE_ID"
  25. gh api -X DELETE repos/${{ github.repository }}/actions/caches/$CACHE_ID
  26. done
  27. echo "Old caches deleted successfully."
  28. fi