From eb4933649dd61f82fa3b1b3dd123b5435659efa5 Mon Sep 17 00:00:00 2001 From: kite_U Date: Sun, 31 Aug 2025 02:34:14 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20?= =?UTF-8?q?=EB=B0=B0=ED=8F=AC=20=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=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 --- deployment/deploy.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 deployment/deploy.sh diff --git a/deployment/deploy.sh b/deployment/deploy.sh new file mode 100644 index 0000000..39c4ac0 --- /dev/null +++ b/deployment/deploy.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +set -e + +ERR_MSG='' + +trap 'echo "Error occured: $ERR_MSG. Exiting deploy script."; exit 1' ERR + + +# 현재 포트 파악 +if sudo docker ps --filter "name=app-blue" --quiet | grep -E .; then + echo "Blue down, Green Up " + BEFORE_COMPOSE_COLOR="blue" + AFTER_COMPOSE_COLOR="green" + HOST_PORT="8001" +else + echo "Green down, Blue up" + BEFORE_COMPOSE_COLOR="green" + AFTER_COMPOSE_COLOR="blue" + HOST_PORT="8000" +fi + +echo "Pulling image: ${IMAGE}" +# docker pull +docker pull ${ECR_URI}/dearbelly-cv:latest + +# 새 컨테이너 실행 +docker run --gpus all -d \ + --name "app-${AFTER_COLOR}" \ + --env-file ./.env \ + -p "${HOST_PORT}:8000" \ + ${ECR_URI}/dearbelly-cv:latest + +# 새 컨테이너가 running 될 때까지 대기 +for i in $(seq 1 60); do + if docker ps --filter "name=^/app-${AFTER_COLOR}$" --filter "status=running" --format '{{.Names}}' | grep -q .; then + echo "New app-${AFTER_COLOR} container is running." + break + fi + sleep 1 + if [ "$i" -eq 60 ]; then + echo "New container failed to start in time." >&2 + exit 1 + fi +done + +# 새로운 컨테이너 확인 후 Nginx 설정 변경 +if docker ps --filter "name=app-${AFTER_COMPOSE_COLOR}" --filter "status=running" --format '{{.Names}}' | grep -q .; then + echo "New app-${AFTER_COMPOSE_COLOR} container is running." + # reload nginx + NGINX_ID=$(sudo docker ps --filter "name=nginx" --quiet) + NGINX_CONFIG="/home/ubuntu/deployment/nginx.conf" + + echo "Switching Nginx upstream config..." + if ! sed -i "s/app-${BEFORE_COMPOSE_COLOR}:8000/app-${AFTER_COMPOSE_COLOR}:8000/" $NGINX_CONFIG; then + echo "Error occured: Failed to update Nginx config. Exiting deploy script." + exit 1 + fi + + echo "Reloding Nginx in Container" + if ! docker exec $NGINX_ID nginx -s reload; then + ERR_MSG='Failed to update Nginx config' + exit 1 + fi + + if ! docker compose restart nginx; then + ERR_MSG='Failed to reload Nginx' + exit 1 + fi + + # 이전 컨테이너 종료 + docker stop app-${BEFORE_COMPOSE_COLOR} + docker rm app-${BEFORE_COMPOSE_COLOR} + docker image prune -af +fi + +echo "Deployment success." +exit 0 \ No newline at end of file From 999c6ce12c481670f045b62e0247e6579c29ea2d Mon Sep 17 00:00:00 2001 From: kite_U Date: Sun, 31 Aug 2025 02:34:26 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=ED=8F=AC=ED=8A=B8=20=EB=A7=A4?= =?UTF-8?q?=ED=95=91=EC=9D=84=20=EC=9C=84=ED=95=9C=20Nginx=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deployment/nginx.conf | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 deployment/nginx.conf diff --git a/deployment/nginx.conf b/deployment/nginx.conf new file mode 100644 index 0000000..a4bff5b --- /dev/null +++ b/deployment/nginx.conf @@ -0,0 +1,21 @@ +events {} + +http { + upstream backend { + server app-blue:8000; + } + + server { + listen 80; + + location / { + proxy_pass http://backend/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + } + } +} \ No newline at end of file From 494d3c142b67d3483b61e76f1ddeacf39d6203ec Mon Sep 17 00:00:00 2001 From: kite_U Date: Sun, 31 Aug 2025 02:34:38 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=EC=8A=A4=ED=81=AC=EB=A6=BD?= =?UTF-8?q?=ED=8A=B8=20=EC=8B=A4=ED=96=89=EC=9C=BC=EB=A1=9C=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/workflow.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 09809c7..fbabb99 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -37,6 +37,14 @@ jobs: - name: Checkout Code uses: actions/checkout@v4 + - name: Transfer deploy file to EC2 + uses: appleboy/scp-action@master + with: + host: ${{ secrets.REMOTE_HOST }} + username: ${{ secrets.REMOTE_USER }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + source: ./deployment/ + target: /home/ubuntu/deployment deploy: name: Deploy @@ -52,8 +60,8 @@ jobs: port: 22 script: | # 1. cd - mkdir -p ~/dearbelly - cd ~/dearbelly + mkdir -p ~/dearbelly/deployment + cd ~/dearbelly/deployment # 2. .env file echo "${{ secrets.ENV }}" > .env @@ -69,5 +77,5 @@ jobs: docker pull ${{ secrets.ECR_URI }}/dearbelly-cv:latest # 6. docker start - # TODO : 스크립트 작성 - docker run -d --name app-blue -p 8000:8000 ${{ secrets.ECR_URI }}/dearbelly-cv:latest \ No newline at end of file + chmod +x deploy.sh + source deploy.sh \ No newline at end of file From b5a123657f997f363a439fca1db9e0e4523ba70c Mon Sep 17 00:00:00 2001 From: kite_U Date: Tue, 2 Sep 2025 00:14:41 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20docker-compose.yml=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 --- deployment/docker-compose.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 deployment/docker-compose.yml diff --git a/deployment/docker-compose.yml b/deployment/docker-compose.yml new file mode 100644 index 0000000..4d256c2 --- /dev/null +++ b/deployment/docker-compose.yml @@ -0,0 +1,30 @@ +services: + app-blue: + image: ${ECR_URI}/dearbelly-dv:latest + ports: + - "8000:8000" + env_file: + - /home/ubuntu/dearbelly/deployment/.env + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: 1 + capabilities: [gpu] + container_name: app-blue + + app-green: + image: ${ECR_URI}/dearbelly-dv:latest + ports: + - "8001:8000" + env_file: + - /home/ubuntu/dearbelly/deployment/.env + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: 1 + capabilities: [ gpu ] + container_name: app-green \ No newline at end of file From 71b6f134bc0d7158223d59e8327cb59715e7424c Mon Sep 17 00:00:00 2001 From: kite_U Date: Tue, 2 Sep 2025 00:17:31 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20docker-compose.yml=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 --- deployment/deploy.sh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/deployment/deploy.sh b/deployment/deploy.sh index 39c4ac0..0e5933f 100644 --- a/deployment/deploy.sh +++ b/deployment/deploy.sh @@ -22,14 +22,9 @@ fi echo "Pulling image: ${IMAGE}" # docker pull -docker pull ${ECR_URI}/dearbelly-cv:latest +docker compose pull ${ECR_URI}/dearbelly-cv:latest +docker compose up -d --no-deps --force-recreate app-${AFTER_COMPOSE_COLOR} -# 새 컨테이너 실행 -docker run --gpus all -d \ - --name "app-${AFTER_COLOR}" \ - --env-file ./.env \ - -p "${HOST_PORT}:8000" \ - ${ECR_URI}/dearbelly-cv:latest # 새 컨테이너가 running 될 때까지 대기 for i in $(seq 1 60); do