From 364a49302a992cda1e94881f648a4f602136d2a1 Mon Sep 17 00:00:00 2001 From: jj Date: Mon, 19 May 2025 21:31:39 +0900 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20health=20=EC=B2=B4=ED=81=AC=20?= =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/controller/StatusCheckController.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/com/blog/global/controller/StatusCheckController.java diff --git a/src/main/java/com/blog/global/controller/StatusCheckController.java b/src/main/java/com/blog/global/controller/StatusCheckController.java new file mode 100644 index 00000000..1694e28d --- /dev/null +++ b/src/main/java/com/blog/global/controller/StatusCheckController.java @@ -0,0 +1,15 @@ +package com.blog.global.controller; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class StatusCheckController { + + @GetMapping("/health-check") + public ResponseEntity checkHealthStatus() { + + return ResponseEntity.ok("OK"); + } +} From 4b94d79f02e17076c6db5041fb92af0f8054dd0b Mon Sep 17 00:00:00 2001 From: jj Date: Mon, 19 May 2025 21:32:28 +0900 Subject: [PATCH 02/11] =?UTF-8?q?chore:=20=EB=B8=94=EB=A3=A8-=EA=B7=B8?= =?UTF-8?q?=EB=A6=B0=20=EB=B0=B0=ED=8F=AC=EB=A5=BC=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?dev.yml=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 77 ++++++++++++++++++++++++++ src/main/resources/application-dev.yml | 13 +++++ 2 files changed, 90 insertions(+) create mode 100644 .github/workflows/dev.yml create mode 100644 src/main/resources/application-dev.yml diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml new file mode 100644 index 00000000..82e271c7 --- /dev/null +++ b/.github/workflows/dev.yml @@ -0,0 +1,77 @@ +name: Java CI with Gradle + +on: + push: + branches: [ "dev" ] + pull_request: + branches: [ "dev" ] + types: [ opened, synchronize, reopened ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout the repository + uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' + + # gradle caching + - name: Gradle Caching + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*gradle*','**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + # Gradle Build (test는 제외) + - name: Build with Gradle + run: ./gradlew build -x test + + # 도커 허브에 로그인 + - name: Docker Hub Login + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USER_NAME }} + password: ${{ secrets.DOCKER_USER_TOKEN }} + + - name: Docker build & push + run: | + docker build -t ${{ secrets.DOCKER_USER_NAME }}/blog-dev -f ./Dockerfile-dev . + docker push ${{ secrets.DOCKER_USER_NAME }}/blog-dev + + deploy: + runs-on: ubuntu-latest + needs: build + # if: github.event_name == 'push' 테스트를 위해 주석 처리 + + steps: + - name: Copy deploy script file to remote + uses: appleboy/scp-action@master + with: + host: ${{ secrets.DEV_HOST }} + username: ${{ secrets.DEV_EC2_USER }} + key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} + port: 22 + source: "deploy.sh" + target: "/home/ubuntu/cicd" + + - name: Deploy to EC2 + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.DEV_HOST }} + username: ${{ secrets.DEV_EC2_USER }} + key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} + port: 22 + script: | + chmod +x /home/ubuntu/cicd/deploy.sh + sh /home/ubuntu/cicd/deploy.sh + debug: true diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 00000000..99753acb --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,13 @@ +spring: + datasource: + url: ${DB_URL} + username: ${RDS_USER_NAME} + password: ${RDS_USER_PW} + driver-class-name: com.mysql.cj.jdbc.Driver + jpa: + show-sql: true + properties: + hibernate: + dialect: org.hibernate.dialect.MySQLDialect + hibernate: + ddl-auto: update \ No newline at end of file From 1be3108dac0811f3940b5e664b9ccba4d8bc29b0 Mon Sep 17 00:00:00 2001 From: jj Date: Mon, 19 May 2025 21:42:51 +0900 Subject: [PATCH 03/11] =?UTF-8?q?chore:=20=EB=B0=B0=ED=8F=AC=20=EB=B8=8C?= =?UTF-8?q?=EB=9E=9C=EC=B9=98=EB=A5=BC=20dev=20=E2=86=92=20main=20?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 82e271c7..b0ea788d 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -2,9 +2,9 @@ name: Java CI with Gradle on: push: - branches: [ "dev" ] + branches: [ "main" ] pull_request: - branches: [ "dev" ] + branches: [ "main" ] types: [ opened, synchronize, reopened ] jobs: From d568c7c54d517aa7fba407cf0ebd990393a01370 Mon Sep 17 00:00:00 2001 From: jj Date: Mon, 19 May 2025 21:52:22 +0900 Subject: [PATCH 04/11] =?UTF-8?q?feat:=20=EA=B0=9C=EB=B0=9C=20=ED=99=98?= =?UTF-8?q?=EA=B2=BD=EC=9A=A9=20Dockerfile-dev=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile-dev | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Dockerfile-dev diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 00000000..05aabe61 --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,12 @@ +# open jdk 21버전 +FROM openjdk:21-jdk + +# build가 되는 시점에 JAR_FILE이라는 변수 명에 build/libs/*.jar 선언 +# build/libs - gradle로 빌드했을 때 jar 파일이 생성되는 경로 +ARG JAR_FILE=build/libs/*.jar + +# JAR_FILE을 app.jar로 복사 +COPY ${JAR_FILE} docker-springboot.jar + +# 운영 및 개발에서 사용되는 환경 설정을 분리 +ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=dev", "/docker-springboot.jar"] \ No newline at end of file From a8e49c86d8d31f9a362e8b3de6ab7c9103669792 Mon Sep 17 00:00:00 2001 From: jj Date: Mon, 19 May 2025 22:11:15 +0900 Subject: [PATCH 05/11] =?UTF-8?q?feat:=20=EB=B0=B0=ED=8F=AC=20deploy.sh=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 deploy.sh diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 00000000..2f4b3058 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,36 @@ +cd /home/ubuntu/compose + +EXIST_BLUE=$(docker inspect -f '{{.State.Running}}' spring-blue 2>/dev/null) + +if [ "$EXIST_BLUE" != "true" ]; then + docker compose up -d spring-blue + BEFORE_COLOR="green" + AFTER_COLOR="blue" + BEFORE_PORT=8081 + AFTER_PORT=8080 +else + docker compose up -d spring-green + BEFORE_COLOR="blue" + AFTER_COLOR="green" + BEFORE_PORT=8080 + AFTER_PORT=8081 +fi + +echo "===== ${AFTER_COLOR} server upc(port:${AFTER_PORT}) =====" + +for cnt in {1..10} +do + echo "===== 서버 응답 확인중(${cnt}/10) =====" + UP=$(curl -s http://localhost:${AFTER_PORT}/health-check) + if [ "$UP" != "OK" ]; then + sleep 10 + continue + else + break + fi +done + +if [ $cnt -eq 10 ]; then + echo "===== 서버 실행 실패 =====" + exit 1 +fi \ No newline at end of file From 559c61ca3923c6fe32903183195c406b8b973d8d Mon Sep 17 00:00:00 2001 From: jj Date: Tue, 20 May 2025 00:11:24 +0900 Subject: [PATCH 06/11] =?UTF-8?q?refactor:=20deploy.sh=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 5 +++-- deploy.sh => scripts/deploy.sh | 0 2 files changed, 3 insertions(+), 2 deletions(-) rename deploy.sh => scripts/deploy.sh (100%) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index b0ea788d..4bdb26ae 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -54,15 +54,16 @@ jobs: # if: github.event_name == 'push' 테스트를 위해 주석 처리 steps: - - name: Copy deploy script file to remote + - name: Copy deploy.sh to EC2 uses: appleboy/scp-action@master with: host: ${{ secrets.DEV_HOST }} username: ${{ secrets.DEV_EC2_USER }} key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} port: 22 - source: "deploy.sh" + source: "/scripts/deploy.sh" target: "/home/ubuntu/cicd" + rm: true - name: Deploy to EC2 uses: appleboy/ssh-action@v1.0.3 diff --git a/deploy.sh b/scripts/deploy.sh similarity index 100% rename from deploy.sh rename to scripts/deploy.sh From e5b5e073b577fadc3d3ebff69bdfa7f0a5ef9640 Mon Sep 17 00:00:00 2001 From: jj Date: Tue, 20 May 2025 00:25:11 +0900 Subject: [PATCH 07/11] =?UTF-8?q?refactor:=20deploy.sh=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EA=B2=BD=EB=A1=9C=20=EC=83=81=EB=8C=80=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 4bdb26ae..5b7c2525 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -61,7 +61,7 @@ jobs: username: ${{ secrets.DEV_EC2_USER }} key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} port: 22 - source: "/scripts/deploy.sh" + source: "scripts/deploy.sh" target: "/home/ubuntu/cicd" rm: true From b8ca722bac71c7abe65e0a4c3c577decab44ab97 Mon Sep 17 00:00:00 2001 From: jj Date: Tue, 20 May 2025 00:34:44 +0900 Subject: [PATCH 08/11] =?UTF-8?q?refactor:=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=EB=8B=A8=EA=B3=84=EC=97=90=EC=84=9C=20=EB=A0=88=ED=8F=AC?= =?UTF-8?q?=EC=A7=80=ED=86=A0=EB=A6=AC=20=EC=BD=94=EB=93=9C=20=EC=A0=91?= =?UTF-8?q?=EA=B7=BC=EC=9D=84=20=EC=9C=84=ED=95=9C=20checkout=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 5b7c2525..7ac56427 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -54,6 +54,9 @@ jobs: # if: github.event_name == 'push' 테스트를 위해 주석 처리 steps: + - name: Checkout the repository + uses: actions/checkout@v4 + - name: Copy deploy.sh to EC2 uses: appleboy/scp-action@master with: From 36bae00b0fd11f0f8b0b6fe2d19a711f0bd0328b Mon Sep 17 00:00:00 2001 From: jj Date: Tue, 20 May 2025 01:08:51 +0900 Subject: [PATCH 09/11] =?UTF-8?q?refactor:=20EC2=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EB=B3=B5=EC=82=AC=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=EB=B0=8F=20rm=20=EC=98=B5=EC=85=98=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 7ac56427..87548718 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -65,8 +65,7 @@ jobs: key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} port: 22 source: "scripts/deploy.sh" - target: "/home/ubuntu/cicd" - rm: true + target: "/home/ubuntu/cicd/deploy.sh" - name: Deploy to EC2 uses: appleboy/ssh-action@v1.0.3 From 778cdf06f5c070a6736ffbbf074436e128d9274f Mon Sep 17 00:00:00 2001 From: jj Date: Tue, 20 May 2025 01:13:21 +0900 Subject: [PATCH 10/11] =?UTF-8?q?refactor:=20EC2=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EB=B3=B5=EC=82=AC=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 87548718..8388a0ab 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -65,7 +65,7 @@ jobs: key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} port: 22 source: "scripts/deploy.sh" - target: "/home/ubuntu/cicd/deploy.sh" + target: "/home/ubuntu" - name: Deploy to EC2 uses: appleboy/ssh-action@v1.0.3 @@ -75,6 +75,6 @@ jobs: key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} port: 22 script: | - chmod +x /home/ubuntu/cicd/deploy.sh - sh /home/ubuntu/cicd/deploy.sh + chmod +x /home/ubuntu/scripts/deploy.sh + sh /home/ubuntu/scripts/deploy.sh debug: true From 112525299077a49b12f58974d6422f90be9fdc18 Mon Sep 17 00:00:00 2001 From: jj Date: Tue, 20 May 2025 01:29:52 +0900 Subject: [PATCH 11/11] =?UTF-8?q?feat:=20=EA=B8=B0=EC=A1=B4=20=EC=BB=A8?= =?UTF-8?q?=ED=85=8C=EC=9D=B4=EB=84=88=EB=A5=BC=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=EC=8B=9C=20=EC=9E=90=EB=8F=99=EC=9C=BC=EB=A1=9C=20=EC=A4=91?= =?UTF-8?q?=EC=A7=80=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/deploy.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 2f4b3058..5a657c35 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -33,4 +33,13 @@ done if [ $cnt -eq 10 ]; then echo "===== 서버 실행 실패 =====" exit 1 -fi \ No newline at end of file +fi + +echo "===== Caddy 설정 변경 =====" +sudo sed -i "s/${BEFORE_PORT}/${AFTER_PORT}/g" /etc/caddy/Caddyfile + +sudo caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile + +echo "$BEFORE_COLOR server down(port:${BEFORE_PORT})" +docker compose stop spring-${BEFORE_COLOR} +docker compose rm -f spring-${BEFORE_COLOR} \ No newline at end of file