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 96f192c commit d889fd4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions server/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ services:
container_name: app-blue
ports:
- '8080:8080'
- '8082:8082'
app-green:
<<: *app
container_name: app-green
ports:
- '8081:8080'
- '8083:8082'
26 changes: 14 additions & 12 deletions server/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,60 @@
NGINX_CONFIG_PATH="/etc/nginx/sites-available/api.fluffy.run"
HEALTH_CHECK_ATTEMPTS=5
HEALTH_CHECK_DELAY=3
BLUE_HEALTH_CHECK_URL="http://localhost:8082/actuator/health"
GREEN_HEALTH_CHECK_URL="http://localhost:8083/actuator/health"

health_check() {
local endpoint=$1
local target_url=$1

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

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

sleep $HEALTH_CHECK_DELAY
done

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

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

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

local health_check_url="http://app-blue:8082/actuator/health"

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

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

echo "Reloading Nginx configuration..."
if ! sudo nginx -s reload; then
echo "Failed to reload Nginx"
echo "Failed to reload Nginx: $(sudo nginx -t 2>&1)"
return 1
fi

echo "$next_container is now live!"
echo "$next_container is now live and accepting requests!"
}

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

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

0 comments on commit d889fd4

Please sign in to comment.