This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
82 lines (76 loc) · 2.5 KB
/
clean.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
name: Clean
permissions:
contents: write
pull-requests: read
actions: write
defaults:
run:
shell: bash
on:
schedule:
- cron: '0 */12 * * *'
workflow_dispatch: { }
jobs:
clean:
timeout-minutes: 5
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1
persist-credentials: false
- name: Remove Cache
uses: actions/github-script@v6
with:
# clean up caches,
# ref to https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries,
# and https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache.
script: |
const owner = context.repo.owner
const repo = context.repo.repo
var deleteCaches = new Array()
// get candidate items.
const { data: cs } = await github.rest.actions.getActionsCacheList({
owner: owner,
repo: repo,
});
for (const c of cs.actions_caches) {
// clean closed pull request's caches.
if (c.ref.match(/^refs\/pull\/.*$/)) {
var prNum = c.ref.replace(/[^\d]/g, "")
const { data: pr } = await github.rest.pulls.get({
owner: owner,
repo: repo,
pull_number: prNum,
})
if (pr.state === 'closed') {
deleteCaches.push(c)
}
continue
}
// do not clean toolbox caches.
if (c.key.match(/^toolbox-.*$/)) {
continue
}
// clean push archived caches.
if (c.key.match(/^archive-.*$/)) {
deleteCaches.push(c)
continue
}
// clean stale built caches.
if (!c.key.match(/^setup-go-.*-${{ hashFiles('**/go.sum') }}$/)) {
deleteCaches.push(c)
continue
}
}
// delete
for (const c of deleteCaches) {
await github.rest.actions.deleteActionsCacheById({
owner: owner,
repo: repo,
cache_id: c.id,
})
console.log(`cleaned cache "${c.key}"`)
}
continue-on-error: true