Create README.md #64
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..." | |
| ACTIVE_COLOR=$(docker exec nginx-proxy sh -c "grep -oP 'node-app-(blue|green)' /etc/nginx/conf.d/default.conf | head -1 | cut -d'-' -f3") | |
| if [ "$ACTIVE_COLOR" = "blue" ]; then | |
| TARGET_COLOR=green | |
| TARGET_PORT=3001 | |
| TARGET_CONF=/etc/nginx/conf.d/default-green.conf.disabled | |
| else | |
| TARGET_COLOR=blue | |
| TARGET_PORT=3000 | |
| TARGET_CONF=/etc/nginx/conf.d/default-blue.conf.disabled | |
| 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..." | |
| docker exec nginx-proxy cp $TARGET_CONF /etc/nginx/conf.d/default.conf | |
| echo "📋 Switched nginx config to: $TARGET_CONF" | |
| docker exec nginx-proxy nginx -s reload | |
| echo "🔄 Nginx 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 |