Skip to content

Instantly share code, notes, and snippets.

@snivas
Created October 29, 2021 19:18
Show Gist options
  • Save snivas/e47c7e64f2ea6738fcb61cf5d033dd72 to your computer and use it in GitHub Desktop.
Save snivas/e47c7e64f2ea6738fcb61cf5d033dd72 to your computer and use it in GitHub Desktop.
JFrog Cli script to prune docker repositories and keep latest N images
# JFrog Cli script to prune docker repositories and keep latest 5 images
# This file is called by main.sh script by passing repo name each time
# This script can also be called directly by passing "prune-docker-images.sh <docker-image-repo-name>"
#check if repo name is passed if not exit
[ -f "$1" ] && echo "Path not given " && exit 1
REPO="$1"
TMP_LOC="/tmp/a.json" # path to store values temperorily
LIMIT=4 # Number of images to keep. Latest is not included
export PATH=SPATH:/C/jfrog # path where jfrog exe resides
echo "Processing repo: $REPO"
jfrog.exe rt s "$REPO/*/manifest.json" --exclusions="*/latest/*" --sort-by created --sort-order asc > $TMP_LOC
TOTAL_IMGS=$(cat $TMP_LOC | jq.exe length )
echo "Total Images: $TOTAL_IMGS"
REMOVE_CNT=`expr $TOTAL_IMGS - $LIMIT`
echo "IMAGES to REMOVE: $REMOVE_CNT"
if [ "$REMOVE_CNT" -le 0 ]; then
echo "Less number of images $TOTAL_IMGS hence not deleting"
fi
PATH_ARR=$(cat $TMP_LOC | jq.exe '.[].path' | head -n $REMOVE_CNT )
for i in $PATH_ARR
do
P=$(dirname $i | tr -d \")
echo "DELETE $p"
jfrog.exe rt del $p --quiet
done
rm $TMP_LOC # Delete the temperoryily created json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment