diff --git a/.github/workflows/auto-deployment.yaml b/.github/workflows/auto-deployment.yaml index 4e81815..8f7f5bb 100644 --- a/.github/workflows/auto-deployment.yaml +++ b/.github/workflows/auto-deployment.yaml @@ -46,8 +46,8 @@ jobs: - name: Upload deployment scripts run: | - sudo chmod 755 JWT/deploy/* - scp -i private_key.pem -r JWT/deploy "${username}@${host}:~" + sudo chmod 755 script/deploy/* + scp -i private_key.pem -r script/deploy/* "${username}@${host}:deploy" - name: Kill current running Spring process run: ssh -i private_key.pem "${username}@${host}" "deploy/shutdown.sh; deploy/backup.sh" diff --git a/script/deploy/backup.sh b/script/deploy/backup.sh new file mode 100755 index 0000000..91de5bc --- /dev/null +++ b/script/deploy/backup.sh @@ -0,0 +1,13 @@ +#!/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 created." + exit 0 +fi + +echo "no server.jar file found." +exit 0 diff --git a/script/deploy/check.sh b/script/deploy/check.sh new file mode 100755 index 0000000..daa08a1 --- /dev/null +++ b/script/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/script/deploy/check_and_recover.sh b/script/deploy/check_and_recover.sh new file mode 100755 index 0000000..2b7477c --- /dev/null +++ b/script/deploy/check_and_recover.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# 서버의 상태를 확인하여 실행시 실패했다면 복구한다. + +pid=$(sudo lsof -t -i :8080) + +if [ -z "$pid" ]; then + echo "Error: server is not running." + + echo + echo "========= ERROR LOG ==========" + cat out.log + echo "========== LOG END ===========" + echo + + script_dir=$(dirname "$0") + + if [ -f "server.bak.jar" ]; then + sudo mv server.bak.jar server.jar + echo "backup jar recovered." + + "${script_dir}/startup.sh" + exit 1 + fi + + echo "recovory fail: no backup jar file found" + exit 2 + +fi + +echo "PID '${pid}' is running on port 8080." + diff --git a/script/deploy/recover.sh b/script/deploy/recover.sh new file mode 100755 index 0000000..6b46a88 --- /dev/null +++ b/script/deploy/recover.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# 새로운 jar 배포가 실패했을 때 백업된 기존 jar 로 다시 서버를 시작한다. + +script_dir=$(dirname "$0") + +if [ -f "server.bak.jar" ]; then + sudo mv server.bak.jar server.jar + echo "backup jar recovered." + "${script_dir}/runner.sh" server.jar + exit_code=$? + + if [ $exit_code -ne 0 ]; then + echo "recovory fail: error occured starting backup jar." + exit 1 + fi + + exit 0 +fi + +echo "recovory fail: no backup jar file found" +exit 2 diff --git a/script/deploy/shutdown.sh b/script/deploy/shutdown.sh new file mode 100755 index 0000000..537f760 --- /dev/null +++ b/script/deploy/shutdown.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# 기존 server 프로세스를 끝낸다. + +pid=$(sudo lsof -t -i :8080) +if [ -n "$pid" ]; then + kill $pid + echo "${pid} closed." + exit 0 +fi + +echo "no process running on port 8080." diff --git a/script/deploy/startup.sh b/script/deploy/startup.sh new file mode 100755 index 0000000..b7df3ad --- /dev/null +++ b/script/deploy/startup.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# server.jar 로 새로운 서버 프로세스를 실행한다. + +pid=$(sudo lsof -t -i :8080) + +if [ -n "$pid" ]; then + echo "Error: PID '${pid}' is conneted to port 8080. cannot startup new process." + exit 1 +fi +nohup java -jar server.jar --spring.profiles.active=dev > out.log 2>&1 & disown +echo "server started at port 8080." +echo "stdout & stderr directed to 'out.log'"