name: Deploy to Production on: push: branches: - main # triggers automatically on pushes to main jobs: deploy: name: Deploy TurboTrades runs-on: ubuntu-latest steps: # ------------------------- # DEBUG: Verify workflow trigger & secrets # ------------------------- - name: Debug Info run: | echo "🟢 Workflow triggered!" echo "Branch: $GITHUB_REF" echo "Commit: $GITHUB_SHA" echo "Actor: $GITHUB_ACTOR" echo "Repository: $GITHUB_REPOSITORY" echo "Server host secret set? $(if [ -z '${{ secrets.SERVER_HOST }}' ]; then echo '❌'; else echo '✅'; fi)" echo "Server user secret set? $(if [ -z '${{ secrets.SERVER_USER }}' ]; then echo '❌'; else echo '✅'; fi)" echo "Server password secret set? $(if [ -z '${{ secrets.SERVER_PASSWORD }}' ]; then echo '❌'; else echo '✅'; fi)" echo "Runner OS: $RUNNER_OS" # ------------------------- # Checkout code # ------------------------- - name: Checkout code uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: "20" cache: "npm" - name: Install Backend Dependencies run: npm ci --production - name: Install Frontend Dependencies run: | cd frontend npm ci - name: Build Frontend run: | cd frontend npm run build env: NODE_ENV: production VITE_API_URL: https://api.turbotrades.dev VITE_WS_URL: https://ws.turbotrades.dev # ------------------------- # Deploy via SSH # ------------------------- - name: Deploy via SSH (Gitea + password) uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SERVER_HOST }} username: ${{ secrets.SERVER_USER }} password: ${{ secrets.SERVER_PASSWORD }} port: ${{ secrets.SERVER_PORT || 22 }} script: | echo "🚀 Starting deployment..." mkdir -p /root/ttbackend /root/ttbackend/logs /var/www/html/turbotrades cd /root/ttbackend pm2 stop turbotrades-backend || echo "Backend not running" pm2 delete turbotrades-backend || true if [ -d "/root/ttbackend-backup" ]; then rm -rf /root/ttbackend-backup-old mv /root/ttbackend-backup /root/ttbackend-backup-old fi mkdir -p /root/ttbackend-backup rsync -a --delete /root/ttbackend/ /root/ttbackend-backup/ || true if [ -d ".git" ]; then git fetch origin git reset --hard origin/main git clean -fd else cd /root rm -rf ttbackend git clone https://username:${{ secrets.SERVER_PASSWORD }}@git.turbotrades.dev/iDefineHD/TurboTrades.git ttbackend cd ttbackend fi npm ci --production cd frontend npm ci npm run build rm -rf /var/www/html/turbotrades/* cp -r dist/* /var/www/html/turbotrades/ chown -R www-data:www-data /var/www/html/turbotrades chmod -R 755 /var/www/html/turbotrades cd /root/ttbackend pm2 start ecosystem.config.js --env production pm2 save echo "✅ Deployment complete!" pm2 list # ------------------------- # Health Check # ------------------------- - name: Health Check run: | sleep 15 curl -f https://api.turbotrades.dev/api/health || echo "⚠️ Backend health check failed" curl -f https://turbotrades.dev || echo "⚠️ Frontend check failed" # ------------------------- # Notifications # ------------------------- - name: Notify Success if: success() run: | echo "✅ Deployment successful!" echo "🌐 Frontend: https://turbotrades.dev" echo "🔧 Backend: https://api.turbotrades.dev" echo "💬 WebSocket: https://ws.turbotrades.dev" echo "📅 Time: $(date)" - name: Notify Failure if: failure() run: | echo "❌ Deployment failed! Check logs above." - name: Rollback on Failure if: failure() uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SERVER_HOST }} username: ${{ secrets.SERVER_USER }} password: ${{ secrets.SERVER_PASSWORD }} port: ${{ secrets.SERVER_PORT || 22 }} script: | echo "🔄 Rolling back..." if [ -d "/root/ttbackend-backup" ]; then rsync -a --delete /root/ttbackend-backup/ /root/ttbackend/ cd /root/ttbackend pm2 start ecosystem.config.js --env production pm2 save echo "✅ Rollback complete" else echo "❌ No backup found!" fi