|
| 1 | +name: Deploy to VPS (Docker) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + skip_tests: |
| 10 | + description: 'Skip tests' |
| 11 | + required: false |
| 12 | + default: 'false' |
| 13 | + |
| 14 | +env: |
| 15 | + VPS_HOST: 72.62.86.210 |
| 16 | + VPS_USER: root |
| 17 | + DEPLOY_PATH: /var/www/smarthaven |
| 18 | + |
| 19 | +jobs: |
| 20 | + test: |
| 21 | + name: Run Tests |
| 22 | + runs-on: ubuntu-latest |
| 23 | + if: github.event.inputs.skip_tests != 'true' |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Setup Node.js |
| 30 | + uses: actions/setup-node@v4 |
| 31 | + with: |
| 32 | + node-version: '20' |
| 33 | + |
| 34 | + - name: Install pnpm |
| 35 | + uses: pnpm/action-setup@v2 |
| 36 | + with: |
| 37 | + version: 8 |
| 38 | + |
| 39 | + - name: Install dependencies |
| 40 | + run: pnpm install |
| 41 | + |
| 42 | + - name: Run unit tests |
| 43 | + run: pnpm test |
| 44 | + |
| 45 | + deploy: |
| 46 | + name: Deploy to VPS |
| 47 | + needs: [test] |
| 48 | + if: always() && (needs.test.result == 'success' || github.event.inputs.skip_tests == 'true') |
| 49 | + runs-on: ubuntu-latest |
| 50 | + |
| 51 | + steps: |
| 52 | + - name: Checkout code |
| 53 | + uses: actions/checkout@v4 |
| 54 | + |
| 55 | + - name: Install SSH key |
| 56 | + uses: shimataro/ssh-key-action@v2 |
| 57 | + with: |
| 58 | + key: ${{ secrets.VPS_SSH_KEY }} |
| 59 | + known_hosts: ${{ secrets.VPS_KNOWN_HOSTS }} |
| 60 | + |
| 61 | + - name: Create deployment package |
| 62 | + run: | |
| 63 | + tar -czvf deploy-vps.tar.gz \ |
| 64 | + --exclude=node_modules \ |
| 65 | + --exclude=dist \ |
| 66 | + --exclude=.git \ |
| 67 | + --exclude=coverage \ |
| 68 | + --exclude=test-results \ |
| 69 | + --exclude=playwright-report \ |
| 70 | + --exclude='*.tar.gz' \ |
| 71 | + . |
| 72 | +
|
| 73 | + - name: Upload to VPS |
| 74 | + run: | |
| 75 | + scp deploy-vps.tar.gz ${{ env.VPS_USER }}@${{ env.VPS_HOST }}:/tmp/ |
| 76 | +
|
| 77 | + - name: Deploy on VPS |
| 78 | + run: | |
| 79 | + ssh ${{ env.VPS_USER }}@${{ env.VPS_HOST }} << 'ENDSSH' |
| 80 | + set -e |
| 81 | +
|
| 82 | + echo "📦 Extracting deployment package..." |
| 83 | + cd ${{ env.DEPLOY_PATH }} |
| 84 | +
|
| 85 | + # Backup current deployment |
| 86 | + if [ -d "src" ]; then |
| 87 | + echo "📁 Creating backup..." |
| 88 | + BACKUP_NAME="backup-$(date +%Y%m%d-%H%M%S).tar.gz" |
| 89 | + tar -czf /tmp/$BACKUP_NAME src/ docker-compose.yml nginx/ 2>/dev/null || true |
| 90 | + # Keep only last 5 backups |
| 91 | + ls -t /tmp/backup-*.tar.gz 2>/dev/null | tail -n +6 | xargs -r rm |
| 92 | + fi |
| 93 | +
|
| 94 | + # Extract new deployment |
| 95 | + echo "📂 Extracting new files..." |
| 96 | + tar -xzf /tmp/deploy-vps.tar.gz |
| 97 | + rm /tmp/deploy-vps.tar.gz |
| 98 | +
|
| 99 | + # Rebuild and restart containers |
| 100 | + echo "🐳 Rebuilding Docker containers..." |
| 101 | + docker compose down |
| 102 | + docker compose build --no-cache app |
| 103 | + docker compose up -d |
| 104 | +
|
| 105 | + # Wait for services to be healthy |
| 106 | + echo "⏳ Waiting for services to start..." |
| 107 | + sleep 15 |
| 108 | +
|
| 109 | + # Check if app is running |
| 110 | + if docker compose ps | grep -q "transcript-parser.*running"; then |
| 111 | + echo "✅ App container is running" |
| 112 | + else |
| 113 | + echo "❌ App container failed to start" |
| 114 | + docker compose logs app --tail=50 |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | +
|
| 118 | + # Reload nginx to pick up any config changes |
| 119 | + docker compose exec -T nginx nginx -s reload || true |
| 120 | +
|
| 121 | + echo "🎉 Deployment complete!" |
| 122 | + ENDSSH |
| 123 | +
|
| 124 | + - name: Verify deployment |
| 125 | + run: | |
| 126 | + echo "🔍 Verifying deployment..." |
| 127 | + sleep 5 |
| 128 | +
|
| 129 | + # Check if site is accessible |
| 130 | + HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://smarthavenai.com || echo "000") |
| 131 | +
|
| 132 | + if [ "$HTTP_STATUS" = "200" ]; then |
| 133 | + echo "✅ Site is accessible (HTTP $HTTP_STATUS)" |
| 134 | + else |
| 135 | + echo "⚠️ Site returned HTTP $HTTP_STATUS" |
| 136 | + fi |
| 137 | +
|
| 138 | + # Check CORS headers for FFmpeg |
| 139 | + echo "🔍 Checking FFmpeg CORS headers..." |
| 140 | + HEADERS=$(curl -sI https://smarthavenai.com | grep -i "cross-origin" || echo "none") |
| 141 | + echo "$HEADERS" |
| 142 | +
|
| 143 | + if echo "$HEADERS" | grep -q "cross-origin-opener-policy"; then |
| 144 | + echo "✅ FFmpeg headers are configured" |
| 145 | + else |
| 146 | + echo "⚠️ FFmpeg headers may be missing" |
| 147 | + fi |
| 148 | +
|
| 149 | + - name: Notify on failure |
| 150 | + if: failure() |
| 151 | + run: | |
| 152 | + echo "❌ Deployment failed! Check the logs above." |
0 commit comments