Skip to content

Instantly share code, notes, and snippets.

@viet-wego
Last active September 7, 2018 10:36
Show Gist options
  • Save viet-wego/29adebc362403c45090894c4588fa506 to your computer and use it in GitHub Desktop.
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
#!/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
@lukenvn
Copy link

lukenvn commented Sep 7, 2018

Can you give us some more information about this script? ex. use case of it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment