Skip to content

Cleanup Caches by Cron #8

Cleanup Caches by Cron

Cleanup Caches by Cron #8

name: Cleanup Caches by Cron
on:
schedule:
- cron: '0 3 * * *' # Runs daily at 03:00 UTC
workflow_dispatch: # Allows manual triggering if needed
permissions:
contents: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Set Up GitHub CLI
run: |
sudo apt-get update && sudo apt-get install -y gh
gh extension install actions/gh-actions-cache || echo "Extension already installed"
- name: Cleanup Caches
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
run: |
REPO=${{ github.repository }}
BRANCH="master" # Only focus on the master branch
echo "Fetching list of cache keys for branch: $BRANCH"
cacheKeysForMaster=$(gh actions-cache list -R $REPO -B $BRANCH --json key | jq -r '.[].key')
if [ -z "$cacheKeysForMaster" ]; then
echo "No caches found for branch: $BRANCH"
exit 0
fi
echo "Deleting caches..."
for cacheKey in $cacheKeysForMaster; do
echo "Deleting cache: $cacheKey"
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Cleanup completed."
- name: Verify Remaining Caches
run: |
echo "Checking for remaining caches in branch: $BRANCH"
remainingCaches=$(gh actions-cache list -R ${{ github.repository }} -B $BRANCH --json key | jq -r '.[].key')
if [ -z "$remainingCaches" ]; then
echo "All caches successfully cleared."
else
echo "Remaining caches detected:"
echo "$remainingCaches"
exit 1
fi
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}