-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-ecr.sh
More file actions
executable file
·117 lines (102 loc) · 3.78 KB
/
deploy-ecr.sh
File metadata and controls
executable file
·117 lines (102 loc) · 3.78 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
# deploy-ecr.sh - Build and push to AWS ECR
set -e
# Configuration
REGISTRY="${ECR_REGISTRY:-public.ecr.aws/<registry_alias>/<repository_name>:latest}"
IMAGE_NAME="${IMAGE_NAME:-express-suite-example-brightstack}"
TAG="${1:-express-suite-example-brightstack-latest}"
# Extract region from registry URL
if [[ $REGISTRY == *.dkr.ecr.*.amazonaws.com* ]]; then
REGION=$(echo "$REGISTRY" | sed -n 's/.*\.ecr\.\([^.]*\)\.amazonaws.*/\1/p')
ACCOUNT_ID=$(echo "$REGISTRY" | cut -d'.' -f1)
LOGIN_DOMAIN="${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com"
else
REGION="${AWS_REGION:-us-east-1}"
LOGIN_DOMAIN="public.ecr.aws"
fi
echo "🚀 AWS ECR Deployment"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Repository: $REGISTRY"
echo "Tag: $TAG"
echo "Full Image: $REGISTRY:$TAG"
echo ""
# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
echo "❌ Error: Docker is not running"
exit 1
fi
# Check if AWS CLI is available
if ! command -v aws &> /dev/null; then
echo "❌ Error: AWS CLI is not installed"
exit 1
fi
# Check AWS credentials
echo "🔍 Checking AWS credentials..."
if ! aws sts get-caller-identity > /dev/null 2>&1; then
echo "❌ Error: AWS credentials not configured"
exit 1
fi
# Fix Docker credential helper issue
echo "🔧 Configuring Docker credentials..."
mkdir -p ~/.docker
if [ ! -f ~/.docker/config.json ]; then
echo '{"credsStore":""}' > ~/.docker/config.json
else
# Backup existing config
cp ~/.docker/config.json ~/.docker/config.json.backup
# Set credsStore to empty
jq '.credsStore = ""' ~/.docker/config.json.backup > ~/.docker/config.json || \
echo '{"credsStore":""}' > ~/.docker/config.json
fi
# Login to ECR
echo "🔐 Logging in to ECR..."
echo " Region: $REGION"
echo " Domain: $LOGIN_DOMAIN"
if [[ $REGISTRY == public.ecr.aws* ]]; then
aws ecr-public get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin public.ecr.aws
else
aws ecr get-login-password --region "$REGION" | \
docker login --username AWS --password-stdin "$LOGIN_DOMAIN"
# Check if repository exists, create if it doesn't
REPO_NAME=$(echo "$REGISTRY" | cut -d'/' -f2-)
echo "📦 Checking if repository exists: $REPO_NAME"
if ! aws ecr describe-repositories --repository-names "$REPO_NAME" --region "$REGION" &>/dev/null; then
echo " Creating repository: $REPO_NAME"
aws ecr create-repository \
--repository-name "$REPO_NAME" \
--region "$REGION" \
--image-scanning-configuration scanOnPush=true \
--encryption-configuration encryptionType=AES256 > /dev/null
echo " ✓ Repository created"
else
echo " ✓ Repository exists"
fi
fi
# Build application locally first
echo "🏗️ Building application locally..."
if [ ! -d "dist/express-suite-example-api" ]; then
echo " Running yarn build..."
yarn build
else
echo " ✓ dist folder exists, skipping yarn build"
echo " To force rebuild, run: rm -rf dist && ./deploy-ecr.sh"
fi
# Build Docker image
echo "📦 Building Docker image..."
docker build -t $IMAGE_NAME:$TAG .
# Tag image
echo "🏷️ Tagging image..."
docker tag $IMAGE_NAME:$TAG $REGISTRY:$TAG
# Push to ECR
echo "⬆️ Pushing to ECR..."
docker push $REGISTRY:$TAG
echo ""
echo "✅ Successfully deployed!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Image: $REGISTRY:$TAG"
echo ""
echo "🚀 Next steps:"
echo " 1. Deploy to ECS/App Runner/Elastic Beanstalk"
echo " 2. Set environment variables (see AWS_ECR_DEPLOYMENT.md)"
echo " 3. Configure load balancer and scaling"