Skip to content
Open
Show file tree
Hide file tree
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
70 changes: 70 additions & 0 deletions .github/workflows/cleanup-closed-pr-caches.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Reclaim closed PR caches

on:
workflow_run:
workflows: [Retire closed PR verification]
types: [completed]

permissions: {}

jobs:
cleanup:
if: >-
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' &&
startsWith(github.event.workflow_run.display_title, 'Retire PR ') &&
github.event.workflow_run.path == '.github/workflows/retire-merged-pr.yml' &&
github.event.workflow_run.repository.id == github.event.repository.id &&
github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
runs-on: ubuntu-24.04
timeout-minutes: 10
permissions:
actions: write
pull-requests: read
steps:
- name: Reclaim the closed pull request's cache IDs
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
repo="repos/$GITHUB_REPOSITORY"
read -r number closed < <(jq -er '
.workflow_run.display_title |
capture("^Retire PR (?<number>[1-9][0-9]*) closed (?<closed>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)$") |
[.number, .closed] | @tsv' "$GITHUB_EVENT_PATH")
still_closed() {
jq -e --argjson number "$number" --arg closed "$closed" --slurpfile event "$GITHUB_EVENT_PATH" '
$event[0].workflow_run as $run |
.number == $number and .state == "closed" and .closed_at == $closed and
(.base.ref == "main" or .base.ref == "develop") and .base.repo.id == $run.repository.id and
(.head.repo.id | type == "number") and .head.repo.id == $run.head_repository.id and
.head.ref == $run.head_branch and .head.sha == $run.head_sha
' >/dev/null
}
pull=$(gh api "$repo/pulls/$number")
if ! still_closed <<< "$pull"; then
echo "PR $number no longer matches the recorded closure"
exit 0
fi
ref="refs/pull/$number/merge"
gh api --method GET --paginate --slurp "$repo/actions/caches" -f ref="$ref" -F per_page=100 > "$RUNNER_TEMP/pr-caches.json"
jq -r --arg ref "$ref" --arg closed "$closed" '
.[] | .actions_caches[] | select(.ref == $ref and
(.created_at | sub("Z$"; "")) <= ($closed | sub("Z$"; ".000000000"))) | .id
' "$RUNNER_TEMP/pr-caches.json" > "$RUNNER_TEMP/pr-cache-ids"
while read -r id; do
[[ "$id" =~ ^[1-9][0-9]*$ ]]
pull=$(gh api "$repo/pulls/$number")
if ! still_closed <<< "$pull"; then
echo "PR $number changed before cache deletion"
break
fi
status=$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--request DELETE --header "Authorization: Bearer $GH_TOKEN" \
"$GITHUB_API_URL/$repo/actions/caches/$id")
case "$status" in
204) echo "Reclaimed cache $id for closed PR $number" ;;
404) echo "Cache $id for closed PR $number was already absent" ;;
*) echo "::error::Could not reclaim cache $id for PR $number (HTTP $status)"; exit 1 ;;
esac
done < "$RUNNER_TEMP/pr-cache-ids"
2 changes: 1 addition & 1 deletion .github/workflows/retire-merged-pr-android.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Retire merged PR Android verification

on:
pull_request_target:
pull_request:
types: [closed]
branches: [main, develop]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/retire-merged-pr-promotion.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Retire merged PR promotion checks

on:
pull_request_target:
pull_request:
types: [closed]
branches: [main]

Expand Down
22 changes: 6 additions & 16 deletions .github/workflows/retire-merged-pr.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
name: Retire closed PR verification
run-name: Retire PR ${{ github.event.pull_request.number }} closed ${{ github.event.pull_request.closed_at }}

on:
pull_request_target:
pull_request:
types: [closed]
branches: [main, develop]

permissions:
actions: write
permissions: {}

concurrency:
group: ci-pr-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
cleanup-pr-caches:
name: Reclaim closed PR caches
runs-on: ubuntu-latest
timeout-minutes: 5
closed-pr-retired:
runs-on: ubuntu-24.04
steps:
- name: Delete GitHub Actions caches for closed PR
env:
GH_TOKEN: ${{ github.token }}
PR_REF: refs/pull/${{ github.event.pull_request.number }}/merge
run: |
ids=$(gh api --paginate "repos/${GITHUB_REPOSITORY}/actions/caches?ref=${PR_REF}&per_page=100" --jq '.actions_caches[].id' || true)
for id in $ids; do
gh api -X DELETE "repos/${GITHUB_REPOSITORY}/actions/caches/${id}" || true
done
- run: echo "Closed PR verification retired"