Skip to content

Commit ab85896

Browse files
ci: periodically prune old GitHub Actions caches
1 parent fc81024 commit ab85896

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

.github/workflows/prune-caches.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Prune Actions caches
2+
3+
on:
4+
schedule:
5+
- cron: '0 21 * * *'
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
actions: write
11+
12+
jobs:
13+
prune:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- run: rake ci:prune_caches
17+
env:
18+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

rakelib/ci.rake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,40 @@ namespace :ci do
132132
end
133133
end
134134

135+
desc "Purge old caches in GitHub Actions"
136+
task :purge_caches, [:days] do |_, args|
137+
RubyWasm::Toolchain.check_executable("gh")
138+
139+
days = (args[:days] || 1).to_i
140+
repo = ENV["GITHUB_REPOSITORY"] || "ruby/ruby.wasm"
141+
cutoff = (Time.now.utc - days * 86_400).iso8601
142+
143+
puts "Deleting caches last accessed before #{cutoff}…"
144+
145+
page = 1
146+
deleted = 0
147+
148+
loop do
149+
resp = `gh api -H 'Accept: application/vnd.github+json' "/repos/#{repo}/actions/caches?per_page=100&page=#{page}"`
150+
raise "gh api failed" unless $?.success?
151+
152+
data = JSON.parse(resp)
153+
caches = data["actions_caches"]
154+
155+
old = caches.select { |c| c["last_accessed_at"] < cutoff }
156+
old.each do |c|
157+
id = c["id"]
158+
system("gh api -X DELETE /repos/#{repo}/actions/caches/#{id}")
159+
deleted += 1
160+
end
161+
162+
break if caches.size < 100
163+
page += 1
164+
end
165+
166+
puts "Deleted #{deleted} caches older than #{days} days."
167+
end
168+
135169
desc "Publish artifacts as a GitHub Release"
136170
task :publish, [:tag] do |t, args|
137171
RubyWasm::Toolchain.check_executable("gh")

0 commit comments

Comments
 (0)