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 diff --git a/deployment/deploy.sh b/deployment/deploy.sh new file mode 100644 index 0000000..0e5933f --- /dev/null +++ b/deployment/deploy.sh @@ -0,0 +1,73 @@ +#!/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 compose pull ${ECR_URI}/dearbelly-cv:latest +docker compose up -d --no-deps --force-recreate app-${AFTER_COMPOSE_COLOR} + + +# 새 컨테이너가 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 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 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