From 25ed6ad51e1e9e304a3d205e5301118dedfe0a4c Mon Sep 17 00:00:00 2001 From: wingyou Date: Mon, 25 Mar 2024 23:19:51 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=B0=B0=ED=8F=AC=20=EC=8A=A4?= =?UTF-8?q?=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/auto-deployment.yml | 20 +++++++++++++++----- JWT/deploy/backup.sh | 5 ++++- JWT/deploy/check.sh | 12 ++++++++++++ JWT/deploy/check_and_recover.sh | 15 +++++++++++++++ JWT/deploy/recover.sh | 4 ++-- JWT/deploy/runner.sh | 18 ------------------ JWT/deploy/shutdown.sh | 2 ++ JWT/deploy/startup.sh | 17 ++++++++--------- 8 files changed, 58 insertions(+), 35 deletions(-) create mode 100755 JWT/deploy/check.sh create mode 100755 JWT/deploy/check_and_recover.sh delete mode 100755 JWT/deploy/runner.sh diff --git a/.github/workflows/auto-deployment.yml b/.github/workflows/auto-deployment.yml index 17c3e1c..1e665a3 100644 --- a/.github/workflows/auto-deployment.yml +++ b/.github/workflows/auto-deployment.yml @@ -67,16 +67,26 @@ jobs: scp -i private_key.pem $jarPath "${username}@${host}:~/server.jar" working-directory: JWT - - name: Run new uploaded jar + - name: Startup new uploaded jar uses: fifsky/ssh-action@v0.0.6 with: command: | sudo chmod 755 deploy/* deploy/startup.sh - echo "done" - exit 0 host: ${{ secrets.EC2_HOST }} user: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_PRIVATE_KEY }} - args: - -o ConnectTimeout=60 + + - name: Wait 30 seconds for server to startup + run: sleep 30 + + - name: Check and Recover + uses: fifsky/ssh-action@v0.0.6 + with: + command: | + sudo chmod 755 deploy/* + deploy/check_and_recover.sh + host: ${{ secrets.EC2_HOST }} + user: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_PRIVATE_KEY }} + diff --git a/JWT/deploy/backup.sh b/JWT/deploy/backup.sh index 634aa3c..91de5bc 100755 --- a/JWT/deploy/backup.sh +++ b/JWT/deploy/backup.sh @@ -1,8 +1,11 @@ #!/bin/bash +# 기존 프로세스를 shutdown 후 새 jar를 배포하기 전 기존 jar를 백업하는 용도로 쓰인다. +# server.jar 파일이 있다면 server.bak.jar로 백업하고, 아니면 아무것도 하지 않는다. + if [ -f server.jar ]; then sudo mv server.jar server.bak.jar - echo "server.bak.jar create." + echo "server.bak.jar created." exit 0 fi diff --git a/JWT/deploy/check.sh b/JWT/deploy/check.sh new file mode 100755 index 0000000..daa08a1 --- /dev/null +++ b/JWT/deploy/check.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# 서버가 실행중인지 확인한다. + +pid=$(sudo lsof -t -i :8080) + +if [ -z "$pid" ]; then + echo "Error: server is not running." + exit 1 +fi + +echo "PID '${pid}' is running on port 8080." diff --git a/JWT/deploy/check_and_recover.sh b/JWT/deploy/check_and_recover.sh new file mode 100755 index 0000000..46a069a --- /dev/null +++ b/JWT/deploy/check_and_recover.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# 서버의 상태를 확인하여 실행시 실패했다면 복구한다. + +script_dir=$(dirname "$0") +"${script_dir}/check.sh" +exit_code=$? + +if [ $exit_code -ne 0 ]; then + echo + echo "========= ERROR LOG ==========" + cat out.log + "${script_dir}/recover.sh" + exit 1 +fi diff --git a/JWT/deploy/recover.sh b/JWT/deploy/recover.sh index 723dfb9..6b46a88 100755 --- a/JWT/deploy/recover.sh +++ b/JWT/deploy/recover.sh @@ -1,6 +1,6 @@ #!/bin/bash -# -# use this script when new jar file exit with unexpected error. + +# 새로운 jar 배포가 실패했을 때 백업된 기존 jar 로 다시 서버를 시작한다. script_dir=$(dirname "$0") diff --git a/JWT/deploy/runner.sh b/JWT/deploy/runner.sh deleted file mode 100755 index fa06dc7..0000000 --- a/JWT/deploy/runner.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -nohup java -jar $1 --spring.profiles.active=dev 2>&1 > out.log & - -echo "waiting for server to startup..." -sleep 10 - -# after sleep, check server is still running. -pid=$(sudo lsof -t -i :8080) - -if [ -z "$pid" ]; then - echo "!server startup failed!" - echo "check out.log to see what happend." - exit 1 -fi - -echo "${pid} is successfully running on port 8080." -exit 0 diff --git a/JWT/deploy/shutdown.sh b/JWT/deploy/shutdown.sh index 9b75da3..537f760 100755 --- a/JWT/deploy/shutdown.sh +++ b/JWT/deploy/shutdown.sh @@ -1,5 +1,7 @@ #!/bin/bash +# 기존 server 프로세스를 끝낸다. + pid=$(sudo lsof -t -i :8080) if [ -n "$pid" ]; then kill $pid diff --git a/JWT/deploy/startup.sh b/JWT/deploy/startup.sh index 7d73d4a..fd791de 100755 --- a/JWT/deploy/startup.sh +++ b/JWT/deploy/startup.sh @@ -1,16 +1,15 @@ #!/bin/bash -# -# startup fresh deployed jar file -script_dir=$(dirname "$0") +# server.jar 로 새로운 서버 프로세스를 실행한다. -"${script_dir}/runner.sh" server.jar -exit_code=$? +pid=$(sudo lsof -t -i :8080) -if [ $exit_code -ne 0 ]; then - echo "Error: there's some problems running new jar. recover backup jar." - "${script_dir}/recover.sh" +if [ -n "$pid" ]; then + echo "Error: PID '${pid}' is conneted to port 8080. cannot startup new process." exit 1 fi -exit 0 +nohup java -jar $1 --spring.profiles.active=dev 2>&1 > out.log & +echo "server started at port 8080." +echo "stdout & stderr directed to 'out.log'" +