Skip to content

Commit 0325ec0

Browse files
ntindlePwuts
andauthored
fix(frontend): Fix environment variable handling in Docker builds for dev/prod deployments (#10859)
<!-- Clearly explain the need for these changes: --> Sentry was not being enabled in dev/prod deployments because environment variables were being incorrectly overwritten during the Docker build process. ### Changes 🏗️ - Fixed Dockerfile environment variable merging logic to prevent `.env.default` from overwriting `.env.production` values - Added `NODE_ENV=production` to build stage to ensure Next.js looks for production env files - Updated env file merging to only run when not in CI/CD (when `.env.production` doesn't exist) - When `.env.production` exists (CI/CD), now merges defaults with production values properly ### Checklist 📋 #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [ ] I have tested my changes according to the test plan: <!-- Put your test plan here: --> - [ ] Verify local Docker builds still work with `docker compose up` - [ ] Verify dev deployment has `NEXT_PUBLIC_APP_ENV=dev` in built JavaScript - [ ] Verify prod deployment has `NEXT_PUBLIC_APP_ENV=prod` in built JavaScript - [ ] Verify Sentry is enabled in dev/prod deployments (`isProdOrDev=true`) #### For configuration changes: - [x] `.env.default` is updated or already compatible with my changes - [x] `docker-compose.yml` is updated or already compatible with my changes - [x] I have included a list of my configuration changes in the PR description (under **Changes**) ### Technical Details **Root Cause:** 1. CI/CD workflow creates `.env.production` with correct values (e.g., `NEXT_PUBLIC_APP_ENV=dev`) 2. Dockerfile's env merging logic always created `.env` from `.env.default` 3. Next.js loads `.env.production` first, then `.env` second 4. Since `.env` is loaded after `.env.production`, it overwrites the values 5. `.env.default` has `NEXT_PUBLIC_APP_ENV=local`, causing `getAppEnv()` to return "local" instead of "dev"/"prod" 6. This made `isProdOrDev` evaluate to `false`, disabling Sentry **Solution:** The Dockerfile now checks if `.env.production` exists: - If yes (CI/CD): Merges `.env.default` + `.env.production` → `.env.production` (production values take precedence) - If no (local): Merges `.env.default` + `.env` → `.env` (user values take precedence) This ensures production deployments get the correct environment variables while preserving local development workflow. 🤖 Description generated + Investigation assisted with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Reinier van der Leer <[email protected]>
1 parent 3952a1a commit 0325ec0

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

autogpt_platform/frontend/.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ pnpm-lock.yaml
44
.auth
55
build
66
public
7+
Dockerfile
8+
.prettierignore

autogpt_platform/frontend/Dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ COPY autogpt_platform/frontend/ .
1212
# Allow CI to opt-in to Playwright test build-time flags
1313
ARG NEXT_PUBLIC_PW_TEST="false"
1414
ENV NEXT_PUBLIC_PW_TEST=$NEXT_PUBLIC_PW_TEST
15-
RUN if [ -f .env ]; then \
15+
ENV NODE_ENV="production"
16+
# Merge env files appropriately based on environment
17+
RUN if [ -f .env.production ]; then \
18+
# In CI/CD: merge defaults with production (production takes precedence)
19+
cat .env.default .env.production > .env.merged && mv .env.merged .env.production; \
20+
elif [ -f .env ]; then \
21+
# Local with custom .env: merge defaults with .env
1622
cat .env.default .env > .env.merged && mv .env.merged .env; \
1723
else \
24+
# Local without custom .env: use defaults
1825
cp .env.default .env; \
1926
fi
2027
RUN pnpm run generate:api

autogpt_platform/frontend/instrumentation-client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// The config you add here will be used whenever a users loads a page in their browser.
33
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
44

5-
import { BehaveAs, getBehaveAs, getEnvironmentStr } from "@/lib/utils";
5+
import {
6+
AppEnv,
7+
BehaveAs,
8+
getAppEnv,
9+
getBehaveAs,
10+
getEnvironmentStr,
11+
} from "@/lib/utils";
612
import * as Sentry from "@sentry/nextjs";
713

8-
const isProdOrDev =
9-
process.env.NODE_ENV === "production" ||
10-
process.env.NODE_ENV === "development";
14+
const isProdOrDev = [AppEnv.PROD, AppEnv.DEV].includes(getAppEnv());
1115

1216
const isCloud = getBehaveAs() === BehaveAs.CLOUD;
1317
const isDisabled = process.env.DISABLE_SENTRY === "true";

autogpt_platform/frontend/sentry.edge.config.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
55

66
import * as Sentry from "@sentry/nextjs";
7-
import { BehaveAs, getBehaveAs, getEnvironmentStr } from "./src/lib/utils";
8-
9-
const isProdOrDev =
10-
process.env.NODE_ENV === "production" ||
11-
process.env.NODE_ENV === "development";
7+
import {
8+
AppEnv,
9+
BehaveAs,
10+
getAppEnv,
11+
getBehaveAs,
12+
getEnvironmentStr,
13+
} from "./src/lib/utils";
14+
15+
const isProdOrDev = [AppEnv.PROD, AppEnv.DEV].includes(getAppEnv());
1216

1317
const isCloud = getBehaveAs() === BehaveAs.CLOUD;
1418
const isDisabled = process.env.DISABLE_SENTRY === "true";

autogpt_platform/frontend/sentry.server.config.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
// The config you add here will be used whenever the server handles a request.
33
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
44

5-
import { BehaveAs, getBehaveAs, getEnvironmentStr } from "@/lib/utils";
5+
import {
6+
AppEnv,
7+
BehaveAs,
8+
getAppEnv,
9+
getBehaveAs,
10+
getEnvironmentStr,
11+
} from "@/lib/utils";
612
import * as Sentry from "@sentry/nextjs";
713
// import { NodeProfilingIntegration } from "@sentry/profiling-node";
814

9-
const isProdOrDev =
10-
process.env.NODE_ENV === "production" ||
11-
process.env.NODE_ENV === "development";
15+
const isProdOrDev = [AppEnv.PROD, AppEnv.DEV].includes(getAppEnv());
1216

1317
const isCloud = getBehaveAs() === BehaveAs.CLOUD;
1418
const isDisabled = process.env.DISABLE_SENTRY === "true";

0 commit comments

Comments
 (0)