prisma: Github Action Workflow fails because of env("DATABASE_URL") in `schema.prisma` file.
Bug description
Upon pushing my project to Github with a set of github action workflows, the actions work well until reaching the job step of Run tests
which fails generating the following error:
UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error: error: Error validating datasource "db": You must provide a nonempty URL. The environment variable "DATABASE_URL" resolved to an empty string.
This occurs when the test hits the following line in the schema.prisma
file:
--> schema.prisma:6
|
5 | provider = "mongodb"
6 | url = env("DATABASE_URL")
|
Here is a screenshot of failed tests and error generated:
How to reproduce
- Node.js should be installed on your machine.
- Create a project directory
mkdir directory-name
and switch working directory to newly created directorycd directory-name
- Initialize a TypeScript project and add the Prisma CLI as a development dependency to it
npm init -y
npm install prisma typescript ts-node @types/node --save-dev
- Next, create a
tsconfig.json
file and add the following configuration to it:
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
- You can now invoke the Prisma CLI by prefixing it with npx:
npx prisma
- Next, set up your Prisma project by creating your Prisma schema file with the following command:
npx prisma init
- To connect your database, you need to set the url field of the datasource block in your Prisma schema to your database connection URL:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
In this case, the url is set via an environment variable which is defined in .env (the project uses a MongoDB Atlas URL):
DATABASE_URL="mongodb+srv://test:test@cluster0.ns1yp.mongodb.net/myFirstDatabase"
- Because MongoDB is currently a preview feature, you need to explicitly define that in the generator block.
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
}
No need to install the prisma client since we are not focusing on that. We will skip directly into setting up the github action workflows.
- Create a new
.github
folder in the project direcoty. Inside this folder, we create another folderworkflows
and add a.yml
file of any name (example:integration.yml
). We will setup our actions in this file as follows:
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
jobs:
build:
name: Build on node ${{ matrix.node-version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [16.x]
os: [ubuntu-latest, macOS-latest]
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 2
- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install --legacy-peer-deps
- name: Run tests
run: npm run test
# env:
# DATABASE_URL: ${{ secrets.DATABASE_URL }}
# Tried this too, but didn't work.
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v1
# upload to codecov after successful coverage tests.
# - name: Next build
# run: npm run build
# # next build causes error on windows os
- Save all files, create a new git repository & push the project a let the automated workflow integrations do the job.
Expected behavior
All tests should run without that bug occuring. Because when I enter directly the DATABASE_URL
value to the schema.prisma
file, the github action workflows builds successfully.
datasource db {
provider = "mongodb"
// url = env("DATABASE_URL")
url = "mongodb+srv://test:test@cluster0.ns1yp.mongodb.net/myFirstDatabase"
}
Here is a screenshot of a successful test & eventual github workflow successful build.
Prisma information
Environment & setup
- OS: ubuntu-latest, macOS-latest (Virtual Environments running Github Actions)
- Database: MongoDB
- Node.js version: 16.x
Prisma Version
Environment variables loaded from .env
prisma : 3.0.2
@prisma/client : 3.0.2
Current platform : darwin
Query Engine (Node-API) : libquery-engine 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 2452cc6313d52b8b9a96999ac0e974d0aedf88db
Studio : 0.423.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 21 (8 by maintainers)
Fixed it!
I just sorted this out here’s my solution.
settings/secrets/actions
DATABASE_URL
secret underRepository secrets
not Environment secrets.DATABASE_URL: ${{ secrets.DATABASE_URL }}
Resources
https://docs.github.com/en/actions/learn-github-actions/environment-variables https://github.com/gotpop/gotpop-starter/blob/master/.github/workflows/main.yml https://snyk.io/blog/how-to-use-github-actions-environment-variables
Good luck!
Works!
@janpio, it solved the issue ! Thanks ! I too am not quite certain how GH Actions use the “Environments”
Thanks again.
Can you try putting it as
Repository secrets
instead, the table below where it currently is defined? I am not sure how GH Actions interacts with the “Environments”.Here is the code triggering the error:
When the test coverage encounters the
It triggers the following clarified error:
And from the attempts I have described above, I have tried accessing the environmental variables through GH Actions & more attempts but still produces the error.
Same error is being output by Prisma
DEBUG=*
:And all Environment Variable values are entered accordingly in the
.env
&.env.local
files.