summaryrefslogtreecommitdiffstats
path: root/.github/workflows/cleanup_caches.yml
blob: b839cea15f169959bb0f3db5220e6a1d707c998b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
name: Cleanup caches

on:
  workflow_run:
    workflows: [build]
    types: [completed]

jobs:
  cache:
    runs-on: ubuntu-latest
    steps:
      - run: |
          gh cache list -L 100 --json id,key,ref -S last_accessed_at -O desc --jq '
            map(select(.key | startswith("x86_64-w64-mingw32-") or
                              startswith("i686-w64-mingw32-") or
                              startswith("x86_64-windows-msvc-"))) |
            group_by(.ref) |
            map({
              ref: .[0].ref,
              caches: map({
                        key: .key,
                        prefix: (.key | capture("^(?<prefix>[\\w_-]+-)\\d+$").prefix)
                      }) | group_by(.prefix) | map({keys: map(.key)})
            }) |
            .[]
          ' |
          while read -r group; do
            pr=$(echo "$group" | jq -r '.ref | capture("refs/pull/(?<num>[0-9]+)/merge").num')
            if [[ -n "$pr" ]] && [ "$(gh pr view $pr --json state --jq '.state')" != "OPEN" ]; then
              keys=$(echo "$group" | jq -c '.caches | map(.keys) | .[]')
            else
              keys=$(echo "$group" | jq -c '.caches | map(.keys[1:]) | .[]')
            fi
            for key in $(echo "$keys" | jq -r '.[]'); do
              gh cache delete "$key"
            done
          done
    env:
      GH_REPO: ${{ github.repository }}
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}