diff --git a/Dockerfile b/Dockerfile index 0b1ea9c..aa58ad4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,10 +34,6 @@ COPY . . RUN cd packages/database && npx prisma generate RUN pnpm turbo run build --filter=@startover/${APP_NAME} -# Copy Prisma schema and engine for runtime migration -RUN mkdir -p /app/prisma -RUN cp packages/database/prisma/schema.prisma /app/prisma/schema.prisma - # Ensure public directories exist for COPY RUN mkdir -p /app/apps/${APP_NAME}/public @@ -54,7 +50,6 @@ WORKDIR /app COPY --from=builder /app/apps/${APP_NAME}/public ./public COPY --from=builder --chown=nextjs:nodejs /app/apps/${APP_NAME}/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/apps/${APP_NAME}/.next/static ./apps/${APP_NAME}/.next/static -COPY --from=builder /app/prisma/schema.prisma ./prisma/schema.prisma USER nextjs EXPOSE 3000 diff --git a/deploy/deploy.sh b/deploy/deploy.sh index b6f21f3..88ef32e 100644 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -51,22 +51,24 @@ if [ -f "$APP_DIR/.env.production" ]; then cp "$APP_DIR/.env.production" "$APP_DIR/repo/.env.production" fi -# Build and deploy -log "Building and deploying with Docker Compose..." -docker compose -f docker-compose.prod.yml --env-file .env.production up -d --build --remove-orphans 2>&1 | tee -a "$LOG_FILE" - -# Wait for postgres to be healthy +# Step 1: Start postgres and wait for healthy +log "Starting postgres..." +docker compose -f docker-compose.prod.yml --env-file .env.production up -d postgres 2>&1 | tee -a "$LOG_FILE" log "Waiting for postgres to be healthy..." sleep 10 -# Apply database schema +# Step 2: Run database migration (uses builder stage with prisma CLI) log "Applying database schema (prisma db push)..." -docker compose -f docker-compose.prod.yml --env-file .env.production exec -T web npx prisma db push --schema=./prisma/schema.prisma --skip-generate --accept-data-loss 2>&1 | tee -a "$LOG_FILE" +docker compose -f docker-compose.prod.yml --env-file .env.production run --rm migrate 2>&1 | tee -a "$LOG_FILE" log "Database schema applied." +# Step 3: Build and deploy all services +log "Building and deploying all services..." +docker compose -f docker-compose.prod.yml --env-file .env.production up -d --build --remove-orphans 2>&1 | tee -a "$LOG_FILE" + # Wait for services to stabilize log "Waiting for services to stabilize..." -sleep 5 +sleep 10 # Health check if curl -sf http://localhost/health > /dev/null 2>&1; then diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 6f81ac6..ff9041d 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -47,6 +47,18 @@ services: - web - admin + migrate: + build: + context: . + dockerfile: Dockerfile + target: builder + environment: + DATABASE_URL: postgresql://${DB_USER:-startover}:${DB_PASSWORD}@postgres:5432/${DB_NAME:-startover_prod}?schema=public + command: sh -c "cd packages/database && npx prisma db push --skip-generate --accept-data-loss" + depends_on: + postgres: + condition: service_healthy + web: build: context: .