Skip to content

Commit

Permalink
ci: update cache-cleanup and schedule it weekly
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown committed Nov 12, 2024
1 parent 8827b62 commit d2f2649
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions .github/workflows/cache-cleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,34 @@ name: Cache Cleanup
'on':
pull_request:
types: [closed]
schedule:
- cron: '0 0 * * 0' # Every Sunday at 00:00
workflow_dispatch:

permissions: {}

jobs:
cleanup:
name: Cleanup cache
runs-on: ubuntu-latest
env:
BRANCH: >-
${{
github.event_name == 'pull_request' && github.event.pull_request.merged && format('refs/pull/{0}/merge', github.event.pull_request.number)
|| github.event_name != 'pull_request' && github.ref
|| ''
}}
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- id: cache-info
name: Get branch name and cache keys
- if: env.BRANCH != ''
name: Cleanup stale cache entries on ${{ env.BRANCH }}
env:
BRANCH: >-
${{
github.event_name == 'pull_request' && github.event.pull_request.merged && format('refs/pull/{0}/merge', github.event.pull_request.number)
|| github.event_name != 'pull_request' && github.ref
|| ''
}}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh extension install actions/gh-actions-cache
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
if [ -n "$BRANCH" ]; then
# Get cache keys that don't include the branch's SHA (if not a PR)
gh actions-cache list -R ${{ github.repository }} -B $BRANCH --order asc --sort last-used | cut -f 1 > cache-keys.txt
[ -z "${{ github.event.pull_request }}" ] && grep -v ${{ github.sha }} cache-keys.txt > cache-keys.txt.tmp && mv cache-keys.txt.tmp cache-keys.txt
echo cache-keys="$(cat cache-keys.txt | tr '\n' ' ')" >> $GITHUB_OUTPUT
fi
- env:
CACHE_KEYS: ${{ steps.cache-info.outputs.cache-keys }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: ${{ steps.cache-info.outputs.branch != '' }}
name: Delete stale cache entries from ${{ steps.cache-info.outputs.branch }}
run: |
set +e
# Get cache keys that don't include the branch's SHA (if not a PR)
gh actions-cache list -R ${{ github.repository }} -B $BRANCH --order asc --sort last-used | cut -f 1 > cache-keys.txt
[ -z "${{ github.event.pull_request }}" ] && grep -v ${{ github.sha }} cache-keys.txt > cache-keys.txt.tmp && mv cache-keys.txt.tmp cache-keys.txt
CACHE_KEYS="$(cat cache-keys.txt | tr '\n' ' ')"
for key in $CACHE_KEYS; do
gh actions-cache delete $key -R ${{ github.repository }} -B ${{ steps.cache-info.outputs.branch }} --confirm
gh actions-cache delete $key -R ${{ github.repository }} -B $BRANCH --confirm
done

0 comments on commit d2f2649

Please sign in to comment.