Skip to content

Commit 268928d

Browse files
committed
debug(ci): enhance JWT secret and environment variable handling
1 parent 1d99b52 commit 268928d

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

.github/workflows/cd_frontend.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,27 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- name: Checkout the code
10-
uses: actions/checkout@v2
10+
uses: actions/checkout@v4
1111

1212
- name: Docker login
13-
uses: docker/login-action@v2
13+
uses: docker/login-action@v3
1414
with:
1515
username: ${{ secrets.DOCKER_USERNAME }}
1616
password: ${{ secrets.DOCKER_PASSWORD }}
1717

18+
- name: Verify secrets
19+
run: |
20+
if [ -z "${{ secrets.DATABASE_URL }}" ]; then
21+
echo "Error: DATABASE_URL secret is not set"
22+
exit 1
23+
fi
24+
if [ -z "${{ secrets.JWT_SECRET }}" ]; then
25+
echo "Error: JWT_SECRET secret is not set"
26+
exit 1
27+
fi
28+
1829
- name: Build and push
19-
uses: docker/build-push-action@v4
30+
uses: docker/build-push-action@v5
2031
with:
2132
context: .
2233
file: ./docker/Dockerfile.frontend

apps/collabydraw/next.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NextConfig } from "next";
2+
import { webpack } from "next/dist/compiled/webpack/webpack";
23

34
const nextConfig: NextConfig = {
45
/* config options here */
@@ -10,6 +11,18 @@ const nextConfig: NextConfig = {
1011
publicRuntimeConfig: {
1112
JWT_SECRET: process.env.JWT_SECRET,
1213
},
14+
15+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
16+
webpack: (config, { isServer }) => {
17+
// Ensure environment variables are always available
18+
config.plugins.push(
19+
new webpack.DefinePlugin({
20+
'process.env.JWT_SECRET': JSON.stringify(process.env.JWT_SECRET),
21+
'process.env.DATABASE_URL': JSON.stringify(process.env.DATABASE_URL),
22+
})
23+
);
24+
return config;
25+
}
1326
};
1427

1528
export default nextConfig;

apps/collabydraw/utils/auth.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ export const authOptions: NextAuthOptions = {
5858
token.id = user.id;
5959
token.email = user.email;
6060
}
61-
61+
if (!process.env.JWT_SECRET) {
62+
throw new Error("JWT_SECRET is ABSOLUTELY REQUIRED and not set");
63+
}
6264
token.accessToken = jwt.sign(
6365
{ id: token.id, email: token.email },
64-
process.env.NEXT_PUBLIC_JWT_SECRET || "",
66+
process.env.JWT_SECRET,
6567
{ expiresIn: "7d" }
6668
);
6769
return token;
@@ -75,5 +77,5 @@ export const authOptions: NextAuthOptions = {
7577
return session;
7678
},
7779
},
78-
secret: process.env.NEXT_PUBLIC_JWT_SECRET,
80+
secret: process.env.JWT_SECRET,
7981
};

docker/Dockerfile.frontend

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ WORKDIR /usr/src/app
99
ARG DATABASE_URL
1010
ARG JWT_SECRET
1111

12-
# Set environment variables
13-
ENV DATABASE_URL=${DATABASE_URL}
14-
ENV JWT_SECRET=${JWT_SECRET}
15-
ENV NEXT_PUBLIC_JWT_SECRET=${JWT_SECRET}
16-
1712
# Copy necessary files for dependency installation
1813
COPY ./packages ./packages
1914
COPY ./package.json ./package.json
@@ -38,11 +33,30 @@ RUN prisma generate
3833
# Change back to the root directory
3934
WORKDIR /usr/src/app
4035

36+
# Set environment variables
37+
ENV DATABASE_URL=${DATABASE_URL}
38+
ENV JWT_SECRET=${JWT_SECRET}
39+
40+
# Create .env.local for Next.js
41+
WORKDIR /usr/src/app/apps/collabydraw
42+
RUN echo "JWT_SECRET=$JWT_SECRET" > .env.local
43+
RUN echo "DATABASE_URL=$DATABASE_URL" >> .env.local
44+
45+
# Change back to the root directory
46+
WORKDIR /usr/src/app
47+
48+
# Debugging step
49+
# RUN echo "DATABASE_URL length: ${#DATABASE_URL}"
50+
# RUN echo "JWT_SECRET length: ${#JWT_SECRET}"
51+
52+
# Fail if secrets are not set
53+
# RUN test -n "$DATABASE_URL" && test -n "$JWT_SECRET"
54+
4155
# Debug step: print environment variables
42-
RUN echo "DATABASE_URL: ${DATABASE_URL}" && echo "JWT_SECRET set: ${JWT_SECRET:+yes}"
56+
# RUN echo "DATABASE_URL: ${DATABASE_URL}" && echo "JWT_SECRET set: ${JWT_SECRET:+yes}"
4357

4458
# Build the application with database URL
45-
RUN pnpm run build
59+
RUN DATABASE_URL=${DATABASE_URL} JWT_SECRET=${JWT_SECRET} pnpm run build
4660

4761
# Expose the port
4862
EXPOSE 3000

0 commit comments

Comments
 (0)