Skip to content

Commit

Permalink
Refine cache clean action
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Oct 28, 2024
1 parent 863447c commit e1e11f5
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/cleanup-caches.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# Mostly for DeterminateSystems/magic-nix-cache-action
name: 👋 caches in a branch
name: 👋 caches in merged PRs
on:
pull_request:
types:
- closed
schedule:
# Every 12:42 JST
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
- cron: '42 3 * * *'
workflow_dispatch:

jobs:
cleanup:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Intentionally cover all merged PRs to keep simple logics. See https://github.com/kachick/times_kachick/issues/311
- name: Cleanup
run: |
echo "Fetching list of cache key"
cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id')
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
do
gh cache delete $cacheKey
done
echo "Done"
gh pr list --state merged --json number --jq '.[].number' --limit 2000 | \
xargs --no-run-if-empty -I '{}' gh cache list --sort size_in_bytes --order desc --json id --jq '.[].id' --limit 100 --ref 'refs/pull/{}/merge' | \
xargs --no-run-if-empty --max-lines=1 gh cache delete
env:
GH_TOKEN: ${{ github.token }}
BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge

0 comments on commit e1e11f5

Please sign in to comment.