[Cron] Clear cache #19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "[Cron] Clear cache" | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "*/15 2-4 * * *" | |
pull_request: | |
types: | |
- closed | |
permissions: | |
actions: write | |
jobs: | |
# https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries | |
pr-cleanup: | |
if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Closed PR Cleanup | |
run: | | |
gh extension install actions/gh-actions-cache | |
echo "Fetching list of cache key" | |
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 ) | |
## Setting this to not fail the workflow while deleting cache keys. | |
set +e | |
echo "Deleting caches..." | |
for cacheKey in $cacheKeysForPR | |
do | |
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm | |
done | |
echo "Done" | |
env: | |
GH_TOKEN: ${{ github.token }} | |
REPO: ${{ github.repository }} | |
BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge | |
scheduled-cleanup: | |
if: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Clear Cache | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
console.log("About to clear") | |
const caches = await github.rest.actions.getActionsCacheList({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}) | |
for (const cache of caches.data.actions_caches) { | |
console.log(cache) | |
github.rest.actions.deleteActionsCacheById({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
cache_id: cache.id, | |
}) | |
} | |
console.log("Clear completed") |