This repository was archived by the owner on Mar 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathredeploy.sh
More file actions
executable file
·90 lines (75 loc) · 2.83 KB
/
Copy pathredeploy.sh
File metadata and controls
executable file
·90 lines (75 loc) · 2.83 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
#!/bin/bash
set -e
# .env 파일에서 환경 변수 로드
if [ -f .env ]; then
echo "📄 Loading configuration from .env file..."
export $(grep -v '^#' .env | grep -v '^$' | xargs)
else
echo "❌ .env file not found! Please run './setup-config.sh' first."
exit 1
fi
# 필수 변수 확인
if [ -z "$EC2_IP" ] || [ -z "$AWS_REGION" ] || [ -z "$ECR_REPOSITORY_URI" ]; then
echo "❌ Missing required configuration. Please run './setup-config.sh' first."
echo " Required: EC2_IP, AWS_REGION, ECR_REPOSITORY_URI"
exit 1
fi
# AWS 계정 ID 추출 (ECR URI에서)
AWS_ACCOUNT_ID=$(echo $ECR_REPOSITORY_URI | cut -d'.' -f1)
PROJECT_NAME="autobiography-api"
echo "🔄 Starting code redeploy for ${PROJECT_NAME}"
echo "📍 Region: ${AWS_REGION}"
echo "🖥️ EC2 IP: ${EC2_IP}"
echo "📦 ECR URI: ${ECR_REPOSITORY_URI}"
echo "🏢 AWS Account: ${AWS_ACCOUNT_ID}"
echo ""
echo "📦 Step 1: Building TypeScript..."
npm run build
echo ""
echo "🐳 Step 2: Building Docker image..."
docker build --platform linux/amd64 -t ${PROJECT_NAME} .
echo ""
echo "📤 Step 3: Pushing to ECR..."
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
docker tag ${PROJECT_NAME}:latest ${ECR_REPOSITORY_URI}:latest
docker push ${ECR_REPOSITORY_URI}:latest
echo ""
echo "🚀 Step 4: Deploying to EC2..."
ssh -i server.pem -o StrictHostKeyChecking=no ec2-user@${EC2_IP} "
# 기존 컨테이너 정리
docker stop autobiography-api 2>/dev/null || true
docker rm autobiography-api 2>/dev/null || true
# 새 이미지 풀
docker pull ${ECR_REPOSITORY_URI}:latest
# 애플리케이션 실행
docker run -d \\
--name autobiography-api \\
--restart unless-stopped \\
--network host \\
--env-file /home/ec2-user/app/.env \\
-v /home/ec2-user/app:/app/data \\
${ECR_REPOSITORY_URI}:latest
echo '✅ Container started successfully'
"
echo ""
echo "⏳ Waiting for service to be ready..."
sleep 15
echo ""
echo "🧪 Testing deployment..."
if curl -s http://${EC2_IP}:3000/health | jq . >/dev/null 2>&1; then
echo "✅ Health check passed!"
curl -s http://${EC2_IP}:3000/health | jq .
else
echo "⚠️ Health check failed - service may still be starting"
echo "💡 Check logs with: ssh -i server.pem ec2-user@${EC2_IP} docker logs autobiography-api"
fi
echo ""
echo "🎉 Redeploy completed successfully!"
echo "🌐 API: http://${EC2_IP}:3000"
echo "📚 Docs: http://${EC2_IP}:3000/api/v2/docs"
echo "🔍 Health: http://${EC2_IP}:3000/health"
echo ""
echo "💡 Useful commands:"
echo " 📊 Check logs: ssh -i server.pem ec2-user@${EC2_IP} docker logs autobiography-api"
echo " 🔄 Restart: ssh -i server.pem ec2-user@${EC2_IP} docker restart autobiography-api"
echo " 🧪 Test API: ./test-api.sh http://${EC2_IP}:3000/api/v2"