From c5ebcc139599770a1c5272aca864214243c54835 Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 15:32:29 +0900 Subject: [PATCH 1/9] =?UTF-8?q?[Feat]=20#120=20blue-green=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=EB=A5=BC=20=EC=9C=84=ED=95=9C=20docker-compose=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index b747a5e..f770cac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,5 @@ services: - findyou: + findyou_blue: image: ksg1227/findyou:latest env_file: - .env @@ -12,6 +12,27 @@ services: - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} - SERVICE_KEY=${SERVICE_KEY} + - CACHE_ENDPOINT=${CACHE_ENDPOINT} + - JWT_SECRET_KEY=${JWT_SECRET_KEY} + - TZ=Asia/Seoul + volumes: + - ${LOG_DIRECTORY}:/logs + + findyou_green: + image: ksg1227/findyou:latest + env_file: + - .env + ports: + - "9002:9001" + environment: + - DEV_DATASOURCE_URL=${DEV_DATASOURCE_URL} + - DEV_DATASOURCE_USERNAME=${DEV_DATASOURCE_USERNAME} + - DEV_DATASOURCE_PASSWORD=${DEV_DATASOURCE_PASSWORD} + - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} + - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} + - SERVICE_KEY=${SERVICE_KEY} + - CACHE_ENDPOINT=${CACHE_ENDPOINT} + - JWT_SECRET_KEY=${JWT_SECRET_KEY} - TZ=Asia/Seoul volumes: - ${LOG_DIRECTORY}:/logs \ No newline at end of file From 9188e225934e8b8c7a79e539bcb8880b6ceb03bb Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 15:41:07 +0900 Subject: [PATCH 2/9] =?UTF-8?q?[Feat]=20#120=20actuator=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=84=A4=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 +++ .../java/com/kuit/findyou/global/config/SecurityConfig.java | 2 +- src/main/resources/application.yml | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index b2141b6..c3e223c 100644 --- a/build.gradle +++ b/build.gradle @@ -78,6 +78,9 @@ dependencies { // Security implementation("org.springframework.boot:spring-boot-starter-security") testImplementation("org.springframework.security:spring-security-test") + + // Actuator + implementation 'org.springframework.boot:spring-boot-starter-actuator' } tasks.named('test') { diff --git a/src/main/java/com/kuit/findyou/global/config/SecurityConfig.java b/src/main/java/com/kuit/findyou/global/config/SecurityConfig.java index 0810b66..cd36293 100644 --- a/src/main/java/com/kuit/findyou/global/config/SecurityConfig.java +++ b/src/main/java/com/kuit/findyou/global/config/SecurityConfig.java @@ -30,7 +30,7 @@ public class SecurityConfig { private static final String[] PERMIT_URL = { LOGIN_ENDPOINT.getValue(), "api/v1/auth/signup", "/swagger-ui/**", "/api-docs", "/swagger-ui-custom.html", - "/v3/api-docs/**", "/api-docs/**", "/swagger-ui.html", "/swagger-ui/index.html" + "/v3/api-docs/**", "/api-docs/**", "/swagger-ui.html", "/swagger-ui/index.html", "/actuator/health" }; @Bean diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 3eecf40..97fc1ab 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -12,6 +12,11 @@ spring: web: resources: add-mappings: false +management: + endpoints: + web: + exposure: + include: "health, info" --- # 로컬에서 사용하는 DB spring: From 44bcf76c35187d20dd94842a620f2ce00328973c Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 15:48:30 +0900 Subject: [PATCH 3/9] =?UTF-8?q?[Feat]=20#120=20deploy.sh=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 deploy.sh diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..cb659d2 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +IS_BLUE_RUNNING=$(docker ps | grep findyou_blue) +export NGINX_CONF="/etc/nginx/nginx.conf" + +# blue 가 실행 중이면 green 을 up +if [ -n "$IS_BLUE_RUNNING" ]; then + echo "### BLUE => GREEN ####" + + echo ">>> green 컨테이너 실행" + docker compose up -d findyou_green + sleep 7 + + echo ">>> health check 진행..." + while true; do + RESPONSE=$(curl http://localhost:9002/actuator/health | grep UP) + if [ -n "$RESPONSE" ]; then + echo ">>> green health check 성공! " + break; + fi + sleep 3 + done; + + echo ">>> Nginx 설정 변경 (green)" + sudo sed -i 's/set $ACTIVE_APP findyou_blue;/set $ACTIVE_APP findyou_green;/' $NGINX_CONF + sudo nginx -s reload + + echo ">>> blue 컨테이너 종료" + docker compose stop findyou_blue + +# green 이 실행 중이면 blue 를 up +else + echo "### GREEN => BLUE ####" + + echo ">>> blue 컨테이너 실행" + docker compose up -d findyou_blue + sleep 7 + + echo ">>> health check 진행..." + while true; do + RESPONSE=$(curl http://localhost:9001/actuator/health | grep UP) + if [ -n "$RESPONSE" ]; then + echo ">>> blue health check 성공! " + break; + fi + sleep 3 + done; + + echo ">>> Nginx 설정 변경 (blue)" + sudo sed -i 's/set $ACTIVE_APP findyou_green;/set $ACTIVE_APP findyou_blue;/' $NGINX_CONF + sudo nginx -s reload + + echo ">>> green 컨테이너 종료" + docker compose stop findyou_green +fi + +echo ">>> 사용하지 않는 도커 이미지들 정리" +docker image prune -a -f \ No newline at end of file From 7ae2ef8fa471cfe8b221f58adc3942466eee7b69 Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 15:57:19 +0900 Subject: [PATCH 4/9] =?UTF-8?q?[Deploy]=20#120=20deploy.sh=20=EB=A5=BC=20?= =?UTF-8?q?=ED=99=9C=EC=9A=A9=ED=95=9C=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20?= =?UTF-8?q?=EB=B0=B0=ED=8F=AC=EB=A5=BC=20=EC=88=98=ED=96=89=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20cd-worklow.yml=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-workflow.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml index f79fa9c..68bfeb3 100644 --- a/.github/workflows/cd-workflow.yml +++ b/.github/workflows/cd-workflow.yml @@ -74,7 +74,7 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_KEY }} - source: "./docker-compose.yml" + source: "./docker-compose.yml, ./deploy.sh" target: "/home/ec2-user/findyou/" - name: 🚀 deploy to server @@ -89,16 +89,10 @@ jobs: echo "🗂️ Change Directory to EC2 Root" cd /home/ec2-user/findyou - echo "✋🏻Stopping existing container and Cleaning up old images" - sudo docker compose down --rmi all + chmod +x ./deploy.sh - sudo docker ps -a - - echo "🥳 Pulling new image" - sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} - - echo "🌱 Starting new container" - sudo docker compose up -d + echo "✋🏻Stopping Existing Container and Deploy New Container" + ./deploy.sh - name: ❌ Remove GitHub Actions IP run: | From a225ccb82d0551caf05bbb614ced20bc6c9e2e9f Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 16:06:39 +0900 Subject: [PATCH 5/9] =?UTF-8?q?[Deploy]=20#120=20cd-workflow.yml=20?= =?UTF-8?q?=EC=97=90=20step=20=EC=84=A4=EB=AA=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml index 68bfeb3..3abd77e 100644 --- a/.github/workflows/cd-workflow.yml +++ b/.github/workflows/cd-workflow.yml @@ -68,7 +68,7 @@ jobs: --port 22 \ --cidr ${{ steps.ip.outputs.ipv4 }}/32 - - name: ✉️ Send docker-compose.yml + - name: ✉️ Send docker-compose.yml & deploy.sh uses: appleboy/scp-action@master with: host: ${{ secrets.EC2_HOST }} From f523cf82419d1a0ead5d3c779655d9f9a9a4344b Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 16:13:04 +0900 Subject: [PATCH 6/9] =?UTF-8?q?[Deploy]=20#120=20cd-workflow.yml=20?= =?UTF-8?q?=EC=97=90=20=EC=A3=BC=EC=84=9D=20=EC=9D=BC=EB=B6=80=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/cd-workflow.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml index 3abd77e..201e0f7 100644 --- a/.github/workflows/cd-workflow.yml +++ b/.github/workflows/cd-workflow.yml @@ -89,9 +89,10 @@ jobs: echo "🗂️ Change Directory to EC2 Root" cd /home/ec2-user/findyou + echo "👉🏻 Grant Permission to deploy.sh chmod +x ./deploy.sh - echo "✋🏻Stopping Existing Container and Deploy New Container" + echo "✋🏻 Stopping Existing Container and Deploy New Container" ./deploy.sh - name: ❌ Remove GitHub Actions IP From afe5ac60af37f3063677d198e420307b569a3cf9 Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 16:17:06 +0900 Subject: [PATCH 7/9] =?UTF-8?q?[Fix]=20#120=20=EC=8C=8D=EB=94=B0=EC=98=B4?= =?UTF-8?q?=ED=91=9C=20=EB=88=84=EB=9D=BD=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml index 201e0f7..b37d332 100644 --- a/.github/workflows/cd-workflow.yml +++ b/.github/workflows/cd-workflow.yml @@ -89,7 +89,7 @@ jobs: echo "🗂️ Change Directory to EC2 Root" cd /home/ec2-user/findyou - echo "👉🏻 Grant Permission to deploy.sh + echo "👉🏻 Grant Permission to deploy.sh" chmod +x ./deploy.sh echo "✋🏻 Stopping Existing Container and Deploy New Container" From 831302754d406772e394edae181f1476d6c5d4ca Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 16:24:46 +0900 Subject: [PATCH 8/9] =?UTF-8?q?[Deploy]=20#120=20=EB=A7=A4=EB=B2=88=20?= =?UTF-8?q?=EC=83=88=EB=A1=9C=EC=9A=B4=20=EC=9D=B4=EB=AF=B8=EC=A7=80?= =?UTF-8?q?=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/deploy.sh b/deploy.sh index cb659d2..49d5636 100644 --- a/deploy.sh +++ b/deploy.sh @@ -7,8 +7,12 @@ export NGINX_CONF="/etc/nginx/nginx.conf" if [ -n "$IS_BLUE_RUNNING" ]; then echo "### BLUE => GREEN ####" - echo ">>> green 컨테이너 실행" - docker compose up -d findyou_green + # 최신 이미지 강제 가져오기 + echo ">>> 최신 green 이미지 가져오기" + docker compose pull findyou_green + + echo ">>> green 컨테이너 실행 (기존 이미지 무시)" + docker compose up -d --force-recreate findyou_green sleep 7 echo ">>> health check 진행..." @@ -32,8 +36,12 @@ if [ -n "$IS_BLUE_RUNNING" ]; then else echo "### GREEN => BLUE ####" - echo ">>> blue 컨테이너 실행" - docker compose up -d findyou_blue + # 최신 이미지 강제 가져오기 + echo ">>> 최신 blue 이미지 가져오기" + docker compose pull findyou_blue + + echo ">>> blue 컨테이너 실행 (기존 이미지 무시)" + docker compose up -d --force-recreate findyou_blue sleep 7 echo ">>> health check 진행..." @@ -55,4 +63,4 @@ else fi echo ">>> 사용하지 않는 도커 이미지들 정리" -docker image prune -a -f \ No newline at end of file +docker image prune -a -f # 모든 사용하지 않는 이미지 삭제 From 8332a2ba21e55961e050926d2fc3c67cf9252736 Mon Sep 17 00:00:00 2001 From: ksg Date: Fri, 2 May 2025 16:33:37 +0900 Subject: [PATCH 9/9] =?UTF-8?q?[Deploy]=20#120=20=EC=A2=85=EB=A3=8C?= =?UTF-8?q?=EB=90=9C=20=EC=BB=A8=ED=85=8C=EC=9D=B4=EB=84=88=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC=20=EB=B6=80=EB=B6=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/deploy.sh b/deploy.sh index 49d5636..662502e 100644 --- a/deploy.sh +++ b/deploy.sh @@ -62,5 +62,10 @@ else docker compose stop findyou_green fi +echo ">>> 종료된 컨테이너들 정리" +docker container prune -f + echo ">>> 사용하지 않는 도커 이미지들 정리" docker image prune -a -f # 모든 사용하지 않는 이미지 삭제 + +