pwd -> /home/ubuntu
vi ./.bashrc
export OH="aaaaa"
source ./.bashrc (강제 적용) 또는 서버를 재부팅하면 oh 라는 환경변수가 등록되어서 사용할 수 있음
#!/bin/bash
GITHUB_ID="wisdom08"
PROJECT_NAME="aws-v2"
PROJECT_VERSION="0.0.1"
PROJECT_PID=""
JAR_PATH=""
export GITHUB_ID
export PROJECT_NAME
export PROJECT_VERSION
export PROJECT_PID
export JAR_PATH
source ./var.sh
echo $GITHUB_ID
#!/bin/bash
source ./var.sh
echo $GITHUB_ID
---
chmod u+x deploy.sh
./deploy.sh
재배포
1. 프로젝트 종료
2. github 다운
3. jar 빌드
4. 실행
----> 재배포를 하면 서버가 종료되는데 크론이 작동하면 꼬이게 된다. 즉 재배포시 크론이 1분마다 감시하는 걸 안 해야한다. (크론 정지)
+++++ 5. cron 등록
#!/bin/bash
# 1. env variable
source ./var.sh
echo "1. env variable setup is complete"
# 2. delete cron
touch crontab_delete
crontab crontab_delete
rm crontab_delete
echo "2. deleting cron is complete"
jar {enabled=false}
코드 추가함"var.sh"
GITHUB_ID="wisdom08"
PROJECT_NAME="aws-v2"
PROJECT_VERSION="0.0.1"
PROJECT_PID="$(pgrep -f ${PROJECT_NAME}-${PROJECT_VERSION}.jar)"
JAR_PATH="${HOME}/${PROJECT_NAME}/build/libs/${PROJECT_NAME}-${PROJECT_VERSION}.jar "
export GITHUB_ID
export PROJECT_NAME
export PROJECT_VERSION
export PROJECT_PID
export JAR_PATH
---
source var.sh
echo $JAR_PATH
"deploy.sh"
# 1. env variable
source ./var.sh
echo "1. env variable setup is complete"
# 2. delete cron
touch crontab_delete
crontab crontab_delete
rm crontab_delete
echo "2. deleting cron is complete"
# 3. check server
if [ -n "${PROJECT_PID}" ]; then
# redeploy
kill -9 $PROJECT_PID
echo "3. project kill is complete"
else
#first deploy
#3-1 apt-update
sudo apt-get -y update 1>/dev/null
echo "3-1 apt-get update is complete"
#3-2 jdk install
sudo apt-get -y install openjdk-11-jdk 1>/dev/null
echo "3-2 jdk-install is complete"
#3-3 timezone
sudo timedatectl set-timezone Asia/Seoul
echo "3-3 timezone setting is complete"
fi
#4 delete project folder
rm -rf ${HOME}/${PROJECT_NAME}
echo "4 deleting project folder is complete"
#5 git clone
git clone https://github.com/${GITHUB_ID}/${PROJECT_NAME}.git
sleep 3s
echo "5 git clone is complete"
#6 gradles +x
chmod u+x ${HOME}/${PROJECT_NAME}/gradlew
echo "6 gradlew u+x is completed"
#7 build
cd ${HOME}/${PROJECT_NAME}
./gradlew build
echo "7 gradlew build is complete"
#8 start jar
nohup java -jar -Dspring.profiles.active=prod ${JAR_PATH} 1>${HOME}/log.out 2>${HOME}/err.out &
echo "8 start server complete"
#9 cron registration
touch crontab_new
echo "* * * * * ${HOME}/check-and-restart.sh" 1>>crontab_new
#register the others ... you see >> (append)
crontab crontab_new
rm crontab_new
echo "9 cron registration complete"
rm -rf /home/ubuntu/aws-v2
java -jar -Dspring.profiles.active=prod aws-v2-0.0.1.jar
#!/bin/bash
source ./var.sh
if [ -z "$PROJECT_PID" ]; then
nohup java -jar -Dspring.profiles.active=prod ${JAR_PATH} 1>${HOME}/log.out 2>${HOME}/err.out &
fi
---
chmod u+x check-and-restart.sh
var.sh
check-and-restart.sh
deploy.sh
./gradlew build -x test
tar -cvf deploy.tar check-and-restart.sh deploy.sh var.sh
파일질라 이용해서 deploy.tar 파일 로컬로 옮기는 것까지 완료!
tar -xvf deploy.tar
만약에 gradle.build 파일에서 version이 바뀌거나, settings.gradle 에서 project name이 변경되는 경우?
vi var.sh -> 변수 변경
./deploy.sh
netstat -nltp 8080 서버 도는 거 확인
kill -9 PID 8080 서버 죽이기
------ > cron을 등록해놨기 때문에 1분 내 서버가 자동으로 다시 뜬다.
그런데 서버가 죽으면 관리자한테 메일 등의 방법으로 알려줘야 하지 않을까? 추후 추가해보자
AWS-V2 배포 끝!