Cleanup #12
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: Cleanup | |
on: | |
delete: | |
workflow_dispatch: | |
pull_request: | |
types: | |
- closed | |
permissions: | |
actions: write | |
env: | |
BRANCH: ${{ github.event_name == 'pull_request' && 'refs/pull/${{ github.event.pull_request.number }}/merge' || github.event.ref }} | |
jobs: | |
cleanup: | |
name: Cleanup at event ${{ github.event_name }} for branch ${{ github.event_type == 'pull_request' && 'refs/pull/${{ github.event.pull_request.number }}/merge' || github.event.ref }} | |
if: github.event.ref_type == 'branch' || github.event_name == 'workflow_dispatch' || github.event_name == 'pull_request' | |
runs-on: ubuntu-24.04 | |
steps: | |
- name: Branch Caches | |
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..." | |
deletedAny=0 | |
for cacheKey in $cacheKeysForPR | |
do | |
deletedAny=1 | |
gh cache delete $cacheKey | |
exitCode="$?" | |
if [ "$exitCode" -eq 0 ]; then | |
echo "deleted cache entry $cacheKey" | |
else | |
echo "failed to delete cache entry $cacheKey - exited with code $exitCode" | |
fi | |
done | |
if [ "$deletedAny" -eq 0 ]; then | |
echo "No cache entries found" | |
else | |
echo "Done" | |
fi | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GH_REPO: ${{ github.repository }} |