Cache Cleanup #1471
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: Cache Cleanup | |
| # Prune superseded rust-cache generations right after new caches are saved. | |
| # Caches are only saved on master (`save-if: master`), so this runs whenever | |
| # the cache-producing workflows finish on master. Keeping only the newest | |
| # generation per logical key bounds the repo cache at ~1 generation/key, | |
| # regardless of how often dependabot churns Cargo.lock. | |
| on: | |
| workflow_run: | |
| workflows: ["Regression", "CodSpeed"] | |
| types: [completed] | |
| permissions: | |
| actions: write | |
| jobs: | |
| prune: | |
| if: github.event.workflow_run.head_branch == 'master' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Delete superseded cache generations | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| # Group caches by their logical key, keep the newest by created_at in | |
| # each group, and delete the rest. rust-cache keys end in | |
| # `-<rustc-hash>-<lockfile-hash>`; sccache keys end in a single | |
| # `-<commit-sha>` segment. | |
| gh api --paginate "repos/$REPO/actions/caches" \ | |
| --jq '.actions_caches[] | [.key, .created_at, (.id|tostring)] | @tsv' \ | |
| | awk -F'\t' '{ g=$1; if (g ~ /^sccache-/) sub(/-[0-9a-f]+$/,"",g); else sub(/-[0-9a-f]+-[0-9a-f]+$/,"",g); print g"\t"$2"\t"$3 }' \ | |
| | sort -t"$(printf '\t')" -k1,1 -k2,2r \ | |
| | awk -F'\t' '{ if ($1==p) print $3; p=$1 }' \ | |
| | while read -r id; do | |
| echo "delete cache $id" | |
| gh api -X DELETE "repos/$REPO/actions/caches/$id" || echo "failed to delete $id" | |
| done |