Skip to content

Instantly share code, notes, and snippets.

@eugenestarchenko
Forked from kuntao/push_docker_image.sh
Created April 7, 2017 11:38
Show Gist options
  • Select an option

  • Save eugenestarchenko/e3d03e05db1b6284957fd5a9ca201f99 to your computer and use it in GitHub Desktop.

Select an option

Save eugenestarchenko/e3d03e05db1b6284957fd5a9ca201f99 to your computer and use it in GitHub Desktop.
shell script for build Dockerfile and push the image to ECR
#!/bin/bash
REPOSITORY=[[[[[ecr_registrory_name]]]]]
IMAGE=$REPOSITORY:latest
AWS_REGION=[[[[[your_region]]]]]
# docker login
aws ecr get-login --region $AWS_REGION
# docker build
docker build -t $IMAGE .
# push先のレポジトリ
REMOTE_REPOSITORY=`aws ecr describe-repositories | jq -r '.repositories | map(select(.repositoryName == $REPOSITORY))[0] | .repositoryUri'`
# docker tag. なんかタグ付けてる
docker tag $IMAGE $REMOTE_REPOSITORY
# docker push
docker push $REMOTE_REPOSITORY
# find old image
OLD_IMAGE_DIGESTS=`aws ecr --region $AWS_REGION list-images --repository-name $REPOSITORY --filter tagStatus=UNTAGGED | jq '.imageIds | map({imageDigest: .imageDigest})'`
# deleet old images if they exist
if [ ! "$OLD_IMAGE_DIGESTS" = '[]' ]; then
aws ecr --region $AWS_REGION batch-delete-image --repository-name $REPOSITORY --image-ids "$OLD_IMAGE_DIGESTS"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment