Last active
August 29, 2023 12:37
-
-
Save xalvarez/70587e4c8195787e2b2d329a7e2a591e to your computer and use it in GitHub Desktop.
Clear a GitHub repository's cache
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
#!/bin/bash -e | |
# Note: For actions-cache to work one must pass or set a GITHUB_TOKEN variable with a valid PAT. | |
function print_help { | |
echo "usage: $0 -r <user/repository_name> [-b <branch>]" | |
echo " -r Repository. The format must be: <user/repository_name>" | |
echo " -b Delete cache for specific branch only" | |
echo "e.g.: $0 -r xalvarez/github-team-dashboard -b main" | |
} | |
while getopts "r:b:h" option; do | |
case "${option}" in | |
r) | |
REPOSITORY=${OPTARG} | |
;; | |
b) | |
BRANCH=${OPTARG} | |
;; | |
h | *) | |
print_help | |
exit 0 | |
;; | |
esac | |
done | |
if [ -z "$REPOSITORY" ]; then | |
echo "ERROR: Missing repository argument" | |
print_help | |
exit 1 | |
fi | |
gh extension install --force actions/gh-actions-cache | |
echo "Retrieving cache entries..." | |
CACHE_ENTRIES=$(gh actions-cache list -R "$REPOSITORY" ${BRANCH:+"-B $BRANCH"} -L 100 --sort last-used | cut -f 1 | uniq -u) | |
if [ -z "$CACHE_ENTRIES" ]; then | |
echo "Did not find any cache entries" | |
exit 0 | |
fi | |
for cacheEntry in ${CACHE_ENTRIES}; do | |
echo "Deleting cache entry $cacheEntry" | |
gh actions-cache delete "$cacheEntry" -R "$REPOSITORY" ${BRANCH:+"-B $BRANCH"} --confirm | true | |
done | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment