|
| 1 | +name: Deploy to Staging |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: # Allow manual deployment |
| 8 | + |
| 9 | +jobs: |
| 10 | + build-and-deploy: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + env: |
| 14 | + NODE_VERSION: "20.6.0" |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout code |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Setup Node.js |
| 21 | + uses: actions/setup-node@v4 |
| 22 | + with: |
| 23 | + node-version: ${{ env.NODE_VERSION }} |
| 24 | + cache: "npm" |
| 25 | + cache-dependency-path: | |
| 26 | + server/package-lock.json |
| 27 | + client/package-lock.json |
| 28 | +
|
| 29 | + - name: Install server dependencies |
| 30 | + run: | |
| 31 | + cd server |
| 32 | + npm ci |
| 33 | +
|
| 34 | + - name: Install client dependencies |
| 35 | + run: | |
| 36 | + cd client |
| 37 | + npm ci |
| 38 | +
|
| 39 | + - name: Validate build (optional) |
| 40 | + run: | |
| 41 | + cd client |
| 42 | + npm run build:staging |
| 43 | + echo "✅ Client build validation completed" |
| 44 | +
|
| 45 | + - name: Deploy to Lightsail instance |
| 46 | + run: | |
| 47 | + # Install SSH key |
| 48 | + mkdir -p ~/.ssh |
| 49 | + echo "${{ secrets.LIGHTSAIL_SSH_KEY }}" > ~/.ssh/lightsail_key |
| 50 | + chmod 600 ~/.ssh/lightsail_key |
| 51 | +
|
| 52 | + # Deploy using git pull and rebuild |
| 53 | + ssh -i ~/.ssh/lightsail_key -o StrictHostKeyChecking=no \ |
| 54 | + bitnami@${{ vars.LIGHTSAIL_HOST }} << 'ENDSSH' |
| 55 | + |
| 56 | + set -e # Exit on any error |
| 57 | + |
| 58 | + echo "🚀 Starting deployment..." |
| 59 | + |
| 60 | + cd ~/bolt-gameserver-sample |
| 61 | + |
| 62 | + echo "🔄 Fetching latest code..." |
| 63 | + git fetch origin |
| 64 | + git reset --hard origin/${{ github.ref_name }} |
| 65 | + |
| 66 | + # Install server dependencies |
| 67 | + echo "📦 Installing server dependencies..." |
| 68 | + cd server |
| 69 | + npm ci |
| 70 | + |
| 71 | + # Build client |
| 72 | + echo "🏗️ Building client..." |
| 73 | + cd ../client |
| 74 | + npm ci |
| 75 | + npm run build:staging |
| 76 | +
|
| 77 | + echo "▶️ Starting client..." |
| 78 | + sudo /opt/bitnami/ctlscript.sh restart nginx |
| 79 | +
|
| 80 | + echo "▶️ Starting nodejs service..." |
| 81 | + sudo systemctl restart nodejs.service |
| 82 | + |
| 83 | + echo "✅ Service start command executed" |
| 84 | + |
| 85 | + echo "🎉 Deployment completed successfully!" |
| 86 | + ENDSSH |
| 87 | +
|
| 88 | + - name: Health check |
| 89 | + run: | |
| 90 | + echo "Performing health checks..." |
| 91 | +
|
| 92 | + # Wait for service to be ready |
| 93 | + sleep 15 |
| 94 | +
|
| 95 | + # Check health endpoint |
| 96 | + ENDPOINT="${{ vars.HOST_STAGING }}/api/health" |
| 97 | +
|
| 98 | + echo "Checking $ENDPOINT..." |
| 99 | + if curl -f -s --max-time 10 "$ENDPOINT" > /dev/null; then |
| 100 | + echo "✅ Health check passed for $ENDPOINT" |
| 101 | + SUCCESS=true |
| 102 | + else |
| 103 | + echo "❌ Health check failed for $ENDPOINT" |
| 104 | + SUCCESS=false |
| 105 | + fi |
| 106 | +
|
| 107 | + if [ "$SUCCESS" = false ]; then |
| 108 | + echo "❌ Health check failed" |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | +
|
| 112 | + - name: Notify deployment status |
| 113 | + if: always() |
| 114 | + run: | |
| 115 | + if [ ${{ job.status }} == 'success' ]; then |
| 116 | + echo "✅ Staging deployment successful!" |
| 117 | + else |
| 118 | + echo "❌ Staging deployment failed!" |
| 119 | + fi |
0 commit comments