|
1 | | -name: CI |
| 1 | +name: CD |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
5 | | - branches: [develop] |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - develop |
| 7 | + workflow_dispatch: |
6 | 8 |
|
7 | 9 | jobs: |
8 | | - build-and-push: |
| 10 | + deploy: |
9 | 11 | runs-on: ubuntu-latest |
10 | 12 |
|
11 | 13 | steps: |
12 | | - - name: Checkout code |
13 | | - uses: actions/checkout@v4 |
14 | | - |
15 | | - - name: Set up Docker Buildx |
16 | | - uses: docker/setup-buildx-action@v3 |
17 | | - |
18 | | - - name: Log in to Docker Hub |
19 | | - uses: docker/login-action@v3 |
20 | | - with: |
21 | | - username: ${{ secrets.DOCKER_HUB_USERNAME }} |
22 | | - password: ${{ secrets.DOCKER_HUB_TOKEN }} |
23 | | - |
24 | | - - name: Build and push Docker image |
25 | | - uses: docker/build-push-action@v5 |
26 | | - with: |
27 | | - context: . |
28 | | - file: ./Dockerfile |
29 | | - push: true |
30 | | - tags: ${{ secrets.DOCKER_HUB_USERNAME }}/commit-api:latest |
| 14 | + - name: Configure SSH |
| 15 | + run: | |
| 16 | + mkdir -p ~/.ssh |
| 17 | + echo "$EC2_SSH_KEY" > ~/.ssh/id_rsa |
| 18 | + chmod 600 ~/.ssh/id_rsa |
| 19 | + ssh-keyscan -H $EC2_HOST >> ~/.ssh/known_hosts |
| 20 | + cat >> ~/.ssh/config <<EOF |
| 21 | + Host ec2 |
| 22 | + HostName $EC2_HOST |
| 23 | + User ubuntu |
| 24 | + IdentityFile ~/.ssh/id_rsa |
| 25 | + StrictHostKeyChecking no |
| 26 | + EOF |
| 27 | + env: |
| 28 | + EC2_HOST: ${{ secrets.EC2_HOST }} |
| 29 | + EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} |
| 30 | + |
| 31 | + - name: Create .env on EC2 |
| 32 | + run: ssh ec2 "echo '$ENV_FILE' > /opt/app/.env" |
| 33 | + env: |
| 34 | + ENV_FILE: ${{ secrets.ENV_FILE }} |
| 35 | + |
| 36 | + - name: Deploy with Blue-Green |
| 37 | + run: | |
| 38 | + ssh ec2 "DOCKER_HUB_USERNAME=${{ secrets.DOCKER_HUB_USERNAME }} bash -s" <<'EOF' |
| 39 | +
|
| 40 | + echo "📦 Pulling latest image..." |
| 41 | + docker pull $DOCKER_HUB_USERNAME/commit-api:latest |
| 42 | +
|
| 43 | + echo "🔍 Checking current active environment..." |
| 44 | + if docker exec nginx-proxy test -f /etc/nginx/conf.d/default.conf; then |
| 45 | + CURRENT=$(docker exec nginx-proxy readlink -f /etc/nginx/conf.d/default.conf | xargs basename) |
| 46 | + else |
| 47 | + echo "❌ Cannot determine current config. Exiting." |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +
|
| 51 | + if echo "$CURRENT" | grep -q "blue"; then |
| 52 | + TARGET_COLOR=green |
| 53 | + TARGET_PORT=3001 |
| 54 | + TARGET_CONF=/etc/nginx/conf.d/default-green.conf.disabled |
| 55 | + else |
| 56 | + TARGET_COLOR=blue |
| 57 | + TARGET_PORT=3000 |
| 58 | + TARGET_CONF=/etc/nginx/conf.d/default-blue.conf.disabled |
| 59 | + fi |
| 60 | +
|
| 61 | + echo "🚀 Deploying to $TARGET_COLOR container on port $TARGET_PORT..." |
| 62 | + docker rm -f node-app-$TARGET_COLOR 2>/dev/null || true |
| 63 | +
|
| 64 | + docker run -d \ |
| 65 | + --name node-app-$TARGET_COLOR \ |
| 66 | + --env-file /opt/app/.env \ |
| 67 | + -p $TARGET_PORT:3000 \ |
| 68 | + --network=commit-networks \ |
| 69 | + -v /opt/app/config/service-account-key.json:/app/config/service-account-key.json:ro \ |
| 70 | + $DOCKER_HUB_USERNAME/commit-api:latest |
| 71 | +
|
| 72 | + echo "⏳ Health check for $TARGET_COLOR..." |
| 73 | + for i in {1..10}; do |
| 74 | + sleep 2 |
| 75 | + if curl -s http://localhost:$TARGET_PORT/health | grep "ok" > /dev/null; then |
| 76 | + echo "✅ Health check passed. Switching traffic..." |
| 77 | +
|
| 78 | + # Switch nginx config inside container |
| 79 | + docker exec nginx-proxy cp $TARGET_CONF /etc/nginx/conf.d/default.conf |
| 80 | + echo "📋 Switched nginx config to: $TARGET_CONF" |
| 81 | +
|
| 82 | + docker exec nginx-proxy nginx -s reload |
| 83 | + echo "🔄 Nginx reloaded" |
| 84 | +
|
| 85 | + # Remove previous container |
| 86 | + if [ "$TARGET_COLOR" = "blue" ]; then |
| 87 | + docker rm -f node-app-green || true |
| 88 | + else |
| 89 | + docker rm -f node-app-blue || true |
| 90 | + fi |
| 91 | +
|
| 92 | + exit 0 |
| 93 | + else |
| 94 | + echo "⚠️ Health check attempt $i failed." |
| 95 | + fi |
| 96 | + done |
| 97 | +
|
| 98 | + echo "❌ Health check failed. Rolling back..." |
| 99 | + docker rm -f node-app-$TARGET_COLOR || true |
| 100 | + exit 1 |
| 101 | +
|
| 102 | + EOF |
0 commit comments