Skip to content

Commit 0f61b3a

Browse files
committed
fix: env read on build
1 parent b0aed01 commit 0f61b3a

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

.github/workflows/build-reusable.yml

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ name: Build Reusable Steps
33
on:
44
workflow_call:
55
outputs:
6-
build_output: # Changed from build-output to build_output for consistency
6+
build_output: # Changed from build-output to build_output for consistency
77
description: "Build output artifact"
88
value: ${{ jobs.build.outputs.build_output }}
99
jobs:
1010
build:
1111
name: "Build Application"
1212
runs-on: ubuntu-latest
13+
env:
14+
NEXT_PUBLIC_CONVEX_URL: ${{ secrets.NEXT_PUBLIC_CONVEX_URL }}
15+
CONVEX_DEPLOYMENT: ${{ secrets.CONVEX_DEPLOYMENT }}
16+
CLERK_SECRET_KEY: ${{ secrets.CLERK_SECRET_KEY }}
17+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: ${{ secrets.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY }}
18+
LIVE_BLOCK_SECRET_API_KEY: ${{ secrets.LIVE_BLOCK_SECRET_API_KEY }}
1319
outputs:
1420
build_output: ${{ steps.build_step.outputs.result }}
1521

@@ -55,32 +61,28 @@ jobs:
5561
pnpm install
5662
fi
5763
- name: Build
58-
id: build_step # Added this ID which is referenced in the outputs
64+
id: build_step # Added this ID which is referenced in the outputs
5965
env:
6066
NEXT_PUBLIC_CONVEX_URL: ${{ secrets.NEXT_PUBLIC_CONVEX_URL }}
6167
CONVEX_DEPLOYMENT: ${{ secrets.CONVEX_DEPLOYMENT }}
6268
CLERK_SECRET_KEY: ${{ secrets.CLERK_SECRET_KEY }}
6369
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: ${{ secrets.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY }}
6470
LIVE_BLOCK_SECRET_API_KEY: ${{ secrets.LIVE_BLOCK_SECRET_API_KEY }}
6571
run: |
66-
# Create a clean .env file
67-
rm -f .env
68-
touch .env
69-
70-
# Add each environment variable to .env file
71-
# Using printf to avoid issues with special characters
72-
printf "NEXT_PUBLIC_CONVEX_URL=%s\n" "$NEXT_PUBLIC_CONVEX_URL" >> .env
73-
printf "CONVEX_DEPLOYMENT=%s\n" "$CONVEX_DEPLOYMENT" >> .env
74-
printf "CLERK_SECRET_KEY=%s\n" "$CLERK_SECRET_KEY" >> .env
75-
printf "NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=%s\n" "$NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY" >> .env
76-
printf "LIVE_BLOCK_SECRET_API_KEY=%s\n" "$LIVE_BLOCK_SECRET_API_KEY" >> .env
77-
78-
72+
# Create .env file more efficiently (single operation)
73+
cat > .env << EOL
74+
NEXT_PUBLIC_CONVEX_URL=${NEXT_PUBLIC_CONVEX_URL}
75+
CONVEX_DEPLOYMENT=${CONVEX_DEPLOYMENT}
76+
CLERK_SECRET_KEY=${CLERK_SECRET_KEY}
77+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=${NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY}
78+
LIVE_BLOCK_SECRET_API_KEY=${LIVE_BLOCK_SECRET_API_KEY}
79+
EOL
80+
7981
# Run the build command
80-
pnpm build
81-
82-
# Set output for the workflow
83-
echo "result=success" >> $GITHUB_OUTPUT
82+
pnpm build || exit 1 # Add error handling
83+
84+
# Set output for the workflow more securely
85+
echo "result=success" >> "$GITHUB_OUTPUT"
8486
8587
- name: Upload build artifacts
8688
uses: actions/upload-artifact@v4
@@ -92,4 +94,4 @@ jobs:
9294
package.json
9395
pnpm-lock.yaml
9496
next.config.js
95-
.env
97+
.env
Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { auth, currentUser } from "@clerk/nextjs/server";
2-
// import { Liveblocks } from "@liveblocks/node";
3-
// import { ConvexHttpClient } from "convex/browser";
4-
// import { api } from "../../../../../convex/_generated/api";
5-
6-
// const convex = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);
7-
// const liveblocks = new Liveblocks({
8-
// secret: process.env.LIVE_BLOCK_SECRET_API_KEY!,
9-
// });
2+
import { Liveblocks } from "@liveblocks/node";
3+
import { ConvexHttpClient } from "convex/browser";
4+
import { api } from "../../../../../convex/_generated/api";
5+
6+
// Ensure these environment variables are defined in your GitHub Actions workflow
7+
// Add error handling for missing environment variables
8+
const convex = new ConvexHttpClient(
9+
process.env.NEXT_PUBLIC_CONVEX_URL ??
10+
(() => { throw new Error("NEXT_PUBLIC_CONVEX_URL environment variable is not defined") })()
11+
);
12+
13+
const liveblocks = new Liveblocks({
14+
secret: process.env.LIVE_BLOCK_SECRET_API_KEY ??
15+
(() => { throw new Error("LIVE_BLOCK_SECRET_API_KEY environment variable is not defined") })()
16+
});
1017

1118
export async function POST(req: Request) {
1219
const { sessionClaims } = await auth();
@@ -16,31 +23,30 @@ export async function POST(req: Request) {
1623
if (!user) return new Response("Unauthorized", { status: 401 });
1724

1825
const { room } = await req.json();
19-
// const document = await convex.query(api.document.get, {
20-
// id: room,
21-
// ignoreAuth: true,
22-
// });
23-
24-
// if (!document) return new Response("Unauthorized", { status: 401 });
25-
26-
// const isOwner = document.ownerId === user.id;
27-
// const isOrgMember = !!(
28-
// document.organizationId && document.organizationId === sessionClaims.org_id
29-
// );
30-
31-
// if (!isOwner && !isOrgMember)
32-
// return new Response("Unauthorized", { status: 401 });
33-
34-
// const session = liveblocks.prepareSession(user.id, {
35-
// userInfo: {
36-
// name:
37-
// user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous",
38-
// avatar: user.imageUrl,
39-
// },
40-
// });
41-
42-
// session.allow(room, session.FULL_ACCESS);
43-
// const { body, status } = await session.authorize();
44-
// return new Response(body, { status });
45-
return new Response(room, { status: 200 });
26+
const document = await convex.query(api.document.get, {
27+
id: room,
28+
ignoreAuth: true,
29+
});
30+
31+
if (!document) return new Response("Unauthorized", { status: 401 });
32+
33+
const isOwner = document.ownerId === user.id;
34+
const isOrgMember = !!(
35+
document.organizationId && document.organizationId === sessionClaims.org_id
36+
);
37+
38+
if (!isOwner && !isOrgMember)
39+
return new Response("Unauthorized", { status: 401 });
40+
41+
const session = liveblocks.prepareSession(user.id, {
42+
userInfo: {
43+
name:
44+
user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous",
45+
avatar: user.imageUrl,
46+
},
47+
});
48+
49+
session.allow(room, session.FULL_ACCESS);
50+
const { body, status } = await session.authorize();
51+
return new Response(body, { status });
4652
}

0 commit comments

Comments
 (0)