Skip to content

Commit

Permalink
refactor: 배포 스크립트 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
wwingyou committed Mar 25, 2024
1 parent b348609 commit 25ed6ad
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 35 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/auto-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

5 changes: 4 additions & 1 deletion JWT/deploy/backup.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 12 additions & 0 deletions JWT/deploy/check.sh
Original file line number Diff line number Diff line change
@@ -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."
15 changes: 15 additions & 0 deletions JWT/deploy/check_and_recover.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions JWT/deploy/recover.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#
# use this script when new jar file exit with unexpected error.

# 새로운 jar 배포가 실패했을 때 백업된 기존 jar 로 다시 서버를 시작한다.

script_dir=$(dirname "$0")

Expand Down
18 changes: 0 additions & 18 deletions JWT/deploy/runner.sh

This file was deleted.

2 changes: 2 additions & 0 deletions JWT/deploy/shutdown.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

# 기존 server 프로세스를 끝낸다.

pid=$(sudo lsof -t -i :8080)
if [ -n "$pid" ]; then
kill $pid
Expand Down
17 changes: 8 additions & 9 deletions JWT/deploy/startup.sh
Original file line number Diff line number Diff line change
@@ -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'"

0 comments on commit 25ed6ad

Please sign in to comment.