|
| 1 | +# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions |
| 2 | +# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners |
| 3 | +name: ccache-cleanup |
| 4 | + |
| 5 | +# hendrikmuhs/ccache-action saves a new cache entry with a timestamp suffix on every run but never |
| 6 | +# deletes the previous ones. Only the newest entry is ever restored, so the superseded ones just take |
| 7 | +# up space and push the repository over the cache size limit, which causes other caches to be evicted. |
| 8 | + |
| 9 | +on: |
| 10 | + workflow_run: |
| 11 | + workflows: |
| 12 | + - CI-mingw |
| 13 | + - CI-unixish |
| 14 | + - CI-unixish-docker |
| 15 | + - Coverage |
| 16 | + - corpus |
| 17 | + - sanitizers |
| 18 | + - scriptcheck |
| 19 | + - selfcheck |
| 20 | + - valgrind |
| 21 | + types: |
| 22 | + - completed |
| 23 | + schedule: |
| 24 | + - cron: '0 3 * * *' |
| 25 | + workflow_dispatch: |
| 26 | + |
| 27 | +permissions: |
| 28 | + contents: read |
| 29 | + |
| 30 | +concurrency: |
| 31 | + group: ccache-cleanup |
| 32 | + cancel-in-progress: false |
| 33 | + |
| 34 | +jobs: |
| 35 | + cleanup: |
| 36 | + |
| 37 | + # pull requests do not save ccache caches |
| 38 | + if: github.event_name != 'workflow_run' || github.event.workflow_run.event != 'pull_request' |
| 39 | + |
| 40 | + runs-on: ubuntu-24.04 |
| 41 | + |
| 42 | + permissions: |
| 43 | + actions: write |
| 44 | + |
| 45 | + steps: |
| 46 | + - name: Delete superseded ccache caches |
| 47 | + env: |
| 48 | + GH_TOKEN: ${{ github.token }} |
| 49 | + REPO: ${{ github.repository }} |
| 50 | + run: | |
| 51 | + # list all ccache entries, group them by ref and key without the timestamp suffix and keep only the newest of each group |
| 52 | + gh api --paginate "repos/$REPO/actions/caches?key=ccache-&per_page=100" --jq '.actions_caches[]' | |
| 53 | + jq -s -r ' |
| 54 | + map(. + {prefix: (.key | sub("-[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]+Z$"; ""))}) |
| 55 | + | group_by([.ref, .prefix])[] |
| 56 | + | sort_by(.created_at) | .[:-1][] |
| 57 | + | "\(.id) \(.size_in_bytes) \(.ref) \(.key)"' > superseded.txt |
| 58 | + echo "superseded ccache caches: $(wc -l < superseded.txt)" |
| 59 | + while read -r id size ref key; do |
| 60 | + echo "deleting $key ($ref, $((size / 1048576)) MiB)" |
| 61 | + # the cache might already have been deleted or evicted |
| 62 | + gh api -X DELETE "repos/$REPO/actions/caches/$id" || true |
| 63 | + done < superseded.txt |
0 commit comments