diff --git a/.github/workflows/image.yaml b/.github/workflows/image.yaml index 6173482..ed57a2a 100644 --- a/.github/workflows/image.yaml +++ b/.github/workflows/image.yaml @@ -71,6 +71,8 @@ jobs: IMAGE="dispatch:prisma-cli-runtime" docker build -t "$IMAGE" . echo "Validating Prisma CLI in image: $IMAGE" + # Pass DATABASE_URL at runtime so prisma validate can load prisma.config.ts. + # The builder stage ENV is only for npm run build (Next.js static generation). docker run --rm --env DATABASE_URL=postgresql://localhost:5432/dispatch --entrypoint ./node_modules/.bin/prisma "$IMAGE" --version docker run --rm --env DATABASE_URL=postgresql://localhost:5432/dispatch --entrypoint ./node_modules/.bin/prisma "$IMAGE" validate @@ -89,4 +91,4 @@ jobs: if: github.event_name != 'pull_request' && hashFiles('trivy-results.sarif') != '' uses: github/codeql-action/upload-sarif@7211b7c8077ea37d8641b6271f6a365a22a5fbfa # v4 with: - sarif_file: trivy-results.sarif + sarif_file: trivy-results.sarif \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 0a6711c..a116b12 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,10 @@ COPY --from=deps /app/node_modules ./node_modules COPY . . RUN apt-get update && apt-get install -y --no-install-recommends openssl ca-certificates && rm -rf /var/lib/apt/lists/* RUN npx prisma generate +# DATABASE_URL is needed at build time for Next.js static generation (npm run build). +# The runner stage does NOT inherit this ENV; production code requires DATABASE_URL +# at runtime via the check in src/lib/prisma.ts. +ENV DATABASE_URL=postgresql://localhost:5432/dispatch RUN npm run build FROM base AS runner diff --git a/prisma.config.ts b/prisma.config.ts index 4223616..c2ae0c0 100644 --- a/prisma.config.ts +++ b/prisma.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from "prisma/config"; const databaseUrl = process.env.DATABASE_URL; -if (!databaseUrl) { +if (process.env.NODE_ENV === "production" && !databaseUrl) { throw new Error( "DATABASE_URL is not set. Please set the DATABASE_URL environment variable before starting the application.", ); @@ -11,6 +11,9 @@ if (!databaseUrl) { export default defineConfig({ schema: "prisma/schema.prisma", datasource: { - url: databaseUrl, + // Fallback to localhost for dev/CI environments where no real DB is needed + // (e.g., prisma generate, prisma validate with --no-engine). Production + // requires DATABASE_URL via the check above. + url: databaseUrl ?? "postgresql://localhost:5432/dispatch", }, }); diff --git a/src/lib/prisma.ts b/src/lib/prisma.ts index 48460a4..89a85e9 100644 --- a/src/lib/prisma.ts +++ b/src/lib/prisma.ts @@ -3,15 +3,13 @@ import { PrismaPg } from "@prisma/adapter-pg"; import { PrFixQueueClient } from "@/lib/pr-fix-queue"; import { AgentWorkClient } from "@/lib/agent-work"; -const databaseUrl = process.env.DATABASE_URL; - -if (!databaseUrl) { +if (process.env.NODE_ENV === "production" && !process.env.DATABASE_URL) { throw new Error( "DATABASE_URL is not set. Please set the DATABASE_URL environment variable before starting the application.", ); } -const adapter = new PrismaPg(databaseUrl); +const adapter = new PrismaPg(process.env.DATABASE_URL!); const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined;