-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_ecr.sh
More file actions
executable file
·68 lines (55 loc) · 1.9 KB
/
deploy_ecr.sh
File metadata and controls
executable file
·68 lines (55 loc) · 1.9 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# Exit on any error
set -e
# Configuration
AWS_REGION="us-east-1"
ECR_REPO_NAME="rick-morty-api"
IMAGE_TAG="latest"
# Print current step with formatting
print_step() {
echo "→ $1"
}
# Get AWS account ID
print_step "Getting AWS account ID..."
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
if [ $? -ne 0 ]; then
echo "Error: Failed to get AWS account ID. Make sure AWS CLI is configured correctly."
exit 1
fi
# Create ECR repository if it doesn't exist
print_step "Creating ECR repository if it doesn't exist..."
if ! aws ecr describe-repositories --repository-names ${ECR_REPO_NAME} 2>/dev/null; then
print_step "Repository doesn't exist. Creating it..."
aws ecr create-repository --repository-name ${ECR_REPO_NAME}
else
print_step "Repository already exists. Continuing..."
fi
# Get ECR login token and login to Docker
print_step "Logging into ECR..."
aws ecr get-login-password --region ${AWS_REGION} | \
docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
if [ $? -ne 0 ]; then
echo "Error: Failed to log in to ECR"
exit 1
fi
# Build Docker image
print_step "Building Docker image..."
docker build -t ${ECR_REPO_NAME}:${IMAGE_TAG} .
if [ $? -ne 0 ]; then
echo "Error: Docker build failed"
exit 1
fi
# Tag image for ECR
print_step "Tagging image for ECR..."
docker tag ${ECR_REPO_NAME}:${IMAGE_TAG} \
${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPO_NAME}:${IMAGE_TAG}
# Push image to ECR
print_step "Pushing image to ECR..."
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPO_NAME}:${IMAGE_TAG}
if [ $? -eq 0 ]; then
print_step "Successfully pushed image to ECR!"
echo "Repository URI: ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPO_NAME}:${IMAGE_TAG}"
else
echo "Error: Failed to push image to ECR"
exit 1
fi