diff --git a/WebServiceBySpringBootAndAWS/.travis.yml b/WebServiceBySpringBootAndAWS/.travis.yml index 40578e9..5b97f1ab 100644 --- a/WebServiceBySpringBootAndAWS/.travis.yml +++ b/WebServiceBySpringBootAndAWS/.travis.yml @@ -20,9 +20,13 @@ before_install: script: "./gradlew clean build" before_deploy: - - zip -r springboot2-webservice * - - mkdir -p deploy - - mv springboot2-webservice.zip deploy/springboot2-webservice.zip + - mkdir -p before-deploy # zip에 포함시킬 파일들을 담을 디렉토리 설정 + - cp scripts/*.sh before-deploy/ + - cp appspec.yml before-deploy/ + - cp build/libs/*.jar before-deploy/ + - cd before-deploy && zip -r before-deploy * # before-deploy로 이동 후 전체 압축 + - cd ../ && mkdir -p deploy # 상위 디렉토리로 이동 후 deploy 디렉토리 생성 + - mv before-deploy/before-deploy.zip deploy/sprinbboot2-webservice.zip # deploy로 zip파일 이동 deploy: - provider: s3 @@ -37,9 +41,9 @@ deploy: wait-until-deployed: true - provider: codedeploy - access_key_id: $AWS_ACCESS_KEY # Travis repo settings에 설정된 값 - secret_access_key: $AWS_SECRET_KEY # Travis repo settings에 설정된 값 - bucket: banjjoknim-springboot2-webservice-build # S3 버킷 + access_key_id: $AWS_ACCESS_KEY + secret_access_key: $AWS_SECRET_KEY + bucket: banjjoknim-springboot2-webservice-build key: springboot2-webservice.zip # 빌드 파일을 압축해서 전달 bundle_type: zip # 압축 확장자 application: springboot2-webservice # 웹 콘솔에서 등록한 CodeDeploy 애플리케이션 diff --git a/WebServiceBySpringBootAndAWS/appspec.yml b/WebServiceBySpringBootAndAWS/appspec.yml index 4d91ad9..7f557ab 100644 --- a/WebServiceBySpringBootAndAWS/appspec.yml +++ b/WebServiceBySpringBootAndAWS/appspec.yml @@ -3,4 +3,16 @@ os: linux files: - source: / destination: /home/ec2-user/app/step2/zip/ - overwrite: yes \ No newline at end of file + overwrite: yes + +permissions: + - object: / + pattern: "**" + owner: ec2-user + group: ec2-user + +hooks: + ApplicationStart: + - location: deploy.sh + timeout: 60 + runas: ec2-user \ No newline at end of file diff --git a/WebServiceBySpringBootAndAWS/scripts/deploy.sh b/WebServiceBySpringBootAndAWS/scripts/deploy.sh new file mode 100644 index 0000000..a12668c --- /dev/null +++ b/WebServiceBySpringBootAndAWS/scripts/deploy.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +REPOSITORY=/home/ec2-user/app/step2 +PROJECT_NAME=springboot2-webservice + +echo "> Build 파일 복사" + +cp $REPOSITORY/zip/*.jar $REPOSITORY/ + +echo "> 현재 구동 중인 애플리케이션 pid 확인" + +CURRENT_PID=$(pgrep -fl springboot2-webservice | grep jar | awk '{print $1}') + +echo "현재 구동 중인 애플리케이션 pid : $CURRENT_PID" + +if [ -z "$CURRENT_PID"]; then + echo "> 현재 구동 중인 애플리케이션이 없으므로 종료하지 않습니다." +else + echo "> kill -15 $CURRENT_PID" + kill -15 $CURRENT_PID + sleep 5 +fi + +echo "> 새 애플리케이션 배포" + +JAR_NAME=$(ls -tr $REPOSITORY/*.jar | tall -n 1) + +echo "> JAR_NAME 에 실행권한 추가" + +chmod +x $JAR_NAME + +echo "> $JAR_NAME 실행" + +nohup java -jar \ + -Dspring.config.location=classpath:/application.properties,classpath:/application-real.properties,/home/ec2-user/app/application-oauth.properties,/home/ec2-user/app/application-real-db.properties \ + -Dspring.profiles.active=real \ + $JAR_NAME > $REPOSITORY/nohup.out 2>&1 & \ No newline at end of file