Skip to content

CleanUpCache

CleanUpCache #1198

Workflow file for this run

name: CleanUpCache
on:
workflow_run:
workflows: [LinuxClang, cuda, LinuxGcc, hip, Hypre, intel, macos, PETSc, SUNDIALS, windows]
types:
- completed
jobs:
CleanUpCcacheCache:
name: Clean Up Ccahe Cache for ${{ github.event.workflow_run.name }}
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- name: Clean up ccahe
run: |
gh extension install actions/gh-actions-cache
REPO=${{ github.repository }}
# push or pull_request
EVENT=${{ github.event.workflow_run.event }}
# Triggering workflow run name (e.g., LinuxClang)
WORKFLOW_NAME=${{ github.event.workflow_run.name }}
if [[ $EVENT == "push" ]]; then
BRANCH=refs/heads/${{ github.event.workflow_run.head_branch }}
else
gh run download ${{ github.event.workflow_run.id }} -n pr_number
pr_number=`cat pr_number.txt`
BRANCH=refs/pull/${pr_number}/merge
fi
# Setting this to not fail the workflow while deleting cache keys.
set +e
# In our cache keys, substring after `-git-` is git hash, substring
# before that is a unique id for jobs (e.g., `ccache-LinuxClang-configure-2d`).
# The goal is to keep the last used key of each job and delete all others.
# something like ccache-LinuxClang-
keyprefix=ccache-${WORKFLOW_NAME}-
cached_jobs=$(gh actions-cache list -L 100 -R $REPO -B $BRANCH --key $keyprefix | awk -F '-git-' '{print $1}' | sort | uniq)
# cached_jobs is something like "ccache-LinuxClang-configure-1d ccache-LinuxClang-configure-2d".
for j in $cached_jobs
do
old_keys=$(gh actions-cache list -L 100 -R $REPO -B $BRANCH --key ${j}-git- --sort last-used | cut -f 1 | tail -n +2)
for k in $old_keys
do
gh actions-cache delete $k -R $REPO -B $BRANCH --confirm
done
done