-
-
Save eugenestarchenko/e3d03e05db1b6284957fd5a9ca201f99 to your computer and use it in GitHub Desktop.
shell script for build Dockerfile and push the image to ECR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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