From 265f7a8a526bc2b7cc33abd4676a129be2c26ce2 Mon Sep 17 00:00:00 2001 From: bkw535 Date: Thu, 7 Aug 2025 03:53:46 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20nginx=20=EC=84=A4=EC=A0=95=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd.yml | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 590c201..224c5fe 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -35,20 +35,27 @@ jobs: - name: Deploy with Blue-Green run: | - ssh ec2 <<'EOF' + ssh ec2 "DOCKER_HUB_USERNAME=${{ secrets.DOCKER_HUB_USERNAME }} bash -s" <<'EOF' + echo "πŸ“¦ Pulling latest image..." - docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/commit-api:latest + docker pull $DOCKER_HUB_USERNAME/commit-api:latest echo "πŸ” Checking current active environment..." - CURRENT=$(readlink -f /opt/app/nginx/default.conf) - if echo "$CURRENT" | grep -q "default-blue.conf"; then + if docker exec nginx-proxy test -f /etc/nginx/conf.d/default.conf; then + CURRENT=$(docker exec nginx-proxy readlink -f /etc/nginx/conf.d/default.conf | xargs basename) + else + echo "❌ Cannot determine current config. Exiting." + exit 1 + fi + + if echo "$CURRENT" | grep -q "blue"; then TARGET_COLOR=green TARGET_PORT=3001 - TARGET_CONF=/opt/app/nginx/default-green.conf + TARGET_CONF=/etc/nginx/conf.d/default-green.conf.disabled else TARGET_COLOR=blue TARGET_PORT=3000 - TARGET_CONF=/opt/app/nginx/default-blue.conf + TARGET_CONF=/etc/nginx/conf.d/default-blue.conf.disabled fi echo "πŸš€ Deploying to $TARGET_COLOR container on port $TARGET_PORT..." @@ -60,7 +67,7 @@ jobs: -p $TARGET_PORT:3000 \ --network=commit-networks \ -v /opt/app/config/service-account-key.json:/app/config/service-account-key.json:ro \ - ${{ secrets.DOCKER_HUB_USERNAME }}/commit-api:latest + $DOCKER_HUB_USERNAME/commit-api:latest echo "⏳ Health check for $TARGET_COLOR..." for i in {1..10}; do @@ -68,19 +75,14 @@ jobs: if curl -s http://localhost:$TARGET_PORT/health | grep "ok" > /dev/null; then echo "βœ… Health check passed. Switching traffic..." - # 심볼릭 링크 λ³€κ²½ - ln -sf $TARGET_CONF /opt/app/nginx/default.conf - echo "πŸ” Linked $TARGET_CONF to default.conf" - - # ν˜„μž¬ 심볼릭 링크가 μ‹€μ œλ‘œ κ°€λ¦¬ν‚€λŠ” 파일 확인 - ACTUAL_LINK=$(readlink -f /opt/app/nginx/default.conf) - echo "πŸ“Œ Current nginx config link points to: $ACTUAL_LINK" + # Switch nginx config inside container + docker exec nginx-proxy cp $TARGET_CONF /etc/nginx/conf.d/default.conf + echo "πŸ“‹ Switched nginx config to: $TARGET_CONF" - # Nginx reload docker exec nginx-proxy nginx -s reload - echo "πŸ”„ Nginx config reloaded" + echo "πŸ”„ Nginx reloaded" - # 이전 μ»¨ν…Œμ΄λ„ˆ 제거 + # Remove previous container if [ "$TARGET_COLOR" = "blue" ]; then docker rm -f node-app-green || true else @@ -96,4 +98,5 @@ jobs: echo "❌ Health check failed. Rolling back..." docker rm -f node-app-$TARGET_COLOR || true exit 1 + EOF \ No newline at end of file