Repository navigation
49 lines (42 loc) · 1.58 KB
/
Copy pathclean_cache.yml
File metadata and controls
49 lines (42 loc) · 1.58 KB
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
# https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
name: Cleanup caches for closed pull requests
on:
pull_request:
types:
- closed
schedule:
- cron: "0 0 */3 * *"
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
actions: write
pull-requests: read
steps:
- name: Cleanup
shell: bash
run: |
set -euo pipefail
if [[ "$EVENT_NAME" == "pull_request" ]]; then
ref="refs/pull/$PR_NUMBER/merge"
echo "Deleting caches for closed pull request $PR_NUMBER"
gh cache delete --all --succeed-on-no-caches --repo "$REPO" --ref "$ref"
exit 0
fi
echo "Finding caches for closed pull requests"
refs=$(gh cache list --repo "$REPO" --limit 1000 --json ref --jq '.[].ref' | sort -u)
while IFS= read -r ref; do
[[ "$ref" =~ ^refs/pull/([0-9]+)/merge$ ]] || continue
pr_number="${BASH_REMATCH[1]}"
state=$(gh pr view "$pr_number" --repo "$REPO" --json state --jq '.state')
if [[ "$state" == "CLOSED" ]]; then
echo "Deleting caches for closed pull request $pr_number"
gh cache delete --all --succeed-on-no-caches --repo "$REPO" --ref "$ref"
fi
done <<< "$refs"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
EVENT_NAME: ${{ github.event_name }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}