From 0c5d92d73a76dd0f9b7c62de08f7894e2f3bb22a Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 15 Jan 2024 10:15:36 -0600 Subject: [PATCH] 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 --- .github/workflows/CI.yml | 23 ++++++++------ action.yml | 8 ++--- handle_caches.jl | 69 +++++++++++++++++++++++----------------- 3 files changed, 57 insertions(+), 43 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 68146f8..c6febca 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -25,12 +25,11 @@ jobs: outputs: cache-name: ${{ steps.name.outputs.cache-name }} steps: - - name: Generate random file - shell: 'julia --color=yes {0}' - run: 'write("random.txt", string(rand(10)))' - - name: Set cache-name as output + - name: Generate random cache-name id: name - run: echo "cache-name=${{ hashFiles('random.txt') }}" >> $GITHUB_OUTPUT + run: | + cache_name=$(head -n 100 "\\\"") + 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