Skip to content

Instantly share code, notes, and snippets.

@d1rtym0nk3y
Created March 30, 2023 11:48
Show Gist options
  • Save d1rtym0nk3y/c1515a4b0f9952206b5acb1a97c8e9cb to your computer and use it in GitHub Desktop.
Save d1rtym0nk3y/c1515a4b0f9952206b5acb1a97c8e9cb to your computer and use it in GitHub Desktop.
Find an image in aws ecr by image tag prefix and add new image tag
#!/usr/bin/env bash
set -e
REPO_TO_SEARCH="$1"
TAG_TO_FIND="$2"
NEW_TAG="$3"
FOUND_TAG=$(
aws ecr list-images \
--repository-name "$REPO_TO_SEARCH" \
--query "imageIds[].imageTag | sort(@) | [?starts_with(@, '$TAG_TO_FIND')] | [-1]" \
--output json \
| xargs -n1 echo # pipe to xargs to remove the quotes around the output
)
if [ "$FOUND_TAG" == "null" ]; then
echo "No tag found for $TAG_TO_FIND"
exit 1
fi
echo "Found tag: $FOUND_TAG"
echo "Getting Manifest for $FOUND_TAG"
MANIFEST=$(
aws ecr batch-get-image \
--repository-name "$REPO_TO_SEARCH" \
--image-ids imageTag="${FOUND_TAG}" \
--output json \
| jq --raw-output --join-output '.images[0].imageManifest'
)
echo "Tagging $FOUND_TAG as $NEW_TAG"
TAG_OUTPUT=$(aws ecr put-image --repository-name "$REPO_TO_SEARCH" --image-tag "$NEW_TAG" --image-manifest "$MANIFEST" --output json)
if [ $? -eq 0 ]; then
echo "Tagged $FOUND_TAG as $NEW_TAG"
else
echo "Failed to tag $FOUND_TAG as $NEW_TAG"
echo "$TAG_OUTPUT"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment