Last active
July 15, 2024 09:15
-
-
Save matzegebbe/a2678b227add6bafad9a3a802618b5ad to your computer and use it in GitHub Desktop.
Nexus Repository Manager keep the last X docker images delete all other
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 | |
REPO_URL="https://repository.xxx.net/repository/" | |
USER="admin" | |
PASSWORD="datpassword" | |
BUCKET="portal-docker" | |
KEEP_IMAGES=10 | |
IMAGES=$(curl --silent -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/_catalog" | jq .repositories | jq -r '.[]' ) | |
echo ${IMAGES} | |
for IMAGE_NAME in ${IMAGES}; do | |
echo ${IMAGE_NAME} | |
TAGS=$(curl --silent -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/tags/list" | jq .tags | jq -r '.[]' ) | |
TAG_COUNT=$(echo $TAGS | wc -w) | |
let TAG_COUNT_DEL=${TAG_COUNT}-${KEEP_IMAGES} | |
COUNTER=0 | |
echo "THERE ARE ${TAG_COUNT} IMAGES FOR ${IMAGE_NAME}" | |
## skip if smaller than keep | |
if [ "${KEEP_IMAGES}" -gt "${TAG_COUNT}" ] | |
then | |
echo "There are only ${TAG_COUNT} Images for ${IMAGE_NAME} - nothing to delete" | |
continue | |
fi | |
for TAG in ${TAGS}; do | |
let COUNTER=COUNTER+1 | |
if [ "${COUNTER}" -gt "${TAG_COUNT_DEL}" ] | |
then | |
break; | |
fi | |
IMAGE_SHA=$(curl --silent -I -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/manifests/$TAG" | grep Docker-Content-Digest | cut -d ":" -f3 | tr -d '\r') | |
echo "DELETE ${TAG} ${IMAGE_SHA}"; | |
DEL_URL="${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/manifests/sha256:${IMAGE_SHA}" | |
RET="$(curl --silent -k -X DELETE -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} $DEL_URL)" | |
done; | |
done; |
I think this shouldn't be based on tags .. but created datetime. We use commit hashes as versions for example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be aware that the ordering of tags might not what you think! So probably 10 old builds are kept and new ones are deleted!
e.g.
https://your-repo.com/repository/docker-repo/v2/image-name/tags/list