From de2b5e125f0c22a75ec0b833953ea178aeeab031 Mon Sep 17 00:00:00 2001 From: Peter-Yu-0402 Date: Sat, 27 Apr 2024 23:14:41 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix=20:=20CICD=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=EC=9D=84=20develop=20=EB=B8=8C=EB=9E=9C=EC=B9=98=EC=97=90=20?= =?UTF-8?q?=EB=A7=9E=EA=B2=8C=20=EC=9E=AC=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cicd_production.yml | 22 +++++-------- .idea/compiler.xml | 2 +- appspec.yml | 10 ++---- build.gradle | 2 +- scripts/start.sh | 45 +++++++-------------------- scripts/stop.sh | 24 ++++++-------- 6 files changed, 35 insertions(+), 70 deletions(-) diff --git a/.github/workflows/cicd_production.yml b/.github/workflows/cicd_production.yml index e2932e77..03a09ac6 100644 --- a/.github/workflows/cicd_production.yml +++ b/.github/workflows/cicd_production.yml @@ -2,13 +2,13 @@ name: Deploy to Amazon EC2 on: push: - branches: ["release"] + branches: [ "main" ,"develop"] env: AWS_REGION: ap-northeast-2 S3_BUCKET_NAME: sparta-project-build CODE_DEPLOY_APPLICATION_NAME: codedeploy - CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: deployment-group-dongnaebangnae + CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: codedeploy-deployment-group-production permissions: contents: read @@ -35,7 +35,7 @@ jobs: - name: Make an application.properties run: | mkdir -p ./src/main/resources - echo "${{ secrets.APPLICATION_PROD }}" > ./src/main/resources/application.properties + echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties cat ./src/main/resources/application.properties # (4) Gradle build (Test 제외) @@ -55,15 +55,6 @@ jobs: gradle-version: 8.7 arguments: clean build -x test - # 실제 필요한 파일(Jar, appspect.yml, 배포 스크립트)만 담기 - - name: zip 파일 생성 - run: | - mkdir -p deploy/scripts - cp scripts/*.sh deploy/scripts - cp appspec.yml deploy/ - cp build/libs/*.jar deploy/ - cd deploy && zip -r ./$GITHUB_SHA.zip * - shell: bash # (5) AWS 인증 (IAM 사용자 Access Key, Secret Key 활용) - name: Configure AWS credentials @@ -77,8 +68,11 @@ jobs: # (6) 빌드 결과물을 S3 버킷에 업로드 - name: Upload to AWS S3 run: | - aws s3 cp deploy/$GITHUB_SHA.zip s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip - + aws deploy push \ + --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ + --ignore-hidden-files \ + --s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \ + --source . # (7) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행 - name: Deploy to AWS EC2 from S3 diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 712e4840..f6c2e944 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -17,7 +17,7 @@ - + diff --git a/appspec.yml b/appspec.yml index 6e8117ab..9882a4f0 100644 --- a/appspec.yml +++ b/appspec.yml @@ -3,7 +3,7 @@ os: linux files: - source: / - destination: /home/ubuntu/app/step3/zip/ + destination: /home/ubuntu/app overwrite: yes permissions: @@ -14,14 +14,10 @@ permissions: hooks: AfterInstall: - - location: scripts/stop.sh # 엔진엑스와 연결되어 있지 않은 스프링 부트를 종료합니다. + - location: scripts/stop.sh timeout: 60 runas: ubuntu ApplicationStart: - - location: scripts/start.sh # 엔진엑스와 연결되어 있지 않은 Port로 새 버전의 스프링 부트를 시작합니다. - timeout: 60 - runas: ubuntu - ValidateService: - - location: scripts/health.sh # 새 스프링 부트가 정상적으로 실행됐는지 확인합니다. + - location: scripts/start.sh timeout: 60 runas: ubuntu \ No newline at end of file diff --git a/build.gradle b/build.gradle index 49497f25..11c7a736 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'com.nawabali' -version = '1.0.1-SNAPSHOT-' + new Date().format("yyyy-MM-dd-HHmmss") +version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' diff --git a/scripts/start.sh b/scripts/start.sh index 46766c9f..4785bcab 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,42 +1,21 @@ #!/usr/bin/env bash -ABSPATH=$(readlink -f $0) -ABSDIR=$(dirname $ABSPATH) -source ${ABSDIR}/profile.sh +PROJECT_ROOT="/home/ubuntu/app" +JAR_FILE="$PROJECT_ROOT/build/libs/nawabali-0.0.1-SNAPSHOT.jar" -REPOSITORY=/home/ubuntu/app/step3 -PROJECT_NAME=ZeroDowntimeDeployment -DEPLOY_LOG="$REPOSITORY/deploy.log" +APP_LOG="$PROJECT_ROOT/application.log" +ERROR_LOG="$PROJECT_ROOT/error.log" +DEPLOY_LOG="$PROJECT_ROOT/deploy.log" TIME_NOW=$(date +%c) -echo "> Build 파일 복사" -echo "> cp $REPOSITORY/zip/*.jar $REPOSITORY/" +# build 파일 복사 +echo "$TIME_NOW > $JAR_FILE 파일 복사" >> $DEPLOY_LOG +cp $PROJECT_ROOT/build/libs/*.jar $JAR_FILE -cp $REPOSITORY/zip/*.jar $REPOSITORY/ +# jar 파일 실행 +echo "$TIME_NOW > $JAR_FILE 파일 실행" >> $DEPLOY_LOG +nohup java -jar $JAR_FILE > $APP_LOG 2> $ERROR_LOG & -echo "> 새 어플리케이션 배포" -JAR_NAME=$(ls -tr $REPOSITORY/*.jar | tail -n 1) - -echo "> JAR Name: $JAR_NAME" - -echo "> $JAR_NAME 에 실행권한 추가" - -chmod +x $JAR_NAME - -echo "> $JAR_NAME 실행" - -IDLE_PROFILE=$(find_idle_profile) - -echo "> $JAR_NAME 를 profile=$IDLE_PROFILE 로 실행합니다." - -# 쉬고 있던 프로필로 jar파일을 백그라운드 실행 -nohup java -jar \ - -Dspring.config.location="classpath:/application.properties, /home/ubuntu/app/step3/zip/application-$IDLE_PROFILE.properties" \ - -Dspring.profiles.active=$IDLE_PROFILE \ - $JAR_NAME > $REPOSITORY/application.log 2>&1 & - -# Deploy 로그 -echo "$TIME_NOW > $JAR_NAME 파일 실행" >> $DEPLOY_LOG -CURRENT_PID=$(pgrep -f $JAR_NAME) +CURRENT_PID=$(pgrep -f $JAR_FILE) echo "$TIME_NOW > 실행된 프로세스 아이디 $CURRENT_PID 입니다." >> $DEPLOY_LOG \ No newline at end of file diff --git a/scripts/stop.sh b/scripts/stop.sh index fcf1f1b0..bfce85e3 100644 --- a/scripts/stop.sh +++ b/scripts/stop.sh @@ -1,23 +1,19 @@ #!/usr/bin/env bash -ABSPATH=$(readlink -f $0) -ABSDIR=$(dirname $ABSPATH) -source ${ABSDIR}/profile.sh +PROJECT_ROOT="/home/ubuntu/app" +JAR_FILE="$PROJECT_ROOT/build/libs/nawabali-0.0.1-SNAPSHOT.jar" + +DEPLOY_LOG="$PROJECT_ROOT/deploy.log" -IDLE_PORT=$(find_idle_port) TIME_NOW=$(date +%c) -DEPLOY_LOG="/home/ubuntu/app/step3/deploy.log" -echo "> $IDLE_PORT 에서 구동중인 애플리케이션 pid 확인" -IDLE_PID=$(lsof -ti tcp:${IDLE_PORT}) +# 현재 구동 중인 애플리케이션 pid 확인 +CURRENT_PID=$(pgrep -f $JAR_FILE) -if [ -z ${IDLE_PID} ] -then +# 프로세스가 켜져 있으면 종료 +if [ -z $CURRENT_PID ]; then echo "$TIME_NOW > 현재 실행중인 애플리케이션이 없습니다" >> $DEPLOY_LOG - echo "> 현재 구동중인 애플리케이션이 없으므로 종료하지 않습니다." else - echo "$TIME_NOW > 실행중인 $IDLE_PID 애플리케이션 종료 " >> $DEPLOY_LOG - echo "> kill -15 $IDLE_PID" - kill -15 ${IDLE_PID} - sleep 5 + echo "$TIME_NOW > 실행중인 $CURRENT_PID 애플리케이션 종료 " >> $DEPLOY_LOG + kill -15 $CURRENT_PID fi \ No newline at end of file From 5c4461718ff634ffc54962e873746cb62bd404ed Mon Sep 17 00:00:00 2001 From: Peter-Yu-0402 Date: Sat, 27 Apr 2024 23:17:30 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix=20:=20gitignore=20=EC=9E=AC=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 0f7d3648..0e5465b8 100644 --- a/.gitignore +++ b/.gitignore @@ -57,9 +57,9 @@ src/main/resources/application.properties # CI/CD, 무중단배포 관련 파일 /appspec.yml -.github/workflows/cicd_production.yml -scripts/health.sh -scripts/profile.sh -scripts/start.sh -scripts/stop.sh -scripts/switch.sh \ No newline at end of file +/.github/workflows/cicd_production.yml +/scripts/health.sh +/scripts/profile.sh +/scripts/start.sh +/scripts/stop.sh +/scripts/switch.sh \ No newline at end of file From 83b83cd8a3ff04154af98dfae8971479b1a083fe Mon Sep 17 00:00:00 2001 From: Peter-Yu-0402 Date: Sat, 27 Apr 2024 23:40:45 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix=20:=20=EC=BA=90=EC=8B=9C=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=ED=9B=84=20gitignore=20=EC=9E=AC=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cicd_production.yml | 84 --------------------------- .gitignore | 2 +- appspec.yml | 23 -------- scripts/health.sh | 39 ------------- scripts/profile.sh | 38 ------------ scripts/start.sh | 21 ------- scripts/stop.sh | 19 ------ scripts/switch.sh | 16 ----- 8 files changed, 1 insertion(+), 241 deletions(-) delete mode 100644 .github/workflows/cicd_production.yml delete mode 100644 appspec.yml delete mode 100644 scripts/health.sh delete mode 100644 scripts/profile.sh delete mode 100644 scripts/start.sh delete mode 100644 scripts/stop.sh delete mode 100644 scripts/switch.sh diff --git a/.github/workflows/cicd_production.yml b/.github/workflows/cicd_production.yml deleted file mode 100644 index 03a09ac6..00000000 --- a/.github/workflows/cicd_production.yml +++ /dev/null @@ -1,84 +0,0 @@ -name: Deploy to Amazon EC2 - -on: - push: - branches: [ "main" ,"develop"] - -env: - AWS_REGION: ap-northeast-2 - S3_BUCKET_NAME: sparta-project-build - CODE_DEPLOY_APPLICATION_NAME: codedeploy - CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: codedeploy-deployment-group-production - -permissions: - contents: read - -jobs: - deploy: - name: Deploy - runs-on: ubuntu-latest - environment: production - - steps: - # (1) 기본 체크아웃 - - name: Checkout - uses: actions/checkout@v4 - - # (2) JDK 17 세팅 - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - distribution: 'temurin' - java-version: '17' - - # (3) application.properties 생성 - - name: Make an application.properties - run: | - mkdir -p ./src/main/resources - echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties - cat ./src/main/resources/application.properties - - # (4) Gradle build (Test 제외) - - name: Cache Gradle packages - uses: actions/cache@v4 - with: - path: | - ~/.gradle/caches - ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradle- - - - name: Setup Gradle and Build - uses: gradle/actions/setup-gradle@v3 - with: - gradle-version: 8.7 - arguments: clean build -x test - - - # (5) AWS 인증 (IAM 사용자 Access Key, Secret Key 활용) - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v4.0.2 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ${{ env.AWS_REGION }} - - - # (6) 빌드 결과물을 S3 버킷에 업로드 - - name: Upload to AWS S3 - run: | - aws deploy push \ - --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ - --ignore-hidden-files \ - --s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \ - --source . - - # (7) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행 - - name: Deploy to AWS EC2 from S3 - run: | - aws deploy create-deployment \ - --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ - --deployment-config-name CodeDeployDefault.AllAtOnce \ - --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \ - --s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0e5465b8..f1a97a0d 100644 --- a/.gitignore +++ b/.gitignore @@ -55,7 +55,7 @@ src/main/resources/application.properties /src/main/generated/com/nawabali/nawabali/domain/QUser.java /src/main/generated/com/nawabali/nawabali/domain/QNotification.java -# CI/CD, 무중단배포 관련 파일 +# CI/CD, 무중단배포 관련 파일 // git rm --cached {file name} /appspec.yml /.github/workflows/cicd_production.yml /scripts/health.sh diff --git a/appspec.yml b/appspec.yml deleted file mode 100644 index 9882a4f0..00000000 --- a/appspec.yml +++ /dev/null @@ -1,23 +0,0 @@ -version: 0.0 -os: linux - -files: - - source: / - destination: /home/ubuntu/app - overwrite: yes - -permissions: - - object: / - pattern: "**" - owner: ubuntu - group: ubuntu - -hooks: - AfterInstall: - - location: scripts/stop.sh - timeout: 60 - runas: ubuntu - ApplicationStart: - - location: scripts/start.sh - timeout: 60 - runas: ubuntu \ No newline at end of file diff --git a/scripts/health.sh b/scripts/health.sh deleted file mode 100644 index f74b9ce7..00000000 --- a/scripts/health.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash - -ABSPATH=$(readlink -f $0) -ABSDIR=$(dirname $ABSPATH) -source ${ABSDIR}/profile.sh -source ${ABSDIR}/switch.sh - -IDLE_PORT=$(find_idle_port) - -echo "> Health Check Start!" -echo "> IDLE_PORT: $IDLE_PORT" -echo "> curl -s http://127.0.0.1:$IDLE_PORT/profile " -sleep 10 - -for RETRY_COUNT in {1..10} -do - RESPONSE=$(curl -s http://127.0.0.1:${IDLE_PORT}/profile) - UP_COUNT=$(echo ${RESPONSE} | grep 'real' | wc -l) - - if [ ${UP_COUNT} -ge 1 ] - then # $up_count >= 1 ("real" 문자열이 있는지 검증) - echo "> Health check 성공" - switch_proxy - break - else - echo "> Health check의 응답을 알 수 없거나 혹은 실행 상태가 아닙니다." - echo "> Health check: ${RESPONSE}" - fi - - if [ ${RETRY_COUNT} -eq 10 ] - then - echo "> Health check 실패. " - echo "> 엔진엑스에 연결하지 않고 배포를 종료합니다." - exit 1 - fi - - echo "> Health check 연결 실패. 재시도..." - sleep 10 -done \ No newline at end of file diff --git a/scripts/profile.sh b/scripts/profile.sh deleted file mode 100644 index c8114cb6..00000000 --- a/scripts/profile.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# bash는 return value가 안되니 *제일 마지막줄에 echo로 해서 결과 출력*후, 클라이언트에서 값을 사용한다 - -# 쉬고 있는 profile 찾기: real1이 사용중이면 real2가 쉬고 있고, 반대면 real1이 쉬고 있음 -function find_idle_profile() -{ - RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://prod.dongnaebangnae.com/profile) - - if [ ${RESPONSE_CODE} -ge 400 ] # 400 보다 크면 (즉, 40x/50x 에러 모두 포함) - then - CURRENT_PROFILE=real2 - else - CURRENT_PROFILE=$(curl -s https://prod.dongnaebangnae.com/profile) - fi - - if [ ${CURRENT_PROFILE} == real1 ] - then - IDLE_PROFILE=real2 - else - IDLE_PROFILE=real1 - fi - - echo "${IDLE_PROFILE}" -} - -# 쉬고 있는 profile의 port 찾기 -function find_idle_port() -{ - IDLE_PROFILE=$(find_idle_profile) - - if [ ${IDLE_PROFILE} == real1 ] - then - echo "8081" - else - echo "8082" - fi -} \ No newline at end of file diff --git a/scripts/start.sh b/scripts/start.sh deleted file mode 100644 index 4785bcab..00000000 --- a/scripts/start.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash - -PROJECT_ROOT="/home/ubuntu/app" -JAR_FILE="$PROJECT_ROOT/build/libs/nawabali-0.0.1-SNAPSHOT.jar" - -APP_LOG="$PROJECT_ROOT/application.log" -ERROR_LOG="$PROJECT_ROOT/error.log" -DEPLOY_LOG="$PROJECT_ROOT/deploy.log" - -TIME_NOW=$(date +%c) - -# build 파일 복사 -echo "$TIME_NOW > $JAR_FILE 파일 복사" >> $DEPLOY_LOG -cp $PROJECT_ROOT/build/libs/*.jar $JAR_FILE - -# jar 파일 실행 -echo "$TIME_NOW > $JAR_FILE 파일 실행" >> $DEPLOY_LOG -nohup java -jar $JAR_FILE > $APP_LOG 2> $ERROR_LOG & - -CURRENT_PID=$(pgrep -f $JAR_FILE) -echo "$TIME_NOW > 실행된 프로세스 아이디 $CURRENT_PID 입니다." >> $DEPLOY_LOG \ No newline at end of file diff --git a/scripts/stop.sh b/scripts/stop.sh deleted file mode 100644 index bfce85e3..00000000 --- a/scripts/stop.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash - -PROJECT_ROOT="/home/ubuntu/app" -JAR_FILE="$PROJECT_ROOT/build/libs/nawabali-0.0.1-SNAPSHOT.jar" - -DEPLOY_LOG="$PROJECT_ROOT/deploy.log" - -TIME_NOW=$(date +%c) - -# 현재 구동 중인 애플리케이션 pid 확인 -CURRENT_PID=$(pgrep -f $JAR_FILE) - -# 프로세스가 켜져 있으면 종료 -if [ -z $CURRENT_PID ]; then - echo "$TIME_NOW > 현재 실행중인 애플리케이션이 없습니다" >> $DEPLOY_LOG -else - echo "$TIME_NOW > 실행중인 $CURRENT_PID 애플리케이션 종료 " >> $DEPLOY_LOG - kill -15 $CURRENT_PID -fi \ No newline at end of file diff --git a/scripts/switch.sh b/scripts/switch.sh deleted file mode 100644 index 24e1479f..00000000 --- a/scripts/switch.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash - -ABSPATH=$(readlink -f $0) -ABSDIR=$(dirname $ABSPATH) -source ${ABSDIR}/profile.sh - -function switch_proxy() { - IDLE_PORT=$(find_idle_port) - - echo "> 전환할 Port: $IDLE_PORT" - echo "> Port 전환" - echo "set \$service_url http://13.125.104.185:${IDLE_PORT};" | sudo tee /etc/nginx/conf.d/service-url.inc - - echo "> 엔진엑스 Reload" - sudo service nginx reload -} \ No newline at end of file