-
Notifications
You must be signed in to change notification settings - Fork 6
/
build_and_push_docker.sh
37 lines (28 loc) · 1.13 KB
/
build_and_push_docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# The name and tag of our algorithm
image_name=$1
tag=$2
#set -e # stop if anything fails
# Build the docker image locally with the image name and then push it to ECR
# with the full name.
docker build -t ${image_name} .
# Get the AWS account ID
account=$(aws sts get-caller-identity --query Account --output text)
# Get the region defined in the current configuration (default to us-west-2 if none defined)
region=$(aws configure get region)
region=${region:-us-west-2}
# Define the full image name on Amazon ECR
fullname="${account}.dkr.ecr.${region}.amazonaws.com/${image_name}:${tag}"
# If the repository doesn't exist in ECR, create it.
aws ecr describe-repositories --repository-names "${image_name}" > /dev/null 2>&1
if [ $? -ne 0 ]
then
aws ecr create-repository --repository-name "${image_name}" > /dev/null
fi
# Get the login command from ECR and execute it directly
aws ecr get-login-password --region ${region} \
| docker login \
--username AWS \
--password-stdin ${account}.dkr.ecr.${region}.amazonaws.com
# Tag and push the local image to Amazon ECR
docker tag ${image_name} ${fullname}
docker push ${fullname}