Skip to content

Instantly share code, notes, and snippets.

@mcindea
Created July 13, 2020 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcindea/6f1c8e5bb9ec022cf5f8de8ffabe88f0 to your computer and use it in GitHub Desktop.
Save mcindea/6f1c8e5bb9ec022cf5f8de8ffabe88f0 to your computer and use it in GitHub Desktop.
Checks if a docker image tag exists in AWS ECR, and returns an exit code different than 0 if it doesn't.
#!/usr/bin/env bash
# Checks if a docker image tag exists in AWS ECR, and returns an exit code different than 0 if it doesn't.
REGISTRY_ID="${1%%.*}"
IMAGE_TAG=${1##*:}
REPOSITORY="$(echo ${1%%:*} | cut -d / -f 2,3)"
if [[ -z "$REGISTRY_ID" ]] || [[ -z "$IMAGE_TAG" ]] || [[ -z "$REPOSITORY" ]] ; then
echo "Usage: $( basename $0 ) url:tag"
echo "Example: $( basename $0 NNNNNNNNN.dkr.ecr.eu-west-1.amazonaws.com/repo-something:latest) "
exit 1
fi
IMAGE_META="$( aws ecr describe-images --registry-id $REGISTRY_ID --repository-name=$REPOSITORY --image-ids=imageTag=$IMAGE_TAG 2> /dev/null )"
if [[ $? == 0 ]]; then
if echo "${IMAGE_META}" | jq '.imageDetails[0].imageTags' | grep -q \"$IMAGE_TAG\";then
echo "$1 found"
else
echo "$IMAGE_TAG not found or the JSON response changed"
exit 2
fi
else
echo "$1 not found. Please check if the image for this branch has been built and the CI/CD job finished successfuly."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment