-
Notifications
You must be signed in to change notification settings - Fork 1
51 lines (47 loc) · 1.98 KB
/
cache-cleanup.yaml
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
49
50
51
---
name: Cache Cleanup
'on':
pull_request:
types: [closed]
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
renovate-cache-cleanup:
name: Renovate Cache Cleanup
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
name: Install `gh-actions-cache` CLI extension
run: gh extension install actions/gh-actions-cache
- id: cache-info
name: Get information for cache cleanup
env:
BRANCH: >-
${{
github.event_name == 'pull_request' && github.event.pull_request.merged && format('refs/pull/{0}/merge', github.event.pull_request.number)
|| github.event_name != 'pull_request' && github.ref
|| ''
}}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
KEY: ${{ github.repository_owner }}-renovate-cache-
run: |
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
if [ -n "$BRANCH" ]; then
# Get cache keys that don't include the branch's SHA (if not a PR)
gh actions-cache list -R ${{ github.repository }} -B $BRANCH -L 100 --key $KEY --order asc --sort last-used | cut -f 1 > cache-keys.txt
[ -z "${{ github.event.pull_request }}" ] && grep -v ${{ github.sha }} cache-keys.txt > cache-keys.txt.tmp && mv cache-keys.txt.tmp cache-keys.txt
echo cache-keys="$(cat cache-keys.txt | tr '\n' ' ')" >> $GITHUB_OUTPUT
fi
- env:
CACHE_KEYS: ${{ steps.cache-info.outputs.cache-keys }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: ${{ steps.cache-info.outputs.branch != '' }}
name: Delete stale cache entries from ${{ steps.cache-info.outputs.branch }}
run: |
set +e
for key in $CACHE_KEYS; do
gh actions-cache delete $key -R ${{ github.repository }} -B ${{ steps.cache-info.outputs.branch }} --confirm
done