Skip to content

Instantly share code, notes, and snippets.

@willianantunes
Last active July 27, 2019 22:07
Show Gist options
  • Save willianantunes/2373587afbf94ad538a64384b4a3f9b8 to your computer and use it in GitHub Desktop.
Save willianantunes/2373587afbf94ad538a64384b4a3f9b8 to your computer and use it in GitHub Desktop.
Check if an image exists and built it if not
function docker_tag_exists() {
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
echo $IMAGE_META
IMAGE_TAGS="$( echo ${IMAGE_META} | jq '.imageDetails[0].imageTags[0]' -r )"
echo "$1:$2 found"
return 0
else
echo "$1:$2 not found"
return 1
fi
}
echo "---- First querying ECR to see if the image already exist"
if docker_tag_exists $(ENV_APP_NAME) $(Build.BuildId)-$(Build.SourceVersion); then
echo "---- It exists!"
echo "##vso[task.setvariable variable=create_release;]0"
else
echo "---- Does not exist!"
cd $(Release.PrimaryArtifactSourceAlias)/drop
chmod +x *.sh
$(aws ecr get-login --no-include-email)
docker build -t $(ENV_APP_NAME) .
docker tag $(ENV_APP_NAME):latest $(AWS_MAIN_ECR)/$(ENV_APP_NAME):latest
docker tag $(ENV_APP_NAME):latest $(AWS_MAIN_ECR)/$(ENV_APP_NAME):$(Build.BuildId)-$(Build.SourceVersion)
docker tag $(ENV_APP_NAME):latest $(AWS_MAIN_ECR)/$(ENV_APP_NAME):$(ENV_TAG_VERSION)
docker push $(AWS_MAIN_ECR)/$(ENV_APP_NAME)
echo "##vso[task.setvariable variable=create_release;]1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment