Last active
September 7, 2018 10:36
-
-
Save viet-wego/29adebc362403c45090894c4588fa506 to your computer and use it in GitHub Desktop.
Bash script to clean up Google Container Registry by keeping recently images
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 | |
project=your-awesome-project-id | |
main() { | |
for image in $(gcloud container images list --repository=gcr.io/$project); do | |
if [[ $image != 'NAME' ]]; then | |
# clean up dummy tags | |
clean_up_image $image "NOT tags:dev* AND NOT tags:staging* AND NOT tags:production* AND NOT tags:latest" 0 | |
clean_up_image $image "dev" 3 | |
clean_up_image $image "staging" 5 | |
clean_up_image $image "production" 10 | |
fi | |
done | |
} | |
# $0: image link, $1: filter, $2: number of tags want to keep | |
clean_up_image() { | |
local count=0 | |
image=$1 | |
filter=$2 | |
history_limit=$3 | |
for digest in $(gcloud container images list-tags $image --format="get(digest)" --sort-by="~timestamp" --filter="${filter}"); do | |
let count=count+1 | |
img_digest=$image@$digest | |
if [[ $count -gt $history_limit ]]; then | |
gcloud container images delete $img_digest --force-delete-tags --quiet | |
fi | |
done | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you give us some more information about this script? ex. use case of it