Skip to content

Commit

Permalink
[feat] : CICD 결과를 디스코드 메세지로 전송한다 (#148)
Browse files Browse the repository at this point in the history
* #147 [feat] : CICD 결과를 디스코드 메세지로 전송한다

* #147 [feat] : 도커 & Nginx 설정 파일들의 경로를 명확히 지정한다

* #147 [fix] : 누락된 부분을 추가한다
  • Loading branch information
bbbang105 authored Jan 7, 2025
1 parent 81f4628 commit 0495f53
Showing 1 changed file with 57 additions and 11 deletions.
68 changes: 57 additions & 11 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 작업 디렉토리 설정
cd /home/ubuntu

# 현재 실행중인 App이 green인지 확인합니다.
# 현재 실행중인 App이 green인지 확인합니다.
IS_GREEN=$(sudo docker ps --format '{{.Names}}' | grep -w green)

# nginx 설정 파일 경로
Expand All @@ -14,13 +14,30 @@ DEFAULT_CONF="/etc/nginx/nginx.conf"
# docker-compose.yaml 경로
DOCKER_COMPOSE_FILE="/home/ubuntu/docker-compose.yaml"

# blue가 실행중이라면 green을 up합니다.
# discord webhook 관련 변수
DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1326042657880932434/ARfU0zZr8Gf1BLn1D1-qAr1pPber2FOjhKTn6fZGVxOemHL068tWt8nlQOQDhXkCFL03"
MESSAGE_SUCCESS="🥳 배포가 성공적으로 수행되었습니다!"
MESSAGE_FAILURE="🚨 배포 과정에서 오류가 발생했습니다. 빠른 확인바랍니다."

# 💬 디스코드 메시지 보내기 함수
send_discord_message() {
local message=$1
curl -H "Content-Type: application/json" -d "{\"content\": \"$message\"}" $DISCORD_WEBHOOK_URL
}

# 💚 blue가 실행중이라면 green을 up합니다.
if [ -z "$IS_GREEN" ]; then

echo "### BLUE => GREEN ###"

echo ">>> 1. green container를 up합니다."
sudo docker compose -f $DOCKER_COMPOSE_FILE up -d green
sudo docker compose -f "$DOCKER_COMPOSE_FILE" up -d green || {
send_discord_message "$MESSAGE_FAILURE"
exit 1
}

# Health check 타임아웃: 60초
SECONDS=0
while true; do
echo ">>> 2. green health check 중..."
sleep 3
Expand All @@ -29,22 +46,39 @@ if [ -z "$IS_GREEN" ]; then
echo "⏰ health check success!!!"
break
fi
if [ $SECONDS -ge 60 ]; then
echo "💥 health check failed (timeout)!!!"
send_discord_message "$MESSAGE_FAILURE"
exit 1
fi
done

echo ">>> 3. nginx를 다시 실행합니다."
sudo cp $GREEN_NGINX_CONF $DEFAULT_CONF
sudo nginx -s reload
sudo cp "$GREEN_NGINX_CONF" "$DEFAULT_CONF" && sudo nginx -s reload || {
send_discord_message "$MESSAGE_FAILURE"
exit 1
}

echo ">>> 4. blue container를 down합니다."
sudo docker compose -f $DOCKER_COMPOSE_FILE stop blue
sudo docker compose -f "$DOCKER_COMPOSE_FILE" stop blue || {
send_discord_message "$MESSAGE_FAILURE"
exit 1
}

# green이 실행중이면 blue를 up합니다.
send_discord_message "$MESSAGE_SUCCESS"

# 💙 green이 실행중이면 blue를 up합니다.
else
echo "### GREEN => BLUE ###"

echo ">>> 1. blue container를 up합니다."
sudo docker compose -f $DOCKER_COMPOSE_FILE up -d blue
sudo docker compose -f "$DOCKER_COMPOSE_FILE" up -d blue || {
send_discord_message "$MESSAGE_FAILURE"
exit 1
}

# Health check 타임아웃: 60초
SECONDS=0
while true; do
echo ">>> 2. blue health check 중..."
sleep 3
Expand All @@ -53,12 +87,24 @@ else
echo "⏰ health check success!!!"
break
fi
if [ $SECONDS -ge 60 ]; then
echo "💥 health check failed (timeout)!!!"
send_discord_message "$MESSAGE_FAILURE"
exit 1
fi
done

echo ">>> 3. nginx를 다시 실행합니다."
sudo cp $BLUE_NGINX_CONF $DEFAULT_CONF
sudo nginx -s reload
sudo cp "$BLUE_NGINX_CONF" "$DEFAULT_CONF" && sudo nginx -s reload || {
send_discord_message "$MESSAGE_FAILURE"
exit 1
}

echo ">>> 4. green container를 down합니다."
sudo docker compose -f $DOCKER_COMPOSE_FILE stop green
sudo docker compose -f "$DOCKER_COMPOSE_FILE" stop green || {
send_discord_message "$MESSAGE_FAILURE"
exit 1
}

send_discord_message "$MESSAGE_SUCCESS"
fi

0 comments on commit 0495f53

Please sign in to comment.