@@ -38,26 +38,73 @@ jobs:
3838 # This step checks out your repository under $GITHUB_WORKSPACE so your job can access it.
3939 - name : Check out code
4040 uses : actions/checkout@v4
41+ with :
42+ # Only fetch the latest commit to minimize checkout time
43+ fetch-depth : 1
44+ # Preserve existing files for faster deploys
45+ clean : false
46+
47+ # Cache node_modules for faster installs
48+ - name : Cache node modules
49+ uses : actions/cache@v4
50+ with :
51+ path : |
52+ ~/.yarn/cache
53+ **/node_modules
54+ key : ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
55+ restore-keys : |
56+ ${{ runner.os }}-yarn-
57+
4158 # This step sets up Node.js on the runner and installs the version specified in the matrix above.
4259 - name : Set up Node.js
4360 uses : actions/setup-node@v4
4461 with :
4562 node-version : ${{ matrix.node-version }}
46- # This step installs project dependencies using Yarn.
47- # The --frozen-lockfile option ensures the exact package versions specified in yarn.lock are installed.
63+ cache : ' yarn'
64+
65+ # Install dependencies with optimizations
4866 - name : Install dependencies
49- run : yarn install --frozen-lockfile
67+ run : |
68+ # Use yarn with production optimizations
69+ yarn install --frozen-lockfile --production=false --cache-folder ~/.yarn/cache
70+ # Clean up dev dependencies in production
71+ if [ "$NODE_ENV" = "production" ]; then
72+ yarn install --frozen-lockfile --production=true --cache-folder ~/.yarn/cache
73+ fi
74+
5075 # This step copies the .env file from the root directory to the required directories for each package.
5176 # Update these paths if your repository structure is different.
5277 - name : Copy .env files
5378 run : |
5479 cp ../../../.env packages/webapp
5580 cp ../../../.env packages/hocuspocus.server
81+
82+ # Pre-build health check
83+ - name : Pre-build health check
84+ run : |
85+ # Check if essential services are running
86+ make supabase_status || echo "Supabase not running, continuing..."
87+
5688 - name : Build monorepo packages
57- run : yarn build
89+ run : |
90+ # Build with performance optimizations
91+ NODE_ENV=production yarn build
92+
93+ # Post-build validation
94+ - name : Validate build artifacts
95+ run : |
96+ # Check if critical build outputs exist
97+ if [ -d "packages/webapp/.next" ]; then
98+ echo "✓ Next.js build artifacts found"
99+ ls -la packages/webapp/.next/
100+ else
101+ echo "✗ Next.js build failed - no .next directory"
102+ exit 1
103+ fi
58104 env :
59105 # The environment variable DATABASE_URL is sourced from a secret in your repository.
60106 DATABASE_URL : ${{secrets.STAGE_DATABASE_URL}}
107+ NODE_ENV : production
61108
62109 # The "build-front" job builds the front-end, it depends on the "setup" job.
63110 build-front :
@@ -67,9 +114,61 @@ jobs:
67114 # This job will only run if the commit message contains the word 'front'.
68115 if : contains(github.event.head_commit.message, 'front')
69116 steps :
117+ # Pre-deployment health check
118+ - name : Pre-deployment health check
119+ run : |
120+ # Check current PM2 status
121+ pm2 list || echo "PM2 not running, will start fresh"
122+ # Check disk space
123+ df -h
124+ # Check memory usage
125+ free -h
126+
127+ # Backup current deployment for rollback
128+ - name : Backup current deployment
129+ run : |
130+ # Create backup of current .next build if it exists
131+ if [ -d "packages/webapp/.next" ]; then
132+ mv packages/webapp/.next packages/webapp/.next.backup.$(date +%Y%m%d-%H%M%S)
133+ echo "✓ Backup created"
134+ fi
135+
70136 # This step runs the build command for the front-end.
71- - name : Build Front-end
72- run : make build_front_production
137+ - name : Build and Deploy Front-end
138+ run : |
139+ # Build with enhanced error handling
140+ if ! make build_front_production; then
141+ echo "✗ Build failed, attempting rollback..."
142+ # Restore backup if build fails
143+ if [ -d "packages/webapp/.next.backup.*" ]; then
144+ LATEST_BACKUP=$(ls -t packages/webapp/.next.backup.* | head -n1)
145+ mv "$LATEST_BACKUP" packages/webapp/.next
146+ echo "✓ Rollback completed"
147+ fi
148+ exit 1
149+ fi
150+
151+ # Post-deployment health check
152+ - name : Post-deployment health check
153+ run : |
154+ # Wait for application to start
155+ sleep 10
156+
157+ # Check if the application is responding
158+ if curl -f http://localhost:3001/api/health; then
159+ echo "✓ Application health check passed"
160+ else
161+ echo "✗ Application health check failed"
162+ # Show PM2 logs for debugging
163+ pm2 logs nextjs_production --lines 50
164+ exit 1
165+ fi
166+
167+ # Cleanup old backups (keep last 3)
168+ - name : Cleanup old backups
169+ run : |
170+ # Remove old backup directories (keep last 3)
171+ ls -t packages/webapp/.next.backup.* 2>/dev/null | tail -n +4 | xargs rm -rf || true
73172
74173 # The "build-back" job builds the back-end, it also depends on the "setup" job.
75174 build-back :
0 commit comments