From b7712da21fe387f7eb7ae12138774095f7cebb73 Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 09:43:51 +0900 Subject: [PATCH 01/10] =?UTF-8?q?[CHORE]=20gitignore=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 012ccb18..1646b5b4 100644 --- a/.gitignore +++ b/.gitignore @@ -177,7 +177,7 @@ replay_pid* # End of https://www.toptal.com/developers/gitignore/api/intellij,java -*.yml +src/**/*.yml *.sql *.html src/main/generated From 9308d8a3501e21bdd5165a3a9b4a1fc4ab38d75b Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 09:44:30 +0900 Subject: [PATCH 02/10] =?UTF-8?q?[ADD]=20github=20action=20dev=20&=20prod?= =?UTF-8?q?=20=EC=9A=A9=20=EC=B6=94=EA=B0=80[ADD]=20github=20action=20dev?= =?UTF-8?q?=20&=20prod=20=EC=9A=A9=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-dev.yml | 124 ++++++++++++++++++++++++++++++ .github/workflows/deploy-prod.yml | 124 ++++++++++++++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 .github/workflows/deploy-dev.yml create mode 100644 .github/workflows/deploy-prod.yml diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml new file mode 100644 index 00000000..6ca02dc3 --- /dev/null +++ b/.github/workflows/deploy-dev.yml @@ -0,0 +1,124 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: BackEnd - CI/CD(deploy) + +on: + pull_request: + branches: [ "develop" ] + types: [ closed ] + + +permissions: + contents: read + +jobs: + deploy-dev: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + environment: develop + env: + DEPLOYMENT_GROUP_NAME: meeteam-dev + S3_BUCKET_DIR_NAME: dev + steps: + - name: ✅ Checkout branch + uses: actions/checkout@v3 + # JDK를 17 버전으로 셋팅한다. + - name: ⚙️ Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # Gradle을 캐싱한다 -> 빌드 속도가 증가하는 효과가 있다. + - name: ✅ Gradle 캐싱 + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + # 프로젝트 저장소에 업로드하면 안되는 설정 파일들을 만들어줍니다. + - name: 🗂️ Make config + run: | + # src/main/resources 경로 이동 + cd ./src/main/resources + + # yml 파일 생성 + touch ./application-dev.yml + echo "$APPLICATION_DEV" > ./application-dev.yml + + # 폴더 생성 + mkdir templates + + # 메일 관련된 html 파일 생성 + cd ./templates + touch ./ApproveMail.html + echo "$MAIL_APPROVE_TEMPLATE" > ./ApproveMail.html + + touch ./UniversityAuthMail.html + echo "$MAIL_VERIFY_TEMPLATE" > ./UniversityAuthMail.html + + touch ./ApplicationNotificationMail.html + echo "$MAIL_APPLICATION_NOTIFICATION_TEMPLATE" > ./ApplicationNotificationMail.html + + env: + APPLICATION_DEV: ${{ secrets.APPLICATION_DEV }} + MAIL_APPROVE_TEMPLATE: ${{ secrets.MAIL_APPROVE_TEMPLATE }} + MAIL_VERIFY_TEMPLATE: ${{ secrets.MAIL_VERIFY_TEMPLATE }} + MAIL_APPLICATION_NOTIFICATION_TEMPLATE: ${{ secrets.MAIL_APPLICATION_NOTIFICATION_TEMPLATE }} + shell: bash + + - name: ⚙️ Gradle 권한 부여 + run: chmod +x gradlew + + - name: ⚙️ Gradle로 빌드 실행 + run: ./gradlew bootjar + # 배포에 필요한 여러 설정 파일과 프로젝트 빌드파일을 zip 파일로 모아준다. + - name: 📦 zip file 생성 + run: | + mkdir deploy + cp ./docker/dev/docker-compose.dev.yml ./deploy/ + cp ./docker/dev/Dockerfile ./deploy/ + cp ./appspec.yml ./deploy/ + cp ./scripts/deploy.sh ./deploy/ + cp ./build/libs/*.jar ./deploy/ + zip -r -qq -j ./spring-app.zip ./deploy + + + # AWS에 연결해준다. + - name: 🌎 AWS 연결 + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + # S3에 프로젝트를 업로드 한다. + - name: 🚛 S3에 프로젝트 업로드 + run: | + aws s3 cp \ + --region ap-northeast-2 \ + ./spring-app.zip s3://${{ secrets.S3_BUCKET_NAME }}/${{ env.S3_BUCKET_DIR_NAME }}/spring-app.zip + + # CodeDelploy에 배포를 요청한다. + - name: 🚀 Code Deploy 배포 요청 + run: aws deploy create-deployment --application-name meeteam-app + --deployment-config-name CodeDeployDefault.OneAtATime + --deployment-group-name ${{ env.DEPLOYMENT_GROUP_NAME }} + --s3-location bucket=${{ secrets.S3_BUCKET_NAME }},bundleType=zip,key=${{ env.S3_BUCKET_DIR_NAME }}/spring-app.zip + + - name: Discord 알림 봇 + uses: sarisia/actions-status-discord@v1 + if: ${{ failure() }} + with: + title: ❗️ Backend CD failed ❗️ + webhook: ${{ secrets.DISCORD_WEBHOOK }} + color: FF0000 \ No newline at end of file diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml new file mode 100644 index 00000000..88a7a323 --- /dev/null +++ b/.github/workflows/deploy-prod.yml @@ -0,0 +1,124 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: BackEnd - CI/CD(deploy) + +on: + pull_request: + branches: [ "main" ] + types: [ closed ] + + +permissions: + contents: read + +jobs: + deploy-prod: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + environment: production + env: + DEPLOYMENT_GROUP_NAME: meeteam-app + S3_BUCKET_DIR_NAME: prod + steps: + - name: ✅ Checkout branch + uses: actions/checkout@v3 + # JDK를 17 버전으로 셋팅한다. + - name: ⚙️ Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # Gradle을 캐싱한다 -> 빌드 속도가 증가하는 효과가 있다. + - name: ✅ Gradle 캐싱 + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + # 프로젝트 저장소에 업로드하면 안되는 설정 파일들을 만들어줍니다. + - name: 🗂️ Make config + run: | + # src/main/resources 경로 이동 + cd ./src/main/resources + + # yml 파일 생성 + touch ./application-prod.yml + echo "$APPLICATION_PROD" > ./application-prod.yml + + # 폴더 생성 + mkdir templates + + # 메일 관련된 html 파일 생성 + cd ./templates + touch ./ApproveMail.html + echo "$MAIL_APPROVE_TEMPLATE" > ./ApproveMail.html + + touch ./UniversityAuthMail.html + echo "$MAIL_VERIFY_TEMPLATE" > ./UniversityAuthMail.html + + touch ./ApplicationNotificationMail.html + echo "$MAIL_APPLICATION_NOTIFICATION_TEMPLATE" > ./ApplicationNotificationMail.html + + env: + APPLICATION_PROD: ${{ secrets.APPLICATION_PROD }} + MAIL_APPROVE_TEMPLATE: ${{ secrets.MAIL_APPROVE_TEMPLATE }} + MAIL_VERIFY_TEMPLATE: ${{ secrets.MAIL_VERIFY_TEMPLATE }} + MAIL_APPLICATION_NOTIFICATION_TEMPLATE: ${{ secrets.MAIL_APPLICATION_NOTIFICATION_TEMPLATE }} + shell: bash + + - name: ⚙️ Gradle 권한 부여 + run: chmod +x gradlew + + - name: ⚙️ Gradle로 빌드 실행 + run: ./gradlew bootjar + # 배포에 필요한 여러 설정 파일과 프로젝트 빌드파일을 zip 파일로 모아준다. + - name: 📦 zip file 생성 + run: | + mkdir deploy + cp ./docker/prod/docker-compose.prod.yml ./deploy/ + cp ./docker/prod/Dockerfile ./deploy/ + cp ./appspec.yml ./deploy/ + cp ./scripts/deploy.sh ./deploy/ + cp ./build/libs/*.jar ./deploy/ + zip -r -qq -j ./spring-app.zip ./deploy + + + # AWS에 연결해준다. + - name: 🌎 AWS 연결 + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + # S3에 프로젝트를 업로드 한다. + - name: 🚛 S3에 프로젝트 업로드 + run: | + aws s3 cp \ + --region ap-northeast-2 \ + ./spring-app.zip s3://${{ secrets.S3_BUCKET_NAME }}/${{ env.S3_BUCKET_DIR_NAME }}/spring-app.zip + + # CodeDelploy에 배포를 요청한다. + - name: 🚀 Code Deploy 배포 요청 + run: aws deploy create-deployment --application-name meeteam-app + --deployment-config-name CodeDeployDefault.OneAtATime + --deployment-group-name ${{ env.DEPLOYMENT_GROUP_NAME }} + --s3-location bucket=${{ secrets.S3_BUCKET_NAME }},bundleType=zip,key=${{ env.S3_BUCKET_DIR_NAME }}/spring-app.zip + + - name: Discord 알림 봇 + uses: sarisia/actions-status-discord@v1 + if: ${{ failure() }} + with: + title: ❗️ Backend CD failed ❗️ + webhook: ${{ secrets.DISCORD_WEBHOOK }} + color: FF0000 \ No newline at end of file From 9a52298cfcbad73b6610f6fa2d3ca8a5d0a3ba94 Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 09:44:39 +0900 Subject: [PATCH 03/10] =?UTF-8?q?[DEL]=20=EA=B8=B0=EC=A1=B4=20deploy=20git?= =?UTF-8?q?hub=20action=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 122 ----------------------------------- 1 file changed, 122 deletions(-) delete mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index 2959257a..00000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,122 +0,0 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. -# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle - -name: BackEnd - CI/CD(deploy) - -on: - pull_request: - types: [ closed ] - branches: [ "develop" ] - -permissions: - contents: read - -jobs: - deploy: - if: github.event.pull_request.merged == true - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - # JDK를 17 버전으로 셋팅한다. - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - - # Gradle을 캐싱한다 -> 빌드 속도가 증가하는 효과가 있다. - - name: Gradle 캐싱 - uses: actions/cache@v3 - with: - path: | - ~/.gradle/caches - ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradle- - - # 프로젝트 저장소에 업로드하면 안되는 설정 파일들을 만들어줍니다. - - name: Make application.yml & KEYS & ApproveMail.html - run: | - # src/main/resources 경로 이동 - cd ./src/main/resources - - # yml 파일 생성 - touch ./application-dev.yml - echo "$APPLICATION_DEV" > ./application-dev.yml - - # 폴더 생성 - mkdir templates - - # 메일 관련된 html 파일 생성 - cd ./templates - touch ./ApproveMail.html - echo "$MAIL_APPROVE_TEMPLATE" > ./ApproveMail.html - - touch ./UniversityAuthMail.html - echo "$MAIL_VERIFY_TEMPLATE" > ./UniversityAuthMail.html - - touch ./ApplicationNotificationMail.html - echo "$MAIL_APPLICATION_NOTIFICATION_TEMPLATE" > ./ApplicationNotificationMail.html - - env: - APPLICATION_DEV: ${{ secrets.APPLICATION_DEV }} - MAIL_APPROVE_TEMPLATE: ${{ secrets.MAIL_APPROVE_TEMPLATE }} - MAIL_VERIFY_TEMPLATE: ${{ secrets.MAIL_VERIFY_TEMPLATE }} - MAIL_APPLICATION_NOTIFICATION_TEMPLATE: ${{ secrets.MAIL_APPLICATION_NOTIFICATION_TEMPLATE }} - shell: bash - - - name: Gradle 권한 부여 - run: chmod +x gradlew - - - name: Gradle로 빌드 실행 - run: ./gradlew bootjar - - - # 배포에 필요한 여러 설정 파일과 프로젝트 빌드파일을 zip 파일로 모아줍니다. - - name: zip file 생성 - run: | - mkdir deploy - cp ./docker/docker-compose.blue.yml ./deploy/ - cp ./docker/docker-compose.green.yml ./deploy/ - cp ./appspec.yml ./deploy/ - cp ./docker/Dockerfile ./deploy/ - cp ./scripts/*.sh ./deploy/ - cp ./build/libs/*.jar ./deploy/ - zip -r -qq -j ./spring-build.zip ./deploy - - - # AWS에 연결해줍니다. - - name: AWS 연결 - uses: aws-actions/configure-aws-credentials@v1 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ${{ secrets.AWS_REGION }} - - # S3에 프로젝트를 업로드 합니다. - - name: S3에 프로젝트 업로드 - run: | - aws s3 cp \ - --region ap-northeast-2 \ - ./spring-build.zip s3://meeteam-backend-bucket - - # CodeDelploy에 배포를 요청합니다. - - name: Code Deploy 배포 요청 - run: aws deploy create-deployment --application-name meeteam-app - --deployment-config-name CodeDeployDefault.OneAtATime - --deployment-group-name meeteam-app - --s3-location bucket=meeteam-backend-bucket,bundleType=zip,key=spring-build.zip - - - name: Discord 알림 봇 - uses: sarisia/actions-status-discord@v1 - if: ${{ failure() }} - with: - title: ❗️ Backend CD failed ❗️ - webhook: ${{ secrets.DISCORD_WEBHOOK }} - color: FF0000 \ No newline at end of file From 0e6c4634b0570ba87f5c24c45f15103021d757ee Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 09:44:52 +0900 Subject: [PATCH 04/10] =?UTF-8?q?[CHORE]=20build=20github=20action=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89=20=ED=8F=AC=EC=9D=B8=ED=8A=B8=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cb614bf7..98d0796a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,18 +9,16 @@ name: BackEnd - CI/CD(build) on: pull_request: - branches: [ "main","develop" ] + branches: [ "develop", "release/v**", "main" ] push: - branches: [ "develop" ] + branches: [ "develop", "release/v**" ] permissions: contents: read jobs: build: - runs-on: ubuntu-latest - steps: - uses: actions/checkout@v3 # JDK를 17 버전으로 셋팅한다. From 050829f0005ba7cce231fa17353e1ea96845cd9a Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 09:45:17 +0900 Subject: [PATCH 05/10] =?UTF-8?q?[ADD]=20=EA=B0=9C=EB=B0=9C/=EC=9A=B4?= =?UTF-8?q?=EC=9A=A9=EC=9A=A9=20=EB=8F=84=EC=BB=A4=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EB=B0=8F=20=EB=8F=84=EC=BB=A4=20=EC=BB=B4=ED=8F=AC=EC=A6=88=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/{ => dev}/Dockerfile | 0 docker/dev/docker-compose.dev.yml | 26 +++++++++++++++++ docker/docker-compose.blue.yml | 18 ------------ docker/docker-compose.green.yml | 14 --------- docker/prod/Dockerfile | 10 +++++++ docker/prod/docker-compose.prod.yml | 44 +++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 32 deletions(-) rename docker/{ => dev}/Dockerfile (100%) create mode 100644 docker/dev/docker-compose.dev.yml delete mode 100644 docker/docker-compose.blue.yml delete mode 100644 docker/docker-compose.green.yml create mode 100644 docker/prod/Dockerfile create mode 100644 docker/prod/docker-compose.prod.yml diff --git a/docker/Dockerfile b/docker/dev/Dockerfile similarity index 100% rename from docker/Dockerfile rename to docker/dev/Dockerfile diff --git a/docker/dev/docker-compose.dev.yml b/docker/dev/docker-compose.dev.yml new file mode 100644 index 00000000..123e797e --- /dev/null +++ b/docker/dev/docker-compose.dev.yml @@ -0,0 +1,26 @@ +version: '3.8' + +networks: + development_network: + driver: bridge + +services: + redis: + image: "redis:alpine" + container_name: redis-dev + networks: + - development_network + expose: + - "6379" + + spring-app: + build: + context: . + dockerfile: Dockerfile + container_name: dev-spring-app + environment: + - SPRING_PROFILES_ACTIVE=dev + networks: + - development_network + ports: + - "1821:8080" diff --git a/docker/docker-compose.blue.yml b/docker/docker-compose.blue.yml deleted file mode 100644 index 5b2624a2..00000000 --- a/docker/docker-compose.blue.yml +++ /dev/null @@ -1,18 +0,0 @@ -#blue -version: '3' -services: - # 서비스의 이름 - backend: - # 현재 디렉토리에서의 Dockerfile을 사용하여 Docker 이미지를 빌드 - build: . - # 호스트의 8081 포트와 컨테이너의 80 포트를 매핑 - environment: - TZ: "Asia/Seoul" - ports: - - "8081:8080" - # 컨테이너의 이름 - container_name: spring-blue - extra_hosts: - - "host.docker.internal:host-gateway" - volumes: - - /home/ubuntu/app/log:/log \ No newline at end of file diff --git a/docker/docker-compose.green.yml b/docker/docker-compose.green.yml deleted file mode 100644 index 2bfc80ad..00000000 --- a/docker/docker-compose.green.yml +++ /dev/null @@ -1,14 +0,0 @@ -#green -version: '3' -services: - backend: - build: . - ports: - - "8082:8080" - container_name: spring-green - environment: - TZ: "Asia/Seoul" - extra_hosts: - - "host.docker.internal:host-gateway" - volumes: - - /home/ubuntu/app/log:/log \ No newline at end of file diff --git a/docker/prod/Dockerfile b/docker/prod/Dockerfile new file mode 100644 index 00000000..aa8df613 --- /dev/null +++ b/docker/prod/Dockerfile @@ -0,0 +1,10 @@ +### Docker 이미지를 생성할 때 기반이 되는 베이스 이미지를 설정한다. +FROM openjdk:17 +### Dockerfile 내에서 사용할 변수 JAR_FILE을 정의한다. +ARG JAR_FILE=*.jar +### JAR_FILE 경로에 해당하는 파일을 Docker 이미지 내부로 복사한다. +COPY ${JAR_FILE} meeteam.jar +### CloudFront Private Key 복사 +COPY private_key.pem /app/private_key.pem +### Docker 컨테이너가 시작될 때 실행할 명령을 지정한다. +ENTRYPOINT ["java","-jar","-Dspring.profiles.active=prod","-Duser.timezone=Asia/Seoul","meeteam.jar"] diff --git a/docker/prod/docker-compose.prod.yml b/docker/prod/docker-compose.prod.yml new file mode 100644 index 00000000..7efe4d6c --- /dev/null +++ b/docker/prod/docker-compose.prod.yml @@ -0,0 +1,44 @@ +version: '3.8' + +networks: + production_network: + name: production_network + driver: bridge + +services: + redis: + image: "redis:alpine" + container_name: redis + restart: always + networks: + - production_network + expose: + - "6379" + volumes: + - redis_data:/data + + spring-app-blue: + build: . + container_name: prod-blue + networks: + - production_network + ports: + - "8081:8080" + volumes: + - /home/ubuntu/app/log:/log + depends_on: + - redis + + spring-app-green: + build: . + container_name: prod-green + networks: + - production_network + ports: + - "8082:8080" + volumes: + - /home/ubuntu/app/log:/log + depends_on: + - redis +volumes: + redis_data: \ No newline at end of file From 968bbd2cb86ed30449a347bd5e84eb3229afad6d Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 09:46:35 +0900 Subject: [PATCH 06/10] =?UTF-8?q?[CHORE]=20code=20deploy=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=20=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/deploy.sh | 149 +++++++++++++++++++++++++++++++--------------- 1 file changed, 100 insertions(+), 49 deletions(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 2dc5e6d1..c2d987f5 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,74 +1,125 @@ #!/bin/bash -# 작업 디렉토리를 /home/ubuntu/app으로 변경 -cd /home/ubuntu/app - -# 환경변수 DOCKER_APP_NAME을 spring으로 설정 -DOCKER_APP_NAME=spring +DEPLOYMENT_ID=$DEPLOYMENT_ID +# 배포 ID가 제공되지 않으면 오류를 출력하고 종료 +if [ -z "$DEPLOYMENT_ID" ]; then + echo "Deployment ID is not set. Please ensure the deployment ID is provided." + exit 1 +fi +# AWS CLI를 사용하여 배포 세부 정보 가져오기 +DEPLOYMENT_DETAILS=$(aws deploy get-deployment --deployment-id $DEPLOYMENT_ID) -# 실행중인 blue가 있는지 확인 -# 프로젝트의 실행 중인 컨테이너를 확인하고, 해당 컨테이너가 실행 중인지 여부를 EXIST_BLUE 변수에 저장 -EXIST_BLUE=$(sudo docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml ps | grep Up) +# 배포 그룹 이름 추출 +DEPLOYMENT_GROUP_NAME=$(echo $DEPLOYMENT_DETAILS | jq -r '.deploymentInfo.deploymentGroupName') -# 배포 시작한 날짜와 시간을 기록 -echo "배포 시작일자 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log +# 만약 배포가 prod 라면 +if [ "$DEPLOYMENT_GROUP_NAME" = "spring-app" ]; then + # 작업 디렉토리를 /home/ubuntu/app/prod으로 변경 + cd /home/ubuntu/app/prod -# green이 실행중이면 blue up -# EXIST_BLUE 변수가 비어있는지 확인 -if [ -z "$EXIST_BLUE" ]; then + # 환경변수 DOCKER_APP_NAME을 spring-app으로 설정 + DOCKER_APP_NAME=spring-app - # 로그 파일(/home/ubuntu/deploy.log)에 "blue up - blue 배포 : port:8081"이라는 내용을 추가 - echo "blue 배포 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + # 실행중인 blue가 있는지 확인 + # 프로젝트의 실행 중인 컨테이너를 확인하고, 해당 컨테이너가 실행 중인지 여부를 EXIST_BLUE 변수에 저장 + EXIST_REDIS=$(sudo docker ps --filter "name=redis" --filter "status=running") + EXIST_BLUE=$(sudo docker ps --filter "ancestor=${DOCKER_APP_NAME}-blue" --filter "status=running") - # docker-compose.blue.yml 파일을 사용하여 spring-blue 프로젝트의 컨테이너를 빌드하고 실행 - sudo docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml up -d --build + # 배포 시작한 날짜와 시간을 기록 + echo "배포 시작일자 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log - # 30초 동안 대기 - while [ 1 = 1 ]; do - echo ">>> spring blue health check ..." - sleep 3 - REQUEST_SPRING=$(curl 127.0.0.1:8081/actuator/health) - if [ -n "$REQUEST_SPRING" ]; then - echo ">>> spring blue health check success !" - break; - fi - done; + # green이 실행중이면 blue up + # EXIST_BLUE 변수가 비어있는지 확인 + if [ -z "$EXIST_REDIS" ]; then + # 로그 파일(/home/ubuntu/deploy.log)에 "blue up - blue 배포 : port:8081"이라는 내용을 추가 + echo "blue 배포 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log - # /home/ubuntu/deploy.log: 로그 파일에 "green 중단 시작"이라는 내용을 추가 - echo "green 중단 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + # docker-compose.prod.yml 파일을 사용하여 prod-blue, redis 서비스를 빌드하고 실행 + sudo docker-compose -f docker-compose.prod.yml up redis ${DOCKER_APP_NAME}-blue -d --build - # docker-compose.green.yml 파일을 사용하여 spring-green 프로젝트의 컨테이너를 중지 - sudo docker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml down + while [ 1 = 1 ]; do + echo ">>> spring blue health check ..." + sleep 3 + REQUEST_SPRING=$(curl 127.0.0.1:8081/actuator/health) + if [ -n "$REQUEST_SPRING" ]; then + echo ">>> spring blue health check success !" + break; + fi + done; - # 사용하지 않는 이미지 삭제 - sudo docker image prune -af + elif [ -z "$EXIST_BLUE" ]; then - echo "green 중단 완료 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + # 로그 파일(/home/ubuntu/deploy.log)에 "blue up - blue 배포 : port:8081"이라는 내용을 추가 + echo "blue 배포 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log -# blue가 실행중이면 green up -else - echo "green 배포 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log - sudo docker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml up -d --build + # docker-compose.prod.yml 파일을 사용하여 prod-blue 서비스를 빌드하고 실행 + sudo docker-compose -f docker-compose.prod.yml up ${DOCKER_APP_NAME}-blue -d --build while [ 1 = 1 ]; do - echo ">>> spring green health check ..." + echo ">>> spring blue health check ..." sleep 3 - REQUEST_SPRING=$(curl 127.0.0.1:8082/actuator/health) + REQUEST_SPRING=$(curl 127.0.0.1:8081/actuator/health) if [ -n "$REQUEST_SPRING" ]; then - echo ">>> spring green health check success !" + echo ">>> spring blue health check success !" break; fi done; - echo "blue 중단 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log - sudo docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml down - sudo docker image prune -af + # /home/ubuntu/deploy.log: 로그 파일에 "green 중단 시작"이라는 내용을 추가 + echo "green 중단 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log - echo "blue 중단 완료 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + # docker-compose.prod.yml 파일을 사용하여 spring-app-green 서비스 중지 + sudo docker-compose -f docker-compose.prod.yml down ${DOCKER_APP_NAME}-green -fi - echo "배포 종료 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + # 사용하지 않는 이미지 삭제 + sudo docker image prune -af + + echo "green 중단 완료 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + + # blue가 실행중이면 green up + else + echo "green 배포 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + sudo docker-compose -f docker-compose.prod.yml up ${DOCKER_APP_NAME}-green -d --build - echo "===================== 배포 완료 =====================" >> /home/ubuntu/deploy.log - echo >> /home/ubuntu/deploy.log \ No newline at end of file + while [ 1 = 1 ]; do + echo ">>> spring green health check ..." + sleep 3 + REQUEST_SPRING=$(curl 127.0.0.1:8082/actuator/health) + if [ -n "$REQUEST_SPRING" ]; then + echo ">>> spring green health check success !" + break; + fi + done; + + echo "blue 중단 시작 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + # docker-compose.prod.yml 파일을 사용하여 spring-app-blue 서비스 중지 + sudo docker-compose -f docker-compose.prod.yml down ${DOCKER_APP_NAME}-blue + + sudo docker image prune -af + + echo "blue 중단 완료 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + + fi + echo "배포 종료 : $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)" >> /home/ubuntu/deploy.log + + echo "===================== 배포 완료 =====================" >> /home/ubuntu/deploy.log + echo >> /home/ubuntu/deploy.log +#만약 배포가 개발이라면 +else + cd /home/ubuntu/app/dev + # 현재 실행 중인 컨테이너를 중지하고 제거합니다 + docker-compose -f docker-compose.dev.yml down + # 새 이미지를 빌드하고 컨테이너를 백그라운드에서 실행합니다 + docker-compose -f docker-compose.dev.yml up -d --build + while [ 1 = 1 ]; do + echo ">>> spring green health check ..." + sleep 3 + REQUEST_SPRING=$(curl 127.0.0.1:1821/actuator/health) + if [ -n "$REQUEST_SPRING" ]; then + echo ">>> spring green health check success !" + break; + fi + done; + sudo docker image prune -af +fi \ No newline at end of file From b0b0987dc202d0cd8d24d2aa982b2e05d01311f0 Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 11:30:25 +0900 Subject: [PATCH 07/10] =?UTF-8?q?[CHORE]=20=EC=95=95=EC=B6=95=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95=20=EB=B0=8F?= =?UTF-8?q?=20=ED=82=A4=20=ED=8C=8C=EC=9D=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-dev.yml | 15 +++++++++++---- .github/workflows/deploy-prod.yml | 13 ++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 6ca02dc3..851d15e7 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -49,12 +49,16 @@ jobs: - name: 🗂️ Make config run: | # src/main/resources 경로 이동 - cd ./src/main/resources + cd ./src/main/resources # yml 파일 생성 touch ./application-dev.yml echo "$APPLICATION_DEV" > ./application-dev.yml + # 키 파일 생성 + touch ./private-key.pem + echo "$CLOUD_FRONT_KEY" > ./private-key.pem + # 폴더 생성 mkdir templates @@ -74,6 +78,7 @@ jobs: MAIL_APPROVE_TEMPLATE: ${{ secrets.MAIL_APPROVE_TEMPLATE }} MAIL_VERIFY_TEMPLATE: ${{ secrets.MAIL_VERIFY_TEMPLATE }} MAIL_APPLICATION_NOTIFICATION_TEMPLATE: ${{ secrets.MAIL_APPLICATION_NOTIFICATION_TEMPLATE }} + CLOUD_FRONT_KEY: ${{ secrets.CLOUD_FRONT_KEY }} shell: bash - name: ⚙️ Gradle 권한 부여 @@ -85,11 +90,13 @@ jobs: - name: 📦 zip file 생성 run: | mkdir deploy - cp ./docker/dev/docker-compose.dev.yml ./deploy/ - cp ./docker/dev/Dockerfile ./deploy/ + mkdir deploy/dev + cp ./docker/dev/docker-compose.dev.yml ./deploy/dev + cp ./docker/dev/Dockerfile ./deploy/dev + cp ./src/main/resources/private-key.pem ./deploy/dev + cp ./build/libs/*.jar ./deploy/dev cp ./appspec.yml ./deploy/ cp ./scripts/deploy.sh ./deploy/ - cp ./build/libs/*.jar ./deploy/ zip -r -qq -j ./spring-app.zip ./deploy diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml index 88a7a323..4c9b4be3 100644 --- a/.github/workflows/deploy-prod.yml +++ b/.github/workflows/deploy-prod.yml @@ -55,6 +55,10 @@ jobs: touch ./application-prod.yml echo "$APPLICATION_PROD" > ./application-prod.yml + # 키 파일 생성 + touch ./private-key.pem + echo "$CLOUD_FRONT_KEY" > ./private-key.pem + # 폴더 생성 mkdir templates @@ -74,6 +78,7 @@ jobs: MAIL_APPROVE_TEMPLATE: ${{ secrets.MAIL_APPROVE_TEMPLATE }} MAIL_VERIFY_TEMPLATE: ${{ secrets.MAIL_VERIFY_TEMPLATE }} MAIL_APPLICATION_NOTIFICATION_TEMPLATE: ${{ secrets.MAIL_APPLICATION_NOTIFICATION_TEMPLATE }} + CLOUD_FRONT_KEY: ${{ secrets.CLOUD_FRONT_KEY }} shell: bash - name: ⚙️ Gradle 권한 부여 @@ -85,9 +90,11 @@ jobs: - name: 📦 zip file 생성 run: | mkdir deploy - cp ./docker/prod/docker-compose.prod.yml ./deploy/ - cp ./docker/prod/Dockerfile ./deploy/ - cp ./appspec.yml ./deploy/ + mkdir deploy/prod + cp ./docker/prod/docker-compose.prod.yml ./deploy/prod + cp ./docker/prod/Dockerfile ./deploy/prod + cp ./src/main/resources/private-key.pem ./deploy/prod + cp ./build/libs/*.jar ./deploy/prod cp ./scripts/deploy.sh ./deploy/ cp ./build/libs/*.jar ./deploy/ zip -r -qq -j ./spring-app.zip ./deploy From a68f00c1f2ade23a7e16aa630a5e41977487b0aa Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 11:34:52 +0900 Subject: [PATCH 08/10] =?UTF-8?q?[CHORE]=20=EB=B0=B0=ED=8F=AC=20=EA=B7=B8?= =?UTF-8?q?=EB=A3=B9=20=EC=B6=9C=EB=A0=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/deploy.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index c2d987f5..0c5b02d8 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -12,6 +12,7 @@ DEPLOYMENT_DETAILS=$(aws deploy get-deployment --deployment-id $DEPLOYMENT_ID) # 배포 그룹 이름 추출 DEPLOYMENT_GROUP_NAME=$(echo $DEPLOYMENT_DETAILS | jq -r '.deploymentInfo.deploymentGroupName') +echo $DEPLOYMENT_GROUP_NAME # 만약 배포가 prod 라면 if [ "$DEPLOYMENT_GROUP_NAME" = "spring-app" ]; then From 4372904987cb044e01104e5605c4afa68f54ab49 Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 11:49:33 +0900 Subject: [PATCH 09/10] =?UTF-8?q?[CHORE]=20-j=20=EC=98=B5=EC=85=98=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-dev.yml | 2 +- .github/workflows/deploy-prod.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 851d15e7..acfb5c18 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -97,7 +97,7 @@ jobs: cp ./build/libs/*.jar ./deploy/dev cp ./appspec.yml ./deploy/ cp ./scripts/deploy.sh ./deploy/ - zip -r -qq -j ./spring-app.zip ./deploy + zip -r -qq ./spring-app.zip ./deploy # AWS에 연결해준다. diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml index 4c9b4be3..827436f6 100644 --- a/.github/workflows/deploy-prod.yml +++ b/.github/workflows/deploy-prod.yml @@ -97,7 +97,7 @@ jobs: cp ./build/libs/*.jar ./deploy/prod cp ./scripts/deploy.sh ./deploy/ cp ./build/libs/*.jar ./deploy/ - zip -r -qq -j ./spring-app.zip ./deploy + zip -r -qq ./spring-app.zip ./deploy # AWS에 연결해준다. From 132ad9353a0973cde1d058406b7c39ae54228eb2 Mon Sep 17 00:00:00 2001 From: goder-0 Date: Wed, 31 Jul 2024 13:31:13 +0900 Subject: [PATCH 10/10] =?UTF-8?q?[CHORE]=20=EC=98=A4=ED=83=80=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml index 827436f6..935108d8 100644 --- a/.github/workflows/deploy-prod.yml +++ b/.github/workflows/deploy-prod.yml @@ -96,7 +96,7 @@ jobs: cp ./src/main/resources/private-key.pem ./deploy/prod cp ./build/libs/*.jar ./deploy/prod cp ./scripts/deploy.sh ./deploy/ - cp ./build/libs/*.jar ./deploy/ + cp ./appspec.yml ./deploy/ zip -r -qq ./spring-app.zip ./deploy