-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
76 lines (58 loc) · 1.9 KB
/
deploy.sh
File metadata and controls
76 lines (58 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
69
70
71
72
73
74
75
76
#!/bin/bash
APP_NAME=tinybite-api
BLUE_PORT=8080
GREEN_PORT=8081
NGINX_CONF=/home/ubuntu/tinybite/nginx/default.conf
echo "deploy start"
echo "Pulling latest image..."
docker pull ghcr.io/tinybite-2025/tinybite-server:latest
if ! docker ps --format '{{.Names}}' | grep -q "${APP_NAME}-blue" && \
! docker ps --format '{{.Names}}' | grep -q "${APP_NAME}-green"; then
echo "First deployment detected — starting blue container..."
docker compose -f docker-compose.common.yml -f docker-compose.blue.yml up -d
exit 0
fi
if docker ps --format '{{.Names}}' | grep -q "${APP_NAME}-blue"; then
CURRENT="blue"
NEXT="green"
NEXT_PORT=$GREEN_PORT
else
CURRENT="green"
NEXT="blue"
NEXT_PORT=$BLUE_PORT
fi
echo "Current Container : $CURRENT"
echo "Next Container : $NEXT"
echo "deploy $NEXT Container"
docker compose -f docker-compose.common.yml -f docker-compose.${NEXT}.yml up -d
echo "running health check on port ${NEXT_PORT}"
success=false
for i in {1..20}; do
sleep 3
if curl -fs "http://localhost:${NEXT_PORT}/test/health" | grep -q "UP"; then
echo "Health Check Passed"
success=true
break
fi
echo "Waiting for Service to be UP ... (${i}/20)"
done
# 실행 실패 시 -> 롤백 진행 후 종료
if [ "$success" = false ]; then
echo "Health check failed! Rolling back..."
docker compose -f docker-compose.${NEXT}.yml down
exit 1
fi
# Reload Nginx
echo "if success, switch nginx conf and stop old container"
sudo sed -i "s/${APP_NAME}-${CURRENT}/${APP_NAME}-${NEXT}/" $NGINX_CONF
sudo docker exec nginx nginx -s reload
# Stop old container
echo "==> Stopping old container ${APP_NAME}-${CURRENT}"
docker stop ${APP_NAME}-${CURRENT} || true
docker rm ${APP_NAME}-${CURRENT} || true
echo "Cleaning unused images"
docker image prune -f >/dev/null 2>&1
echo "=============================="
echo "DEPLOYMENT SUCCESSFUL"
echo "Active container: ${NEXT}"
echo "=============================="