I've noticed with gcr.io
that when I push a docker image with a specific tag:
gcr.io/myproject/myimage:mytag
If that image/tag combo already exists, it seems to untag the original image, upload the new one, and apply the tag to the new one.
This is leading to my repo becoming bloating with lots of untagged versions of the same image.
How do I test if the image/tag combo already exist in gcr.io
, so that I only push when it's required?
Docker tags are just an alias for an image ID. The tag's name must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. In addition, the tag names must not begin with a period or a dash, and they can only contain 128 characters.
Here's how I solved this in a shell script
existing_tags=$(gcloud container images list-tags --filter="tags:mytag" --format=json gcr.io/myproject/myimage)
if [[ "$existing_tags" == "[]" ]]; then
printf "tag does not exist"
else
printf "tag exists"
fi
Explanation
I'm using gcloud container images list-tags
(documentation here)
And filtering for tags matching mytag
using the --filter
flag
And formatting as JSON using --format=json
So essentially, if the tag mytag
doesn't exist, the output of this will be an empty array []
, otherwise, it does exist. You can test this really simply just by string comparison in your script, and then proceed accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With