cache-prune #159
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-prune | |
| # Keeps the repository's total cache size below a fixed limit, so that GitHub never | |
| # rejects a save for being over the allowance. | |
| on: | |
| schedule: | |
| - cron: "37 */6 * * *" # every six hours at :37 | |
| workflow_dispatch: | |
| workflow_call: | |
| permissions: {} | |
| concurrency: | |
| group: cache-prune | |
| jobs: | |
| prune: | |
| runs-on: ubuntu-latest | |
| if: github.repository == 'go-gitea/gitea' | |
| permissions: | |
| actions: write # to delete caches | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| GH_REPO: ${{ github.repository }} | |
| steps: | |
| # Keep the newest generation per key, restores never reach the older ones. | |
| - name: delete superseded caches | |
| run: | | |
| gh cache list --limit 1000 --json id,key,ref,createdAt | | |
| jq -r 'group_by([.ref, (.key | sub("(-go[0-9.]+)?-[0-9a-f]{40,64}(-[0-9]+-[0-9]+)?$"; ""))])[] | |
| | sort_by(.createdAt)[:-1][] | "\(.id) \(.key)"' | | |
| while read -r id key; do | |
| echo "deleting $key" | |
| gh cache delete "$id" || true | |
| done | |
| # Deletes least recently used first, the order GitHub itself evicts in. | |
| - name: delete caches over the size limit | |
| run: | | |
| caches=$(gh cache list --limit 1000 --sort last_accessed_at --order asc --json id,key,sizeInBytes) | |
| size=$(jq '[.[].sizeInBytes] | add // 0' <<< "$caches") | |
| echo "cache usage: $((size / 1000000)) MB" | |
| while [ "$size" -gt 6000000000 ] && read -r id bytes key; do | |
| echo "deleting $key" | |
| gh cache delete "$id" || true | |
| size=$((size - bytes)) | |
| done <<< "$(jq -r '.[] | "\(.id) \(.sizeInBytes) \(.key)"' <<< "$caches")" |