-
Notifications
You must be signed in to change notification settings - Fork 0
65 lines (63 loc) · 2.71 KB
/
cleanup-caches.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
# Mostly for DeterminateSystems/magic-nix-cache-action
name: 👋 caches
on:
push:
branches: [main]
pull_request:
types:
- closed
schedule:
# Every 12:42 JST
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
- cron: '42 3 * * *'
workflow_dispatch:
defaults:
run:
shell: 'bash -euxo pipefail {0}'
# Take care all jobs do not scope same reference. If then, the steps should be in one job and runs sequential
jobs:
prs:
# Basically skip to avoid rate limit with the N+1
if: github.event_name == 'workflow_dispatch'
concurrency:
group: '${{ github.workflow }}-prs' # Dont' use ${{ github.job }} here
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
# Intentionally cover all merged PRs to keep simple logics. See https://github.com/kachick/times_kachick/issues/311
- name: Cleanup caches in merged PRs
run: |
gh pr list --state merged --json number --jq '.[].number' --limit 2000 | \
xargs --no-run-if-empty -I '{}' gh cache list --sort size_in_bytes --order desc --json id --jq '.[].id' --limit 100 --ref 'refs/pull/{}/merge' | \
xargs --no-run-if-empty --max-lines=1 gh cache delete
env:
GH_TOKEN: ${{ github.token }}
pr:
if: github.event_name == 'pull_request'
concurrency:
group: '${{ github.workflow }}-pr-${{ github.event.pull_request.number }}' # Dont' use ${{ github.job }} here
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
# Intentionally cover all merged PRs to keep simple logics. See https://github.com/kachick/times_kachick/issues/311
- name: Cleanup caches in the PR
run: |
gh cache list --sort size_in_bytes --order desc --json id --jq '.[].id' --limit 100 --ref 'refs/pull/${{ github.event.pull_request.number }}/merge' | \
xargs --no-run-if-empty --max-lines=1 gh cache delete
env:
GH_TOKEN: ${{ github.token }}
main:
concurrency:
group: '${{ github.workflow }}-main' # Dont' use ${{ github.job }} here
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
# 10Gib limit is too small even if focused on default branch
- name: Cleanup caches in main branch
run: |
# We can lexicographical sort RFC3399 formatted timestamp
threshold="$(date --rfc-3339='date' --date='-4 days')"
gh cache list --limit 2000 --ref 'refs/heads/main' --sort last_accessed_at --order asc --json 'id,lastAccessedAt,sizeInBytes' --jq ".[] | select(.lastAccessedAt < \"$threshold\") | .id" | \
xargs --no-run-if-empty --max-lines=1 gh cache delete
env:
GH_TOKEN: ${{ github.token }}