Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
set -e

APP_DIR=/home/ec2-user/app
cd "$APP_DIR"
JAR_PATH=$(ls $APP_DIR/*.jar | grep -v plain | head -1)
echo "> JAR 파일: $JAR_PATH"

Expand All @@ -16,28 +17,64 @@ else
fi

echo "> 애플리케이션 시작"
nohup java -jar \
OTEL_AGENT_JAR=$APP_DIR/grafana-opentelemetry-java.jar
OTEL_ENV_FILE=$APP_DIR/otel.env
JAVA_AGENT_OPTS=""
if [ -f "$OTEL_AGENT_JAR" ] && [ -f "$OTEL_ENV_FILE" ]; then
echo "> Grafana OTel agent 감지, 모니터링 활성화"
set -a
source "$OTEL_ENV_FILE"
set +a
JAVA_AGENT_OPTS="-javaagent:$OTEL_AGENT_JAR"
else
echo "> Grafana OTel agent 미설정, 모니터링 없이 실행"
fi

LOG_FILE=/home/ec2-user/app/nohup.out
START_LINE=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)

nohup java $JAVA_AGENT_OPTS -jar \
-Duser.timezone=Asia/Seoul \
$JAR_PATH \
>> /home/ec2-user/app/nohup.out 2>&1 &
>> "$LOG_FILE" 2>&1 &
APP_PID=$!
echo "> 프로세스 시작됨 (PID: $APP_PID)"

echo "> 15초 후 헬스체크 시작"
sleep 15
echo "> 앱 준비 신호 대기 중 (최대 240초, 프로세스 생존 여부로 실패 판단)"
MAX_WAIT=240
ELAPSED=0
READY=false

for i in {1..10}; do
RESPONSE=$(curl -s http://localhost:8080/actuator/health || true)
if echo "$RESPONSE" | grep -q '"status":"UP"'; then
echo "> 헬스체크 성공"
break
fi
echo "> 헬스체크 실패($i/10): $RESPONSE"
if [ $i -eq 10 ]; then
while [ $ELAPSED -lt $MAX_WAIT ]; do
if ! kill -0 $APP_PID 2>/dev/null; then
echo "> 프로세스가 예기치 않게 종료됨 (크래시)"
echo "> 최근 로그:"
tail -n 40 "$LOG_FILE"
echo "> 배포 실패"
exit 1
fi
sleep 10

if tail -n +"$((START_LINE + 1))" "$LOG_FILE" | grep -q "Started ServerApplication"; then
echo "> 앱 준비 완료 신호 감지 (${ELAPSED}초 소요)"
READY=true
break
fi

sleep 2
ELAPSED=$((ELAPSED + 2))
done

if [ "$READY" != "true" ]; then
echo "> ${MAX_WAIT}초 내에 준비 신호 없음 — 안전장치 발동"
echo "> 최근 로그:"
tail -n 40 "$LOG_FILE"
echo "> 배포 실패"
exit 1
fi

RESPONSE=$(curl -s --max-time 10 http://localhost:8080/actuator/health || true)
echo "> 헬스체크 확인: $RESPONSE"

echo "> Nginx 시작"
sudo systemctl start nginx || true
sudo systemctl enable nginx || true
Expand Down
Loading