redwoodjs / redwood

The App Framework for Startups
https://redwoodjs.com
MIT License
17.33k stars 994 forks source link

[Bug?]: .env.defaults overwriting .env #6111

Open razzeee opened 2 years ago

razzeee commented 2 years ago

What's not working?

We had a setup working since around may.

Basically setting environment variables via gitlab and copying everything over to then build. Today our test and prod instance was not able to connect to the database anymore.

It turned out, that suddenly the entries in .env.defaults that we're also set via gitlab were overwritten by the default values.

I'm not sure what changed or if this is even actionable. So feel free to close it. Just making sure it's documented.

How do we reproduce the bug?

What's your environment? (If it applies)

This is my local one, not the server. It also seems to get the database wrong, we're using postgres.

 System:
    OS: Linux 5.18 Fedora Linux 36 (Workstation Edition)
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 16.14.0 - /tmp/xfs-314f7f4e/node
    Yarn: 3.2.1 - /tmp/xfs-314f7f4e/yarn
  Databases:
    SQLite: 3.36.0 - /usr/bin/sqlite3
  Browsers:
    Firefox: 102.0
  npmPackages:
    @redwoodjs/core: ^2.2.0 => 2.2.0

Are you interested in working on this?

dac09 commented 2 years ago

Hello @razzeee - thanks for raising this, but I'm not sure I quite understand what the issue here is.

I tried reproducing the simplest way to verify that I thought of was overriding the DATABASE_URL variable.

Here's what I did:

  1. My .env.defaults has DATABASE_URL=file:./dev.db
  2. In the command line I override it like this:
    DATABASE_URL=file:./other.db yarn rw prisma migrate dev
  3. See that other.db gets created next to dev.db file

Which means that it's using the env var I set through the command line, not the default one.

Is this not the behaviour you are seeing?

razzeee commented 2 years ago

It's not the behavior I'm seeing. But I think migrating worked at least I didn't see an error at that step, but on rw serve

I'm not sure if my docker file is breaking the env somehow, but I would be very surprised, as I did not change anything in that part of the code.

# ==
# Base
FROM node:16-alpine as base

WORKDIR /app

ARG NODE_ENV
ENV NODE_ENV=$NODE_ENV
ENV RUNTIME_ENV=$RUNTIME_ENV
ARG RUNTIME_ENV
ARG DATABASE_URL
ENV DATABASE_URL=$DATABASE_URL
ARG GITLAB_CLIENT_ID
ENV GITLAB_CLIENT_ID=$GITLAB_CLIENT_ID
ARG GITLAB_REDIRECT_URI
ENV GITLAB_REDIRECT_URI=$GITLAB_REDIRECT_URI
ARG GITLAB_AUTHORITY
ENV GITLAB_AUTHORITY=$GITLAB_AUTHORITY
ARG SITE_ID
ENV SITE_ID=$SITE_ID
ARG BASE_URL
ENV BASE_URL=$BASE_URL

COPY package.json package.json
COPY yarn.lock yarn.lock
COPY .yarn .yarn
COPY .yarnrc.yml .

COPY redwood.toml redwood.toml
COPY graphql.config.js graphql.config.js

COPY api/package.json api/package.json
COPY web/package.json web/package.json

RUN yarn install

COPY .env.defaults .
COPY run.sh .

# ==
# Build
FROM base as build

COPY api api
COPY web web
COPY scripts scripts

RUN yarn rw build api web

# ==
# Serve
FROM node:16-alpine as serve

WORKDIR /app

ARG DATABASE_URL
ENV DATABASE_URL=$DATABASE_URL
ARG GITLAB_AUTHORITY
ENV GITLAB_AUTHORITY=$GITLAB_AUTHORITY

COPY --from=build /app/run.sh /app/run.sh
RUN chmod +x ./run.sh

COPY --from=build /app/api/db/schema.prisma /app/api/db/schema.prisma
COPY --from=build /app/api/db/migrations /app/api/db/migrations

COPY --from=build /app/node_modules/.prisma /app/api/node_modules/.prisma
COPY --from=build /app/api/dist /app/api/dist
COPY --from=build /app/api/package.json /app/api/package.json
COPY --from=build /app/yarn.lock /app/api/yarn.lock

COPY --from=build /app/web/dist /app/web/dist

COPY --from=build /app/redwood.toml /app/redwood.toml

RUN yarn --cwd "api" --production install && \
  yarn global add @redwoodjs/cli react react-dom prisma

EXPOSE 8910

ENTRYPOINT [ "./run.sh" ]
CMD [""]

run.sh

#!/bin/sh

# NOTE: Requires binaries. Install with:
# yarn global add @redwoodjs/api-server @redwoodjs/internal prisma

prisma migrate deploy --schema=/app/api/db/schema.prisma
rw serve