-
-
Notifications
You must be signed in to change notification settings - Fork 0
67 lines (65 loc) · 2.49 KB
/
cache-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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Cache Cleanup
#
# Delete caches when a pull request is closed or on workflow dispatch.
#
# References:
#
# - https://docs.github.com/actions/learn-github-actions/contexts
# - https://docs.github.com/actions/learn-github-actions/expressions
# - https://docs.github.com/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
# - https://docs.github.com/actions/using-workflows/events-that-trigger-workflows#pull_request
# - https://docs.github.com/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch
# - https://docs.github.com/actions/using-workflows/using-github-cli-in-workflows
# - https://docs.github.com/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
# - https://docs.github.com/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_dispatch
# - https://github.com/actions/checkout
# - https://github.com/actions/gh-actions-cache
# - https://github.com/hmarr/debug-action
---
name: cache-cleanup
on:
pull_request:
types:
- closed
workflow_dispatch:
inputs:
all:
default: false
description: delete caches without filtering by branch
type: boolean
permissions:
actions: write
env:
BRANCH: |
${{ github.event.number && format('refs/pull/{0}/merge', github.event.number) || github.ref }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
concurrency:
cancel-in-progress: true
group: ${{ github.workflow }}-${{ format('refs/heads/{0}', github.head_ref || github.ref_name) }}
jobs:
cache-cleanup:
runs-on: ubuntu-latest
steps:
- id: debug
name: Print environment variables and event payload
uses: hmarr/debug-action@v3.0.0
- id: checkout
name: Checkout main
uses: actions/checkout@v4.1.1
with:
persist-credentials: false
ref: main
- id: gh-actions-cache
name: Install actions/gh-actions-cache
run: gh extension install actions/gh-actions-cache
- id: cleanup
name: Delete caches${{ !inputs.all && format(' created by {0}', env.BRANCH) || '' }}
env:
BRANCH_FILTER: ${{ !inputs.all && format('--branch {0}', env.BRANCH) || '' }}
run: |
# prevent workflow failure while deleting cache keys
set +e
# delete all caches or caches created by ${{ env.BRANCH }}
for key in $(gh actions-cache list $BRANCH_FILTER --limit 100 | cut -f 1); do
gh actions-cache delete $key $BRANCH_FILTER --confirm
done