Merge pull request #140 from umc-commit/bug/139-nginx-conf #29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CD | |
| on: | |
| push: | |
| branches: | |
| - develop | |
| workflow_dispatch: | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Configure SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "$EC2_SSH_KEY" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| ssh-keyscan -H $EC2_HOST >> ~/.ssh/known_hosts | |
| cat >> ~/.ssh/config <<EOF | |
| Host ec2 | |
| HostName $EC2_HOST | |
| User ubuntu | |
| IdentityFile ~/.ssh/id_rsa | |
| StrictHostKeyChecking no | |
| EOF | |
| env: | |
| EC2_HOST: ${{ secrets.EC2_HOST }} | |
| EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} | |
| - name: Create .env on EC2 | |
| run: ssh ec2 "echo '$ENV_FILE' > /opt/app/.env" | |
| env: | |
| ENV_FILE: ${{ secrets.ENV_FILE }} | |
| - name: Deploy with Blue-Green | |
| run: | | |
| ssh ec2 <<'EOF' | |
| echo "📦 Pulling latest image..." | |
| docker pull ${{ secrets.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 | |
| TARGET_COLOR=green | |
| TARGET_PORT=3001 | |
| TARGET_CONF=/opt/app/nginx/default-green.conf | |
| else | |
| TARGET_COLOR=blue | |
| TARGET_PORT=3000 | |
| TARGET_CONF=/opt/app/nginx/default-blue.conf | |
| fi | |
| echo "🚀 Deploying to $TARGET_COLOR container on port $TARGET_PORT..." | |
| docker rm -f node-app-$TARGET_COLOR 2>/dev/null || true | |
| docker run -d \ | |
| --name node-app-$TARGET_COLOR \ | |
| --env-file /opt/app/.env \ | |
| -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 | |
| echo "⏳ Health check for $TARGET_COLOR..." | |
| for i in {1..10}; do | |
| sleep 2 | |
| 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" | |
| # Nginx reload | |
| docker exec nginx-proxy nginx -s reload | |
| echo "🔄 Nginx config reloaded" | |
| # 이전 컨테이너 제거 | |
| if [ "$TARGET_COLOR" = "blue" ]; then | |
| docker rm -f node-app-green || true | |
| else | |
| docker rm -f node-app-blue || true | |
| fi | |
| exit 0 | |
| else | |
| echo "⚠️ Health check attempt $i failed." | |
| fi | |
| done | |
| echo "❌ Health check failed. Rolling back..." | |
| docker rm -f node-app-$TARGET_COLOR || true | |
| exit 1 | |
| EOF |