Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions cache-cleanup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,38 @@ runs:
REPOSITORY: ${{ github.repository }}
COMMIT_SHA: ${{ github.sha }}
run: |
# Find the PR that produced this merge commit
PR_DATA=$(gh api /repos/${REPOSITORY}/commits/${COMMIT_SHA}/pulls --jq '.[0] | {number, branch: .head.ref}')
set -euo pipefail # fail fast on errors

echo "Running as actor: $(gh api user --jq .login)"
echo "Token scopes check (should include actions:write):"
gh auth status || true

PR_DATA=$(gh api --jq '.[0] | {number, branch: .head.ref}' \
"/repos/${REPOSITORY}/commits/${COMMIT_SHA}/pulls")

if [ -z "$PR_DATA" ] || [ "$PR_DATA" = "null" ]; then
echo "No PR found for commit ${COMMIT_SHA}, skipping cache cleanup"
echo "No PR found for commit ${COMMIT_SHA} → likely not a merge commit, skipping"
exit 0
fi

PR_NUMBER=$(echo "$PR_DATA" | jq -r '.number')
PR_BRANCH=$(echo "$PR_DATA" | jq -r '.branch')
echo "Cleaning caches for PR #${PR_NUMBER} (branch: ${PR_BRANCH})"

# Delete caches scoped to the PR merge ref and the head branch
for ref in "refs/pull/${PR_NUMBER}/merge" "refs/heads/${PR_BRANCH}"; do
echo "Checking ref: $ref"
gh actions-cache list --repo "${REPOSITORY}" --branch "$ref" --limit 100 \
--json id,sizeInBytes,key | \
jq -r '.[] | "\(.id)\t\(.sizeInBytes)\t\(.key)"' | \
echo "→ Ref: $ref"
caches=$(gh actions-cache list --repo "${REPOSITORY}" --branch "$ref" --limit 100 --json id,sizeInBytes,key 2>&1 || echo "[]")

if [[ "$caches" == *"403"* || "$caches" == *"forbidden"* ]]; then
echo "ERROR: 403 on list → missing actions:write permission"
exit 1
fi

echo "$caches" | jq -r '.[] | "\(.id)\t\(.sizeInBytes)\t\(.key)"' | \
while IFS=$'\t' read -r id size key; do
echo " Deleting: $key ($(( size / 1048576 )) MB)"
gh actions-cache delete "$id" --repo "${REPOSITORY}" --confirm
echo " Deleting: $key ($(( size / 1048576 )) MiB)"
gh actions-cache delete "$id" --repo "${REPOSITORY}" --confirm || echo "Delete failed (already gone?)"
done
done

echo "Cache cleanup complete"
echo "Cleanup finished"