Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. REPOSITORY=[[[[[ecr_registrory_name]]]]]
  4. IMAGE=$REPOSITORY:latest
  5. AWS_REGION=[[[[[your_region]]]]]
  6.  
  7. # docker login
  8. echo -e "\n--------------------- get-login ---------------------\n"
  9. bash -c "`aws ecr get-login --region $AWS_REGION`"
  10.  
  11. # docker build
  12. echo -e "\n--------------------- docker build ---------------------\n"
  13. docker build -t $IMAGE .
  14.  
  15. # AWS_ACCOUNT_IDの取得
  16. echo -e "\n--------------------- get aws account id ---------------------\n"
  17. AWS_ACCOUNT_ID=`aws sts get-caller-identity | jq -r '.Account'`
  18.  
  19. # push先のレポジトリ
  20. REMOTE_REPOSITORY=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE
  21.  
  22. # docker tag. なんかタグ付けてる
  23. echo -e "\n--------------------- docker tag ---------------------\n"
  24. docker tag $IMAGE $REMOTE_REPOSITORY
  25.  
  26. # docker push
  27. echo -e "\n--------------------- docker push ---------------------\n"
  28. docker push $REMOTE_REPOSITORY
  29.  
  30. # find old image
  31. echo -e "\n--------------------- checking for old images ---------------------\n"
  32. OLD_IMAGE_DIGESTS=`aws ecr --region $AWS_REGION list-images --repository-name $REPOSITORY --filter tagStatus=UNTAGGED | jq '.imageIds | map({imageDigest: .imageDigest})'`
  33.  
  34. # deleet old images if they exist
  35. if [ ! "$OLD_IMAGE_DIGESTS" = '[]' ]; then
  36. echo -e "\n--------------------- deleting old images ---------------------\n"
  37. aws ecr --region $AWS_REGION batch-delete-image --repository-name $REPOSITORY --image-ids "$OLD_IMAGE_DIGESTS"
  38. else
  39. echo -e "\nThere are no old images on ECR"
  40. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement