Skip to content

Instantly share code, notes, and snippets.

@d10v
Created March 29, 2023 14:01
Show Gist options
  • Save d10v/31d7c871683abc047d162ca5a5600cca to your computer and use it in GitHub Desktop.
Save d10v/31d7c871683abc047d162ca5a5600cca to your computer and use it in GitHub Desktop.
GCR docker retag image without pulling
#!/usr/bin/env
# Generated via GPT-4
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 source-image destination-image"
exit 1
fi
# Assign the arguments to variables
SOURCE_IMAGE="$1"
DESTINATION_IMAGE="$2"
# Extract the registry, project ID, and image name from the source and destination images
SOURCE_REGISTRY=$(echo "${SOURCE_IMAGE}" | awk -F/ '{print $1}')
SOURCE_PROJECT_ID=$(echo "${SOURCE_IMAGE}" | awk -F/ '{print $2}')
SOURCE_IMAGE_NAME=$(echo "${SOURCE_IMAGE}" | awk -F/ '{print $3}')
DESTINATION_REGISTRY=$(echo "${DESTINATION_IMAGE}" | awk -F/ '{print $1}')
DESTINATION_PROJECT_ID=$(echo "${DESTINATION_IMAGE}" | awk -F/ '{print $2}')
DESTINATION_IMAGE_NAME=$(echo "${DESTINATION_IMAGE}" | awk -F/ '{print $3}')
# Get the GCR authentication token
ACCESS_TOKEN=$(gcloud auth print-access-token)
# Fetch the source image manifest
MANIFEST=$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" -H "Accept: application/vnd.docker.distribution.manifest.v2+json" "https://${SOURCE_REGISTRY}/v2/${SOURCE_PROJECT_ID}/${SOURCE_IMAGE_NAME}/manifests/latest")
# Push the manifest to the destination image
curl -X PUT -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/vnd.docker.distribution.manifest.v2+json" -d "${MANIFEST}" "https://${DESTINATION_REGISTRY}/v2/${DESTINATION_PROJECT_ID}/${DESTINATION_IMAGE_NAME}/manifests/latest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment