cleanup-cache.yml 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. name: Cleanup cache
  2. on:
  3. schedule:
  4. - cron: "0 11 * * *"
  5. pull_request:
  6. types:
  7. - closed
  8. jobs:
  9. cleanup:
  10. runs-on: ubuntu-latest
  11. steps:
  12. - name: Check out code
  13. uses: actions/checkout@v4
  14. - name: Cleanup caches older than 5 days
  15. if: github.event_name == 'schedule'
  16. uses: MyAlbum/purge-cache@v2
  17. with:
  18. max-age: 432000
  19. # https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
  20. - name: Cleanup on PR close
  21. if: github.event_name == 'pull_request'
  22. run: |
  23. gh extension install actions/gh-actions-cache
  24. REPO=${{ github.repository }}
  25. BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
  26. echo "Fetching list of cache key"
  27. cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 )
  28. ## Setting this to not fail the workflow while deleting cache keys.
  29. set +e
  30. echo "Deleting caches..."
  31. for cacheKey in $cacheKeysForPR
  32. do
  33. gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
  34. done
  35. echo "Done"
  36. env:
  37. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}