Multiple issues on NextJs + Prisma deploys

I’m gonna list my questions and issues on the same topic because I think they are all related

  1. Am I correct to assume there are only two options for places my build stage can accour? Locally (wherever flyctl deploy is called) and remote (on any fly.io machine my app owns)?

  2. When I run a build with --remote-only. It means the build will run, for sure, on my fly.io instace AND will have access to both .flycast and .internal from apps on the same organization. And if it’s run --local-only it will run, for sure, on whatever environment the flyctl deploy was called, and will not have access to .flycast and .internal urls. Is this correct?

  3. Environment variables are not available on build steps by default. And the only way to make them available, is to include them on the flyctl deploy command throught the --build-secret DATABASE_URL=${{DATABASE_URL}} argument. Is this correct?

These are the three thing I assumed to be correct when building my deploy pipeline. Unfortunately, there are two thing wrong on my deploys:

  1. I use the nextJs server proxy to route requests to my backend. Unfortunately, nextJs requests are failing the .flycast address to my backend server, as it can be observed on the following logs:
2024-10-25 10:34:55.009 @diet-it/web:start:   hostname: 'diet-it-backend.flycast'
2024-10-25 10:34:55.009 @diet-it/web:start:   syscall: 'getaddrinfo',
2024-10-25 10:34:55.009 @diet-it/web:start:   code: 'ENOTFOUND',
2024-10-25 10:34:55.009 @diet-it/web:start:   errno: -3007,
2024-10-25 10:34:55.009 @diet-it/web:start:   at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26) {
2024-10-25 10:34:55.009 @diet-it/web:start:     Error: getaddrinfo ENOTFOUND diet-it-backend.flycast
2024-10-25 10:34:55.002 @diet-it/web:start:   }

Here are two addresses to my backend server:
https://diet-it-backend.fly.dev/
https://backend.dietit.co/
2. I doesn’t matter what I try. My Fly Deploy github actions never work. They either fail because they don’t have access to the DATABASE_URL (required in my build step) or they do a false positive deploy on github, but the deploy never shows up on my dashboard and the server never gets updates.
Here’re my Fly Deploy github actions:

# /.github/workflows/fly-deploy-web.yml
name: Fly Deploy
on:
  push:
    branches:
      - main
jobs:
  deploy:
    name: Deploy @diet-it/web
    runs-on: ubuntu-latest
    concurrency: deploy-group    # optional: ensure only one action runs at a time
    steps:
      - uses: actions/checkout@v4
      - uses: superfly/flyctl-actions/setup-flyctl@master
      - run: flyctl deploy . --remote-only -a diet-it-web --config ./apps/web/fly.toml --dockerfile ./apps/web/Dockerfile --build-secret DATABASE_URL=${{ secrets.DATABASE_URL }} / DATABASE_URL=https://diet-it-backend.flycast/
        env:
          FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN_WEB }}

# /.github/workflows/fly-deploy-backend.yml
name: Fly Deploy
on:
  push:
    branches:
      - main
jobs:
  deploy:
    name: Deploy @diet-it/backend
    runs-on: ubuntu-latest
    concurrency: deploy-group # optional: ensure only one action runs at a time
    steps:
      - uses: actions/checkout@v4
      - uses: superfly/flyctl-actions/setup-flyctl@master
      - run: flyctl deploy . --remote-only -a diet-it-backend --config ./apps/backend/fly.toml --dockerfile ./apps/backend/Dockerfile
        env:
          FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN_BACKEND }}

Has anyone gone through something like this? Thank you in advance!

I can see clearly that even with this command on github actions:

- run: flyctl deploy . --remote-only -a diet-it-backend --config ./apps/backend/fly.toml --dockerfile ./apps/backend/Dockerfile --build-secret DATABASE_URL=${{ secrets.DATABASE_URL }}```
or this command locally (with the correct .env locally):

flyctl deploy . --remote-only -a diet-it-backend --config ./apps/backend/fly.toml --dockerfile ./apps/backend/Dockerfile

My `diet-it-backend` app never gets access to DATABASE_URL during the build process. As per the following logs on my `diet-it-backend` grafana environment:

2024-10-25 12:12:56.491 @diet-it/db:db:build: ERROR: command finished with error: command (/usr/src/app/packages/db) /usr/local/bin/bun run db:build exited (1)
2024-10-25 12:12:56.490 @diet-it/db:db:build: error: “prisma” exited with code 1
2024-10-25 12:12:56.468 @diet-it/db:db:build: Prisma CLI Version : 5.21.1
2024-10-25 12:12:56.468 @diet-it/db:db:build:
2024-10-25 12:12:56.468 @diet-it/db:db:build: [Context: getConfig]
2024-10-25 12:12:56.468 @diet-it/db:db:build: Validation Error Count: 1
2024-10-25 12:12:56.468 @diet-it/db:db:build:
2024-10-25 12:12:56.468 @diet-it/db:db:build: |
2024-10-25 12:12:56.468 @diet-it/db:db:build: 8 | url = env(“DATABASE_URL”)
2024-10-25 12:12:56.468 @diet-it/db:db:build: 7 | provider = “postgresql”
2024-10-25 12:12:56.468 @diet-it/db:db:build: |
2024-10-25 12:12:56.468 @diet-it/db:db:build: → prisma/schema/schema.prisma:8
2024-10-25 12:12:56.468 @diet-it/db:db:build: error: Environment variable not found: DATABASE_URL.

There are four types of builders, plus there is an optional release deploy step. I don’t know what you are trying to do, but I suspect that what you want is the release deploy step.

The four types of builders are depot, fly legacy, rchab, and local. fly legacy and rchab are basically the same thing, but by launching it yourself you can have greater control.

Release commands are defined in fly.toml, and are run on an ephemeral machine with your image and secrets, and on your network. Even if you are deploying to 50 servers, release commands are run exactly once.

I think you are correct. I want to habdle realse builds. The problem is, I can handle them locally only, but I want to be able to deploy correctly on pushes to my main branch.
And none of the things I’ve tried in the last three days of debugging has helped.

Can you describe what you are trying to do that release commands can’t do?

You don’t need to change your github action, just your fly.toml. flyctl deploy builds the image, pushes it to a repository, runs the release command (if defined) then starts the new image on each of your machines.

The release command is run on an ephemeral machine within your network with access to all of your secrets.

I need to deploy a project that’s next+prisma. My build process can be simplified in a couple commands:

npm install          
npm run prisma migrate deploy   # Apply database migrations
npm run prisma generate         # Generate types based on schema
npm run build                   # -> next build

The step npm run prisma migrate deploy needs access to the database.
The problem is. I’m not sure where this build process happens.

  1. If it happens on github actions server, I think it will not have access to my private only database.
  2. If it happens on fly.io server, it will have access to the .flycast domain.
    Where and how can I run this process while connected to a .flycast or .internal url?