Last active
June 8, 2022 13:50
-
-
Save andrimuhyidin/a05b2b18642f914a7725cf2572e85e8f to your computer and use it in GitHub Desktop.
Deleting unused image tags from Google Container Registry, leaving x number left
This file contains hidden or 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 | |
IFS=$'\n\t' | |
set -eou pipefail | |
if [[ "$#" -ne 3 || "${1}" == '-h' || "${1}" == '--help' ]]; then | |
cat >&2 <<"EOF" | |
gcr-cleaner.sh will cleans up the gcr tags or untaged images pushed in a repository (image name with the specific tag) | |
and except the given number most recent images. | |
USAGE: | |
./gcr-cleaner.sh IMAGE_NAME IMAGE_TAG NUMBER_OF_IMAGES_TO_REMAIN | |
EXAMPLE: | |
./gcr-cleaner.sh asia.gcr.io/adept-primer-345706/alpine service1-develop 10 | |
IMPACT: | |
Would clean up everything under the repository tag asia.gcr.io/test-project/alpine:service1-develop~ | |
pushed except for the 10 most recent images. If image have multiple tag will be delete all tags. | |
EOF | |
exit 1 | |
fi | |
main() { | |
local C=0 | |
IMAGE="${1}" | |
IMAGE_TAG="${2}" | |
NUMBER_OF_IMAGES_TO_REMAIN=$((${3} - 1)) | |
# 1. Get date and time the image want to retain | |
CUTOFF=$( | |
gcloud container images list-tags $IMAGE \ | |
--limit=unlimited \ | |
--sort-by=~TIMESTAMP \ | |
--flatten="[].tags[]" \ | |
--filter="tags~$IMAGE_TAG" \ | |
--format=json | TZ=/usr/share/zoneinfo/UTC jq -r '.['$NUMBER_OF_IMAGES_TO_REMAIN'].timestamp.datetime | sub("(?<before>.*):"; .before ) | strptime("%Y-%m-%d %H:%M:%S%z") | mktime | strftime("%Y-%m-%d %H:%M:%S%z")' | |
) | |
# sample output: 2022-03-29 00:40:05+0000 | |
# 2. Get the image list (digest format) | |
IMAGE_TAG_LIST=$( | |
gcloud container images list-tags $IMAGE \ | |
--limit=unlimited \ | |
--sort-by=~TIMESTAMP \ | |
--flatten="[].tags[]" \ | |
--filter="tags~$IMAGE_TAG AND timestamp.datetime < '${CUTOFF}'" \ | |
--format="get(digest)" | |
) | |
# sample output: sha256:d72e2d383f2d5fb1e8186ebfd1fbb22a87c04f52ac12fc379d21abb368d373df | |
# 3. List of images digest want to delete | |
echo "you will be delete the images tag with digest below:" | |
for digest in $IMAGE_TAG_LIST; do | |
( | |
set -x | |
echo $digest | |
) | |
let C=C+1 | |
done | |
echo "Total image to delete: ${C} images in ${IMAGE}:$IMAGE_TAG~." | |
# 4. Delete the image digest based on filter | |
echo "are you sure to delete it (y/n): " | |
read ans | |
if [ "$ans" == "y" ]; then | |
for digest in $IMAGE_TAG_LIST; do | |
( | |
set -x | |
gcloud container images delete -q --force-delete-tags "${IMAGE}@${digest}" | |
) | |
let C=C+1 | |
done | |
echo "deleted ${C} images in ${IMAGE}:$IMAGE_TAG~." >&2 | |
else | |
echo "INFO: Cancel the execution. Exit without any action" | |
exit 0 | |
fi | |
} | |
main ${1} ${2} ${3} |
Hi
How can we list child repository if we have multiple images?
Hi How can we list child repository if we have multiple images?
Can you give me a sample of the child repository?
eu.gcr.io/gcr-clean-repo/base
eu.gcr.io/gcr-clean-repo/base/alpine/image-digest
eu.gcr.io/gcr-clean-repo/base/mysql/image-digest
eu.gcr.io/gcr-clean-repo/base-2
eu.gcr.io/gcr-clean-repo/base-2/java/image-digest
eu.gcr.io/gcr-clean-repo/base-2/ruby/image-digest
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://gist.github.com/DaanGeurts/d95f67578d0e6f39fee05ff5e2375dc3