Skip to content

Commit 12662c0

Browse files
committed
ci(workflows): add cache-cleanup
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent edc7891 commit 12662c0

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Cache Cleanup
2+
#
3+
# Delete caches when a pull request is closed or on workflow dispatch.
4+
#
5+
# References:
6+
#
7+
# - https://docs.github.com/actions/learn-github-actions/contexts
8+
# - https://docs.github.com/actions/learn-github-actions/expressions
9+
# - https://docs.github.com/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
10+
# - https://docs.github.com/actions/using-workflows/events-that-trigger-workflows#pull_request
11+
# - https://docs.github.com/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch
12+
# - https://docs.github.com/actions/using-workflows/using-github-cli-in-workflows
13+
# - https://docs.github.com/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
14+
# - https://docs.github.com/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_dispatch
15+
# - https://github.com/actions/checkout
16+
# - https://github.com/actions/gh-actions-cache
17+
# - https://github.com/hmarr/debug-action
18+
19+
---
20+
name: cache-cleanup
21+
on:
22+
pull_request:
23+
types:
24+
- closed
25+
workflow_dispatch:
26+
inputs:
27+
all:
28+
default: false
29+
description: delete caches without filtering by github.ref
30+
type: boolean
31+
permissions:
32+
actions: write
33+
env:
34+
BRANCH: ${{ github.head_ref || github.ref_name }}
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
concurrency:
37+
cancel-in-progress: true
38+
group: ${{ github.workflow }}-${{ github.ref }}
39+
jobs:
40+
cache-cleanup:
41+
runs-on: ubuntu-latest
42+
steps:
43+
- id: debug
44+
name: Print environment variables and event payload
45+
uses: hmarr/debug-action@v2.1.0
46+
- id: checkout
47+
name: Checkout ${{ github.ref_name }}
48+
uses: actions/checkout@v3.5.0
49+
with:
50+
persist-credentials: false
51+
ref: ${{ github.ref }}
52+
- id: gh-actions-cache
53+
name: Install actions/gh-actions-cache
54+
run: gh extension install actions/gh-actions-cache
55+
- id: cleanup
56+
name: Delete caches${{ !inputs.all && format(' created by {0}', env.BRANCH) || '' }}
57+
env:
58+
BRANCH_FILTER: ${{ !inputs.all && format('--branch {0}', env.BRANCH) || '' }}
59+
run: |
60+
# prevent workflow failure while deleting cache keys
61+
set +e
62+
63+
# delete all caches or caches created by ${{ env.BRANCH }}
64+
for key in $(gh actions-cache list $BRANCH_FILTER --limit 100 | cut -f 1); do
65+
gh actions-cache delete $key $BRANCH_FILTER --confirm
66+
done

0 commit comments

Comments
 (0)