Missing required environment variable: DATABASE_URL in GitHub CI for generate
Locally, I can build my docker image fine. But in the GitHub CI pipeline, I run into the error in the title.
However, this is a DOCKER image, I don't want to hard-code the DATABASE_URL.
Shouldn't it be grabbing it from the environment variables? Why does it need the database_url for
npx prisma generate?4 Replies
Skipping coffee for instant knowledge! Let's brew that AI answer now.
The error "Missing required environment variable: DATABASE_URL" during GitHub CI for generate is a common issue with Prisma, especially when using Docker or CI/CD pipelines.
Key points from the knowledge sources:
- By default, Prisma expects the
DATABASE_URL environment variable to be set when running commands like prisma generate or prisma migrate. If it's missing, you'll get the error you described.
- Locally, this often works because you have a .env file or your shell exports the variable. In CI or Docker, the environment may not be set up the same way, so Prisma can't find DATABASE_URL and fails.
- When using a prisma.config.ts file, Prisma does not automatically load .env files. You must explicitly load them (e.g., with import 'dotenv/config'), but this only works if the .env file is present and accessible in the CI environment. If the variable is not set in the environment or the .env file is missing, you'll get this error. See source
- In CI, you should set the DATABASE_URL as an environment variable in your workflow configuration or as a secret. For GitHub Actions, use repository secrets and reference them in your workflow YAML. See source
- For Docker builds, you can set a dummy value for DATABASE_URL during the build stage if you don't want to connect to a real database at build time. This is a common workaround to allow prisma generate to succeed. See source
Summary of what you should do:
- In your GitHub Actions workflow, add a step to set DATABASE_URL (even a dummy value) before running npx prisma generate.
- If you want to avoid hardcoding, use GitHub secrets for the real value, or set a placeholder if the real value isn't needed at build time.
Example for GitHub Actions:
This ensures Prisma can find the variable and won't fail during generate, even in CI or Docker builds.Does this mean I can still use my own url later?
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
To confirm, did you had DATABASE_URL set as a GitHub Actions secret/environment variable?