Skip to content

Commit

Permalink
refactor: 배포 스크립트 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
alstn113 committed Jan 8, 2025
1 parent 42e6b87 commit b482950
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
2 changes: 0 additions & 2 deletions server/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ x-app: &app
TZ: Asia/Seoul
SPRING_PROFILES_ACTIVE: prod
restart: always
depends_on:
- redis

services:
redis:
Expand Down
36 changes: 23 additions & 13 deletions server/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
#!/bin/bash

NGINX_CONFIG_PATH="/etc/nginx/sites-available/api.fluffy.run"
HOST_HEALTH_CHECK_ENDPOINT="http://localhost:8082/actuator/health"
HEALTH_CHECK_ATTEMPTS=5
HEALTH_CHECK_DELAY=3

health_check() {
local endpoint=$1
for i in $(seq 1 $HEALTH_CHECK_ATTEMPTS); do
echo "Health check attempt ($i/$HEALTH_CHECK_ATTEMPTS)"
response=$(curl -s -o /dev/null -w "%{http_code}" $HOST_HEALTH_CHECK_ENDPOINT)
echo "Health check attempt ($i/$HEALTH_CHECK_ATTEMPTS) for $endpoint"
response=$(curl -s -o /dev/null -w "%{http_code}" "$endpoint")

if [ $response -eq 200 ]; then
echo "Health check passed"
if [ "$response" -eq 200 ]; then
echo "Health check passed for $endpoint"
return 0
fi

sleep $HEALTH_CHECK_DELAY
done

echo "Health check failed"
echo "Health check failed for $endpoint"
return 1
}

switch_container() {
local prev_container=$1
local next_container=$2
local next_container_port=$3

docker compose -f compose.yml up $next_container -d
echo "Starting $next_container..."
docker compose -f compose.yml up "$next_container" -d

if ! health_check; then
echo "Health check failed, rolling back"
docker compose -f compose.yml down $next_container
local health_check_url="http://localhost:${next_container_port}/health"

if ! health_check "$health_check_url"; then
echo "Health check failed, rolling back..."
docker compose -f compose.yml down "$next_container"
return 1
fi

sed -i "s/server $prev_container:8080;/server $next_container:8080;/" "$NGINX_CONFIG_PATH"

sudo nginx -s reload
echo "Reloading Nginx configuration..."
if ! sudo nginx -s reload; then
echo "Failed to reload Nginx"
return 1
fi

echo "$next_container is now live!"
}

IS_GREEN=$(docker container ps | grep app-green)

if [ -z "$IS_GREEN" ]; then
echo "### BLUE >> GREEN ###"
switch_container "app-blue" "app-green"
switch_container "app-blue" "app-green" "8081"
else
echo "### GREEN >> BLUE ###"
switch_container "app-green" "app-blue"
switch_container "app-green" "app-blue" "8080"
fi

0 comments on commit b482950

Please sign in to comment.