ci/cleanup_caches: group by cache prefixes too

I've simplified this too much during refactoring, forgot about this. We
need to preserve the latest cache with each prefix.

Fixes: c6b950ae1e
This commit is contained in:
Kacper Michajłow 2024-05-19 21:28:08 +02:00
parent c6b950ae1e
commit 0c85b9915f
1 changed files with 6 additions and 3 deletions

View File

@ -18,16 +18,19 @@ jobs:
group_by(.ref) |
map({
ref: .[0].ref,
caches: map(.key)
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 -R ${{ github.repository }} $pr --json state --jq '.state')" != "OPEN" ]; then
keys=$(echo "$group" | jq -c '.caches')
keys=$(echo "$group" | jq -c '.caches | map(.keys) | .[]')
else
keys=$(echo "$group" | jq -c '.caches[1:]')
keys=$(echo "$group" | jq -c '.caches | map(.keys[1:]) | .[]')
fi
for key in $(echo "$keys" | jq -r '.[]'); do
gh cache delete "$key" -R ${{ github.repository }}