Skip to content

Instantly share code, notes, and snippets.

@vutny
Last active March 31, 2020 09:31
Show Gist options
  • Save vutny/73f05707d6bfa06c319ff982b593a9fe to your computer and use it in GitHub Desktop.
Save vutny/73f05707d6bfa06c319ff982b593a9fe to your computer and use it in GitHub Desktop.
Push local Docker images to Google Cloud Registry
#!/usr/bin/env bash
# Exit immediately on error
set -o errexit
# All variables should be explicitly declared
set -o nounset
# Exit on error in a loop
set -o pipefail
# Find local images tagged as ``example/*:latest`` and push them as ``gcr.io/my-example-project/*:latest``
: "${GCR_URL:=gcr.io}"
: "${GCP_PROJECT:=my-example-project}"
: "${DOCKER_IMAGES_TAG:=latest}"
: "${DOCKER_LOCAL_REPO:=example}"
GCP_GCR_URI="${GCR_URL}/${GCP_PROJECT}"
LOCAL_IMAGES="$(docker images --filter="reference=${DOCKER_LOCAL_REPO}/*:${DOCKER_IMAGES_TAG}" | tail -n +2 | awk '{print $1":"$2;}')"
for repo in $LOCAL_IMAGES; do
tag="${GCP_GCR_URI}/${repo#*/}"
docker tag "$repo" "$tag"
docker push "$tag"
docker rmi "$tag"
image=${tag%:*}
layers="$(gcloud container images list-tags "$image" --filter 'NOT tags:*' --format='get(digest)' | tail -n +2)"
for layer in $layers; do
# Delete old image layers
gcloud container images delete "${image}@${layer}" --quiet
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment