-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathbuild.sh
59 lines (42 loc) · 1.75 KB
/
build.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Config file path
CONFIG_FILE=config.txt
# If config file exists, read values from it
if [ -f "$CONFIG_FILE" ]; then
echo "Reading config file..."
# Read values
source $CONFIG_FILE
# Set variables from config
IMAGE_NAME=$image_name
REPO_NAME=$repo_name
AWS_ACCOUNT=$aws_account
AWS_REGION=$aws_region
else
# Prompt for inputs
read -p "Enter image name: " IMAGE_NAME
read -p "Enter repository name: " REPO_NAME
read -p "Enter AWS account ID: " AWS_ACCOUNT
read -p "Enter AWS Region: " AWS_REGION
# Save inputs to config file
echo "Saving config..."
echo "image_name=$IMAGE_NAME" > $CONFIG_FILE
echo "repo_name=$REPO_NAME" >> $CONFIG_FILE
echo "aws_account=$AWS_ACCOUNT" >> $CONFIG_FILE
echo "aws_region=$AWS_REGION" >> $CONFIG_FILE
fi
# Build Docker image
docker build -t $IMAGE_NAME .
# Check if repository exists
REPO_EXISTS=$(aws ecr describe-repositories --repository-names $REPO_NAME --region $AWS_REGION 2>&1 | grep -c RepositoryNotFoundException)
# Create ECR repository if it doesn't exist
if [ $REPO_EXISTS -eq 1 ]; then
aws ecr create-repository --repository-name $REPO_NAME --region $AWS_REGION
fi
# Tag image
docker tag $IMAGE_NAME $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO_NAME:latest
# Login to ECR
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com
# Push image to ECR
docker push $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO_NAME:latest
# Print image ID
IMAGE_ID=$(aws ecr describe-images --repository-name $REPO_NAME --image-ids imageTag=latest --region $AWS_REGION --query 'imageDetails[].imageId' --output text)
echo "Pushed image URI: $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO_NAME:latest"