-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathaws-ecr-repository-push.sh
More file actions
executable file
·53 lines (39 loc) · 1.67 KB
/
aws-ecr-repository-push.sh
File metadata and controls
executable file
·53 lines (39 loc) · 1.67 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
#Script used to push the image to ECR repository. Please ensure that the role associated with the instance has access to AWS ECR
echo "Starting the PUSH to AWS ECR...."
if [ $# -eq 0 ]
then
echo "Please provide the image name"
echo "Usage: $0 <image-name>"
exit 1
fi
Dockerimage=$1
# Fetch the AWS account number
aws_account=$(aws sts get-caller-identity --query Account --output text)
if [ $? -ne 0 ]
then
echo "Failed to get AWS account number. Please check your AWS credentials."
exit 255
fi
# Get the region defined in the current configuration (default to us-east-1 if none defined)
aws_region=$(aws configure get region)
aws_region=${aws_region:-us-east-1}
reponame="${aws_account}.dkr.ecr.${aws_region}.amazonaws.com/${Dockerimage}:latest"
# Creates a repo if it does not exist
echo "Create or replace if repo does not exist...."
aws ecr describe-repositories --repository-names "${Dockerimage}" > /dev/null 2>&1
if [ $? -ne 0 ]
then
aws ecr create-repository --repository-name "${Dockerimage}" > /dev/null
fi
# Get the AWS ECR login to pull base image from public ECR
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
# Build the docker image and push to ECR
echo "Building the docker image"
docker build -t ${Dockerimage} .
echo "Tagging the Docker image"
docker tag ${Dockerimage} ${reponame}
# Get the AWS ECR login to push the image to private ECR
aws ecr get-login-password --region "${aws_region}" | docker login --username AWS --password-stdin "${aws_account}".dkr.ecr."${aws_region}".amazonaws.com
echo "Pushing the Docker image to AWS ECR"
docker push ${reponame}