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: Screen Shot 2021-11-11 at 8 16 25 AM

How to reproduce

  1. Node.js should be installed on your machine.
  2. Create a project directory mkdir directory-name and switch working directory to newly created directory cd directory-name
  3. 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
  1. Next, create a tsconfig.json file and add the following configuration to it:
{
  "compilerOptions": {
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "lib": ["esnext"],
    "esModuleInterop": true
  }
}
  1. You can now invoke the Prisma CLI by prefixing it with npx:
npx prisma
  1. Next, set up your Prisma project by creating your Prisma schema file with the following command:
npx prisma init
  1. 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"
  1. 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.

  1. Create a new .github folder in the project direcoty. Inside this folder, we create another folder workflows 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
  1. 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. Screen Shot 2021-11-11 at 8 28 55 AM

Prisma information

Screen Shot 2021-11-11 at 7 55 51 AM

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)

Most upvoted comments

Fixed it!

I just sorted this out here’s my solution.

  1. go to settings/secrets/actions
  2. Add your DATABASE_URL secret under Repository secrets not Environment secrets.
  3. Ensure you don’t use quotation marks
  4. In your GitHub workflow file import the environment variable in the build step not at the top.
  5. Use the following syntax: DATABASE_URL: ${{ secrets.DATABASE_URL }}
name: Cypress Tests 🧪

on: [push]

jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 🛒
        uses: actions/checkout@v2
      - name: Cypress build & run 🏗️
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
        uses: cypress-io/github-action@v4.2.0
        with:
          build: npm run build
          start: npm start
      - name: Run Component tests 🔬
        uses: cypress-io/github-action@v4
        with:
          install: false
          component: true

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!

Fixed it!

I just sorted this out here’s my solution.

  1. go to settings/secrets/actions
  2. Add your DATABASE_URL secret under Repository secrets not Environment secrets.
  3. Ensure you don’t use quotation marks
  4. In your GitHub workflow file import the environment variable in the build step not at the top.
  5. Use the following syntax: DATABASE_URL: ${{ secrets.DATABASE_URL }}
name: Cypress Tests 🧪

on: [push]

jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 🛒
        uses: actions/checkout@v2
      - name: Cypress build & run 🏗️
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
        uses: cypress-io/github-action@v4.2.0
        with:
          build: npm run build
          start: npm start
      - name: Run Component tests 🔬
        uses: cypress-io/github-action@v4
        with:
          install: false
          component: true

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:

Screen Shot 2021-11-16 at 7 07 46 AM

When the test coverage encounters the

url      = env("DATABASE_URL")

It triggers the following clarified error:

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.

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=*:

Screen Shot 2021-11-16 at 7 19 21 AM :

And all Environment Variable values are entered accordingly in the .env & .env.local files.