Skip to content

Commit 3d7cf60

Browse files
authored
Update Delete Caches action [ci] (#266)
1 parent 51da35e commit 3d7cf60

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

.github/workflows/cache.yaml

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Delete Caches
22

33
on:
4-
pull_request:
4+
pull_request_target:
55
types: [closed]
66
workflow_dispatch:
77
inputs:
@@ -12,11 +12,42 @@ on:
1212

1313
jobs:
1414
delete:
15-
name: "Delete unused caches (PR #${{ github.event.inputs.pr-number || github.event.number }})"
15+
name: Delete caches
1616
runs-on: ubuntu-latest
1717
permissions:
1818
actions: write
19+
env:
20+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1921
steps:
20-
- uses: snnaplab/[email protected]
21-
with:
22-
ref: refs/pull/${{ github.event.inputs.pr-number || github.event.number }}/merge
22+
- name: Delete caches
23+
run: |
24+
ref="refs/pull/${{ github.event.inputs.pr-number || github.event.number }}/merge"
25+
sort="last_accessed_at"
26+
del_count=0
27+
28+
res=$(gh api --paginate \
29+
-H "Accept: application/vnd.github+json" \
30+
/repos/${{ github.repository }}/actions/caches?sort=$sort)
31+
32+
res_count=$(echo "$res" | jq '.actions_caches | length')
33+
if [[ "$res_count" -eq 0 ]]; then break; fi
34+
35+
targets=$(echo "$res" | jq \
36+
--arg ref "$ref" \
37+
'.actions_caches[] | select(.ref == $ref)
38+
| del(.created_at, .size_in_bytes, .version)')
39+
targets_count=$(echo "$targets" | jq -s 'length')
40+
if [[ "$targets_count" -eq 0 ]]; then break; fi
41+
echo "Found $targets_count caches"
42+
echo "$targets"
43+
44+
for id in $(echo "$targets" | jq '.id'); do
45+
echo "Delete cache with id: $id ..."
46+
gh api \
47+
--method DELETE \
48+
-H "Accept: application/vnd.github+json" \
49+
/repos/${{ github.repository }}/actions/caches/$id
50+
(( del_count += 1 ))
51+
done
52+
53+
echo "Deleted $del_count caches"

0 commit comments

Comments
 (0)