Skip to content

Commit f26a877

Browse files
authored
ENG-1395 Official deployment version cannot access database (#751)
* ENG-1395 Add a route to get the anonymous key, and use it in roam build script
1 parent 4928605 commit f26a877

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

apps/roam/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22
set -e
3+
export ROAM_BUILD_SCRIPT=1
34
npm install -g corepack@latest
45
corepack enable pnpm
56
pnpm install
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { NextResponse, NextRequest } from "next/server";
2+
import {
3+
handleRouteError,
4+
defaultOptionsHandler,
5+
} from "~/utils/supabase/apiUtils";
6+
7+
export const GET = (request: NextRequest): NextResponse => {
8+
try {
9+
const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env;
10+
if (!SUPABASE_URL || !SUPABASE_ANON_KEY)
11+
return new NextResponse("Missing variables", { status: 500 });
12+
return NextResponse.json(
13+
// eslint-disable-next-line @typescript-eslint/naming-convention
14+
{ SUPABASE_URL, SUPABASE_ANON_KEY },
15+
{ status: 200 },
16+
);
17+
} catch (e: unknown) {
18+
return handleRouteError(request, e, "/api/supabase/env");
19+
}
20+
};
21+
22+
export const OPTIONS = defaultOptionsHandler;

packages/database/scripts/createEnv.mts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fileURLToPath } from "node:url";
55
import dotenv from "dotenv";
66
import { Vercel } from "@vercel/sdk";
77

8+
// eslint-disable-next-line @typescript-eslint/naming-convention
89
const __dirname = dirname(fileURLToPath(import.meta.url));
910
const projectRoot = join(__dirname, "..");
1011
const baseParams: Record<string, string> = {};
@@ -106,11 +107,27 @@ const makeProductionEnv = async (vercel: Vercel, vercelToken: string) => {
106107
};
107108

108109
const main = async (variant: Variant) => {
109-
// Do not execute in deployment or github action.
110-
if (
110+
if (process.env.ROAM_BUILD_SCRIPT) {
111+
// special case: production build
112+
try {
113+
const response = execSync('curl https://discoursegraphs.com/api/supabase/env');
114+
const asJson = JSON.parse(response.toString()) as Record<string, string>;
115+
writeFileSync(
116+
join(projectRoot, ".env"),
117+
Object.entries(asJson).map(([k,v])=>`${k}=${v}`).join('\n')
118+
);
119+
return;
120+
} catch (e) {
121+
if (process.env.SUPABASE_URL && process.env.SUPABASE_ANON_KEY)
122+
return;
123+
throw new Error("Could not get environment from site");
124+
}
125+
}
126+
else if (
111127
process.env.HOME === "/vercel" ||
112128
process.env.GITHUB_ACTIONS !== undefined
113129
)
130+
// Do not execute in deployment or github action.
114131
return;
115132

116133
if (variant === Variant.none) return;

turbo.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"GEMINI_API_KEY",
3131
"GH_CLIENT_SECRET_PROD",
3232
"GITHUB_ACTIONS",
33+
"HOME",
3334
"OPENAI_API_KEY",
3435
"POSTGRES_PASSWORD",
3536
"POSTGRES_USER",
@@ -42,6 +43,7 @@
4243
],
4344
"tasks": {
4445
"build": {
46+
"env": ["ROAM_BUILD_SCRIPT"],
4547
"dependsOn": ["@repo/database#genenv"],
4648
"inputs": ["$TURBO_DEFAULT$", ".env*"],
4749
"outputs": [".next/**", "!.next/cache/**", "dist/**", "src/dbTypes.ts"]
@@ -69,7 +71,7 @@
6971
"with": ["@repo/database#dev"]
7072
},
7173
"genenv": {
72-
"env": ["SUPABASE_USE_DB"],
74+
"env": ["SUPABASE_USE_DB", "ROAM_BUILD_SCRIPT"],
7375
"cache": false,
7476
"outputs": [".env*"]
7577
},

0 commit comments

Comments
 (0)