mirror of
https://github.com/julia-actions/cache.git
synced 2026-02-12 01:16:54 +08:00
Delete cache entries only on the workflow branch (#97)
* Delete cache entries on the workflow branch * Grant permissions for cache cleanup * Add delete-old-caches required for testing purposes * Revise help message * Faster generate-key * Use distinct cache-names for matrix/no-matrix jobs * Remove redundant permissions * Better fork detection logic
This commit is contained in:
@@ -1,58 +1,67 @@
|
||||
using Pkg, Dates
|
||||
function handle_caches()
|
||||
repo = ARGS[1]
|
||||
func = ARGS[2]
|
||||
restore_key = get(ARGS, 3, "")
|
||||
subcommand = ARGS[1]
|
||||
|
||||
if func == "list"
|
||||
if subcommand == "list"
|
||||
repo = ARGS[2]
|
||||
println("Listing existing caches")
|
||||
run(`gh cache list --limit 100 --repo $repo`)
|
||||
elseif func == "rm"
|
||||
caches = String[]
|
||||
failed = String[]
|
||||
for _ in 1:5 # limit to avoid accidental rate limiting
|
||||
hits = split(strip(read(`gh cache list --limit 100 --repo $repo`, String)), keepempty=false)
|
||||
search_again = length(hits) == 100
|
||||
filter!(startswith(restore_key), hits)
|
||||
isempty(hits) && break
|
||||
# We can delete everything that matches the restore key because the new cache is saved later.
|
||||
for c in hits
|
||||
elseif subcommand == "rm"
|
||||
repo, restore_key, ref = ARGS[2:4]
|
||||
allow_failure = ARGS[5] == "true"
|
||||
|
||||
endpoint = "/repos/$repo/actions/caches"
|
||||
page = 1
|
||||
per_page = 100
|
||||
escaped_restore_key = replace(restore_key, "\"" => "\\\"")
|
||||
query = ".actions_caches[] | select(.key | startswith(\"$escaped_restore_key\")) | .id"
|
||||
|
||||
deletions = String[]
|
||||
failures = String[]
|
||||
while 1 <= page <= 5 # limit to avoid accidental rate limiting
|
||||
cmd = `gh api -X GET $endpoint -F ref=$ref -F per_page=$per_page -F page=$page --jq $query`
|
||||
ids = split(read(cmd, String); keepempty=false)
|
||||
page = length(ids) == per_page ? page + 1 : -1
|
||||
|
||||
# We can delete all cache entries on this branch that matches the restore key
|
||||
# because the new cache is saved later.
|
||||
for id in ids
|
||||
try
|
||||
run(`gh cache delete $(split(c)[1]) --repo $repo`)
|
||||
push!(caches, c)
|
||||
run(`gh cache delete $id --repo $repo`)
|
||||
push!(deletions, id)
|
||||
catch e
|
||||
@error e
|
||||
push!(failed, c)
|
||||
push!(failures, id)
|
||||
end
|
||||
end
|
||||
search_again || break
|
||||
end
|
||||
if isempty(failed) && isempty(caches)
|
||||
println("No existing caches found for restore key `$restore_key`")
|
||||
if isempty(failures) && isempty(deletions)
|
||||
println("No existing caches found on ref `$ref` matching restore key `$restore_key`")
|
||||
else
|
||||
if !isempty(failed)
|
||||
println("Failed to delete $(length(failed)) existing caches for restore key `$restore_key`")
|
||||
println.(failed)
|
||||
if !isempty(failures)
|
||||
println("Failed to delete $(length(failures)) existing caches on ref `$ref` matching restore key `$restore_key`")
|
||||
println.(failures)
|
||||
@info """
|
||||
To delete caches you need to grant the following to the default `GITHUB_TOKEN` by adding
|
||||
this to your yml:
|
||||
this to your workflow:
|
||||
```
|
||||
permissions:
|
||||
actions: write
|
||||
contents: read
|
||||
actions: write
|
||||
contents: read
|
||||
```
|
||||
(Note this won't work for fork PRs but should once merged)
|
||||
Or provide a token with `repo` scope via the `token` input option.
|
||||
See https://cli.github.com/manual/gh_cache_delete
|
||||
"""
|
||||
allow_failure || exit(1)
|
||||
end
|
||||
if !isempty(caches)
|
||||
println("$(length(caches)) existing caches deleted that match restore key `$restore_key`:")
|
||||
println.(caches)
|
||||
if !isempty(deletions)
|
||||
println("Deleted $(length(deletions)) caches on ref `$ref` matching restore key `$restore_key`")
|
||||
println.(deletions)
|
||||
end
|
||||
end
|
||||
else
|
||||
throw(ArgumentError("Unexpected second argument: $func"))
|
||||
throw(ArgumentError("Unexpected subcommand: $subcommand"))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user