From 8110f41819ebaffa3de86efa8942daa315bfb5e6 Mon Sep 17 00:00:00 2001 From: LeeJaeHoon Date: Thu, 17 Oct 2024 10:38:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev-deploy.yml | 6 +- .github/workflows/staging-deploy.yml | 12 ---- deploy/deploy.sh | 56 +++++++++++++++++++ .../controller/v1/snap/SnapController.java | 2 + .../snap/repository/SnapRepository.java | 1 + 5 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 deploy/deploy.sh diff --git a/.github/workflows/dev-deploy.yml b/.github/workflows/dev-deploy.yml index 3b946fc..b1a683e 100644 --- a/.github/workflows/dev-deploy.yml +++ b/.github/workflows/dev-deploy.yml @@ -48,7 +48,7 @@ jobs: run: | docker push ${{ secrets.DOCKER_HUB_USERNAME }}/dnd-server:latest - - name: SSH로 EC2에 접속하여 Docker Compose 실행 + - name: SSH로 EC2에 접속하여 배포 스크립트 실행 uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.EC2_HOST }} @@ -56,6 +56,4 @@ jobs: key: ${{ secrets.EC2_PRIVATE_KEY }} script_stop: true script: | - docker-compose -f /home/ubuntu/compose/docker-compose.yml down || true - docker-compose -f /home/ubuntu/compose/docker-compose.yml pull - docker-compose -f /home/ubuntu/compose/docker-compose.yml up -d + bash /home/ubuntu/compose/deploy.sh diff --git a/.github/workflows/staging-deploy.yml b/.github/workflows/staging-deploy.yml index 948bd9d..86e636b 100644 --- a/.github/workflows/staging-deploy.yml +++ b/.github/workflows/staging-deploy.yml @@ -47,15 +47,3 @@ jobs: - name: Docker Hub에 Docker 이미지 Push하기 run: | docker push ${{ secrets.DOCKER_HUB_USERNAME }}/dnd-staging-server:latest - -# - name: SSH로 EC2에 접속하여 Docker Compose 실행 -# uses: appleboy/ssh-action@v1.0.3 -# with: -# host: ${{ secrets.EC2_HOST }} -# username: ${{ secrets.EC2_USERNAME }} -# key: ${{ secrets.EC2_PRIVATE_KEY }} -# script_stop: true -# script: | -# docker-compose -f /home/ubuntu/compose/docker-compose.yml down || true -# docker-compose -f /home/ubuntu/compose/docker-compose.yml pull -# docker-compose -f /home/ubuntu/compose/docker-compose.yml up -d diff --git a/deploy/deploy.sh b/deploy/deploy.sh new file mode 100644 index 0000000..f45c41c --- /dev/null +++ b/deploy/deploy.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +IP=127.0.0.1 +IS_DEV1=$(docker ps | grep dev1) + +if [ -z "$IS_DEV1" ];then + BLUE_PORT="8081" + GREEN_PORT="8080" + CONTAINER_NAME="dev1" + STOP_CONTAINER_NAME="dev2" +else + BLUE_PORT="8080" + GREEN_PORT="8081" + CONTAINER_NAME="dev2" + STOP_CONTAINER_NAME="dev1" +fi + +if [ $GREEN_PORT == "none" ] +then + echo "블루와 그린을 임으로 지정합니다.\n" + BLUE_PORT="8080" + GREEN_PORT="8081" +fi + +echo -e "그린($GREEN_PORT)과 블루($BLUE_PORT) 서버 확인 ${CONTAINER_NAME} 컨테이너를 실행합니다." + +docker compose pull $CONTAINER_NAME +docker compose up -d $CONTAINER_NAME + +for retry in {1..10} +do + RESPONSE=$(curl -s http://$IP:$GREEN_PORT/actuator/health) + GREEN_HEALTH=$(echo ${RESPONSE} | grep 'UP' | wc -l) + if [ $GREEN_HEALTH -eq 1 ] + then + break + else + echo -e "$IP:$GREEN_PORT 가 켜져있지 않습니다. 10초 슬립하고 다시 헬스체크를 수행합니다." + sleep 10 + fi +done + +if [ $GREEN_HEALTH -eq 0 ] +then + echo -e "$IP:$GREEN_PORT 가 작동하지 않습니다." + exit 0 +else + echo -e "$IP:$GREEN_PORT 가 정상적으로 실행 중입니다." +fi + +echo "set \$service_url $CONTAINER_NAME;" | sudo tee ./conf/service-url.inc +sudo docker exec nginx nginx -s reload + +echo "$STOP_CONTAINER_NAME 컨테이너 종료" +docker compose stop $STOP_CONTAINER_NAME +docker compose rm -f $STOP_CONTAINER_NAME \ No newline at end of file diff --git a/src/main/java/com/dnd/snappy/controller/v1/snap/SnapController.java b/src/main/java/com/dnd/snappy/controller/v1/snap/SnapController.java index b2a9d32..3e8a3b8 100644 --- a/src/main/java/com/dnd/snappy/controller/v1/snap/SnapController.java +++ b/src/main/java/com/dnd/snappy/controller/v1/snap/SnapController.java @@ -18,6 +18,7 @@ import jakarta.validation.Valid; import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; @@ -115,4 +116,5 @@ public ResponseEntity> findSnapById( var data = snapService.findSnapById(snapId); return ResponseDto.ok(data); } + } diff --git a/src/main/java/com/dnd/snappy/domain/snap/repository/SnapRepository.java b/src/main/java/com/dnd/snappy/domain/snap/repository/SnapRepository.java index a768b84..7b11b75 100644 --- a/src/main/java/com/dnd/snappy/domain/snap/repository/SnapRepository.java +++ b/src/main/java/com/dnd/snappy/domain/snap/repository/SnapRepository.java @@ -51,4 +51,5 @@ List findParticipantSnapsInMeetingByCursorId( """) Optional findSnapByIdWithParticipant(@Param("snapId") Long snapId); + }