-
Notifications
You must be signed in to change notification settings - Fork 0
48 lines (44 loc) · 1.57 KB
/
cleanup.yml
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
41
42
43
44
45
46
47
48
name: Cleanup
on:
delete:
workflow_dispatch:
pull_request:
types:
- closed
permissions:
actions: write
env:
BRANCH: ${{ github.event_type == 'pull_request' && 'refs/pull/${{ github.event.pull_request.number }}/merge' || github.event.ref }}
jobs:
cleanup:
name: Cleanup at event ${{ github.event_name }} for branch ${{ github.event_type == 'pull_request' && 'refs/pull/${{ github.event.pull_request.number }}/merge' || github.event.ref }}
if: github.event.ref_type == 'branch' || github.event_name == 'workflow_dispatch' || github.event_name == 'pull_request'
runs-on: ubuntu-24.04
steps:
- name: Branch Caches
run: |
echo "Fetching list of cache key"
cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id')
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
deletedAny=0
for cacheKey in $cacheKeysForPR
do
deletedAny=1
gh cache delete $cacheKey
exitCode="$?"
if [ "$exitCode" -eq 0 ]; then
echo "deleted cache entry $cacheKey"
else
echo "failed to delete cache entry $cacheKey - exited with code $exitCode"
fi
done
if [ "$deletedAny" -eq 0 ]; then
echo "No cache entries found"
else
echo "Done"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}