1# JOB 2: DEPLOY TO PRODUCTION
2deploy:
3 needs: tests
4 runs-on: ubuntu-latest
5 if: github.ref == 'refs/heads/main'
6
7 steps:
8 - name: Deploy via SSH
9 uses: appleboy/ssh-action@master
10 with:
11 host: ${{ secrets.SSH_HOST }}
12 username: ${{ secrets.SSH_USERNAME }}
13 key: ${{ secrets.SSH_PRIVATE_KEY }}
14 script: |
15 # 1. Define Variables
16 RELEASE_DATE=$(date +%Y%m%d%H%M%S)
17 BASE_DIR="/var/www/beartropy-app"
18 NEW_RELEASE_DIR="$BASE_DIR/releases/$RELEASE_DATE"
19
20 # 2. Clone Repository
21 echo "🚀 Cloning repository..."
22 git clone --depth 1 -b main git@github.com:beartropy/app.git $NEW_RELEASE_DIR
23
24 # 3. Link Storage & Env
25 echo "🔗 Linking shared assets..."
26 ln -nfs $BASE_DIR/.env $NEW_RELEASE_DIR/.env
27 rm -rf $NEW_RELEASE_DIR/storage
28 ln -nfs $BASE_DIR/storage $NEW_RELEASE_DIR/storage
29
30 # 4. Install Backend Dependencies
31 echo "📦 Installing Composer dependencies..."
32 cd $NEW_RELEASE_DIR
33 composer install --no-dev --optimize-autoloader
34
35 # 5. Build Frontend Assets
36 echo "🎨 Building Vite assets..."
37 npm ci
38 npm run build
39 # Cleanup node_modules to save space
40 rm -rf node_modules
41
42 # 6. Database Migrations (Force is safe here due to atomic structure)
43 echo "🗄️ Running Migrations..."
44 php artisan migrate --force
45
46 # 7. Atomic Swap (The Magic Moment)
47 echo "🔄 Swapping symlinks..."
48 ln -nfs $NEW_RELEASE_DIR $BASE_DIR/current
49
50 # 8. Reset Caches & Queues
51 echo "🧹 Clearing caches..."
52 php artisan config:cache
53 php artisan event:cache
54 php artisan route:cache
55 php artisan view:cache
56 php artisan queue:restart
57
58 # 9. Cleanup Old Releases (Keep last 5)
59 echo "🗑️ Cleaning up old releases..."
60 cd $BASE_DIR/releases
61 ls -t | tail -n +6 | xargs rm -rf
62
63 echo "✅ Deployment Finished!"