@@ -43,17 +43,34 @@ phases:
4343 # Build Next.js portal app
4444 - echo "Building Next.js portal application..."
4545 - cd apps/$APP_NAME
46+ - rm -rf .next/cache/
4647 - NODE_TLS_REJECT_UNAUTHORIZED=0 bun run build
4748
4849 # Upload Next.js chunks and CSS to S3 for CDN performance
4950 - echo "Uploading Next.js static assets to S3..."
5051 - |
5152 if [ -d ".next/static" ]; then
5253 echo "📦 Uploading .next/static/ files to CDN..."
54+ LOCAL_CHUNK_COUNT=$(find .next/static -name "*.js" | wc -l)
55+ echo "Local chunks: $LOCAL_CHUNK_COUNT"
5356 aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/portal/_next/static/ \
5457 --cache-control "public, max-age=31536000, immutable" \
5558 --exclude "*.map"
5659 echo "✅ Uploaded Next.js static assets to S3"
60+
61+ # Verify upload
62+ S3_CHUNK_COUNT=$(aws s3 ls s3://$STATIC_ASSETS_BUCKET/portal/_next/static/ --recursive | grep ".js$" | wc -l)
63+ echo "S3 chunks: $S3_CHUNK_COUNT"
64+ if [ "$S3_CHUNK_COUNT" -lt "$LOCAL_CHUNK_COUNT" ]; then
65+ echo "❌ S3 upload verification failed: Missing chunks"
66+ exit 1
67+ fi
68+
69+ # Invalidate CloudFront cache for static assets
70+ if [ -n "$CLOUDFRONT_DISTRIBUTION_ID" ]; then
71+ echo "Invalidating CloudFront cache..."
72+ aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION_ID --paths "/portal/_next/static/*"
73+ fi
5774 else
5875 echo "⚠️ No .next/static directory found"
5976 fi
@@ -88,14 +105,14 @@ phases:
88105 # Use the standalone build properly: copy from .next/standalone + app's own build
89106 - echo "DEBUG - Building container from standalone + app build..."
90107
91- # Copy the app's own built files (server.js, etc.)
108+ # Copy standalone build first (includes server actions)
109+ - echo "Copying standalone build..."
110+ - cp -r .next/standalone/* container-build/ || echo "Standalone copy failed"
111+
112+ # Then copy app's own .next build to ensure server actions are included
92113 - echo "Copying app's own .next build..."
93114 - cp -r .next container-build/ || echo "App .next copy failed"
94115
95- # Copy shared node_modules from standalone (minimal runtime deps)
96- - echo "Copying standalone node_modules (runtime dependencies)..."
97- - cp -r .next/standalone/node_modules container-build/ || echo "Standalone node_modules copy failed"
98-
99116 # Copy or create server.js for standalone
100117 - |
101118 if [ -f ".next/standalone/apps/$APP_NAME/server.js" ]; then
@@ -134,6 +151,19 @@ phases:
134151 - test -f container-build/server.js && echo "✅ Server.js exists" || echo "❌ Server.js missing"
135152 - test -d container-build/.next && echo "✅ .next directory exists" || echo "❌ .next directory missing"
136153 - test -d container-build/node_modules && echo "✅ node_modules exists" || echo "❌ node_modules missing"
154+
155+ # Critical: Verify static files and server files exist
156+ - |
157+ if [ ! -d "container-build/.next/static" ]; then
158+ echo "❌ Missing .next/static directory in container build"
159+ exit 1
160+ fi
161+ - |
162+ if [ ! -d "container-build/.next/server" ]; then
163+ echo "❌ Missing .next/server directory in container build"
164+ exit 1
165+ fi
166+ - echo "✅ Container build verification passed"
137167
138168 # Add Dockerfile to the standalone build
139169 - cp Dockerfile container-build/ || echo "No Dockerfile"
@@ -173,8 +203,13 @@ phases:
173203
174204 # Build Docker image (static assets now served from S3)
175205 - echo "Building Docker image..."
206+ - sleep 5
176207 - docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f container-build/Dockerfile -t $ECR_REPOSITORY_URI:$IMAGE_TAG container-build/
177208 - docker tag $ECR_REPOSITORY_URI:$IMAGE_TAG $ECR_REPOSITORY_URI:latest
209+
210+ # Test container startup
211+ - echo "Testing container startup..."
212+ - timeout 30 docker run --rm -e NODE_ENV=production $ECR_REPOSITORY_URI:$IMAGE_TAG node --version || echo "Container test completed"
178213
179214 post_build :
180215 commands :
@@ -190,7 +225,6 @@ cache:
190225 - ' node_modules/**/*'
191226 - ' packages/db/node_modules/**/*'
192227 - ' /root/.bun/install/cache/**/*'
193- - ' .next/cache/**/*'
194228 - ' bun.lock'
195229
196230artifacts :
0 commit comments