Skip to content

Instantly share code, notes, and snippets.

@outofcoffee
Last active March 1, 2024 13:35
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save outofcoffee/8f40732aefacfded14cce8a45f6e5eb1 to your computer and use it in GitHub Desktop.
Save outofcoffee/8f40732aefacfded14cce8a45f6e5eb1 to your computer and use it in GitHub Desktop.
Check if Docker image exists with tag in AWS ECR
#!/usr/bin/env bash
# Example:
# ./find-ecr-image.sh foo/bar mytag
if [[ $# -lt 2 ]]; then
echo "Usage: $( basename $0 ) <repository-name> <image-tag>"
exit 1
fi
IMAGE_META="$( aws ecr describe-images --repository-name=$1 --image-ids=imageTag=$2 2> /dev/null )"
if [[ $? == 0 ]]; then
IMAGE_TAGS="$( echo ${IMAGE_META} | jq '.imageDetails[0].imageTags[0]' -r )"
echo "$1:$2 found"
else
echo "$1:$2 not found"
exit 1
fi
@JSakhamuri
Copy link

The following would push only if the image with the tag does not exist.
aws ecr list-images --repository-name ${REPO_NAME} --query "imageIds[?imageTag=='${GIT_SHA1}'].imageTag" --output text | docker push ${AWSACCOUNTID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${GIT_SHA1}

@sarthak
Copy link

sarthak commented Jun 15, 2022

Thanks for this script!

I've expanded it to include support for public repositories, using the -p or --public flag and incorporated the usage block from @jeevanshu

aws ecr-public commands only work in us-east-1 region, therefore the correct command should be

    IMAGE_META="$( aws ecr-public --region=us-east-1 describe-images --repository-name=$1 --image-ids=imageTag=$2 2> /dev/null )"

@jeremiahlukus
Copy link

jeremiahlukus commented Jan 12, 2023

          IMAGE_META="$( aws ecr batch-get-image --repository-name=$REPOSITORY --image-ids=imageTag=$IMAGE_TAG --query 'images[].imageId.imageTag' --output text )"
          if [[ $IMAGE_META == $IMAGE_TAG ]]; then
            echo "$IMAGE_META found skipping build"
          else
            echo "$REPOSITORY:$IMAGE_TAG not found, building new image"
            docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
            docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
          fi

  This way is working for me, thanks for the help

@devxoul
Copy link

devxoul commented Jan 19, 2023

@jeremiahlukus, thanks this does work for me 👍

@cig0
Copy link

cig0 commented Jan 26, 2023

Thanks, AWS CLI goes out of it's way to make things less obvious -

👆 👆 👆

@anxo-outeiral
Copy link

anxo-outeiral commented Nov 20, 2023

          IMAGE_META="$( aws ecr batch-get-image --repository-name=$REPOSITORY --image-ids=imageTag=$IMAGE_TAG --query 'images[].imageId.imageTag' --output text )"
          if [[ $IMAGE_META == $IMAGE_TAG ]]; then
            echo "$IMAGE_META found skipping build"
          else
            echo "$REPOSITORY:$IMAGE_TAG not found, building new image"
            docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
            docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
          fi
  This way is working for me, thanks for the help

@jeremiahlukus thanks a lot! It works really well!

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