From a3e7998d111b13a7252a5b2aeef0207c7d63a4dd Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 28 Feb 2025 15:14:22 +0100 Subject: [PATCH 1/2] fix: use conditionally the right url for backend in nextjs frontend --- .env | 4 +--- backend/server.ts | 4 ++++ frontend/Dockerfile | 3 ++- frontend/next.config.ts | 7 ++++++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.env b/.env index f570331..7d9e540 100644 --- a/.env +++ b/.env @@ -2,6 +2,4 @@ NODE_ENV=development HOSTNAME=0.0.0.0 FRONTEND_PORT=3000 -BACKEND_PORT=4000 -NEXT_PUBLIC_BACKEND_URL=http://localhost:4000 -CORS_ALLOWED_ORIGIN=http://localhost:3000 \ No newline at end of file +BACKEND_PORT=4000 \ No newline at end of file diff --git a/backend/server.ts b/backend/server.ts index 1b7bd3e..fbdfc91 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -10,6 +10,10 @@ import { generateQRCode } from "./src/routes/generateqrcode.route"; import { wordCounter } from "./src/routes/wordcounter.route"; import { videoToAudio } from "./src/routes/videotoaudio.route"; import { removeBG } from "./src/routes/removebg.route"; +import dotenv from "dotenv"; +import path from "node:path"; + +dotenv.config({ path: path.resolve(__dirname, "../.env") }); const app = Fastify({ logger: true }); diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 8c2aca8..6261969 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -24,6 +24,7 @@ WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . +ENV NODE_ENV=production ARG NEXT_PUBLIC_BACKEND_URL ENV NEXT_PUBLIC_BACKEND_URL=$NEXT_PUBLIC_BACKEND_URL @@ -62,4 +63,4 @@ EXPOSE 3000 ENV PORT=3000 ENV HOSTNAME="0.0.0.0" -CMD echo "NEXT_PUBLIC_BACKEND_URL = $NEXT_PUBLIC_BACKEND_URL" && node server.js \ No newline at end of file +CMD node server.js \ No newline at end of file diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 61ab14e..6030ff1 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -1,9 +1,14 @@ import type { NextConfig } from "next"; +const NEXT_PUBLIC_BACKEND_URL = + process.env.NODE_ENV === "production" + ? process.env.NEXT_PUBLIC_BACKEND_URL || "" + : "http://localhost:4000"; + const nextConfig: NextConfig = { output: "standalone", env: { - NEXT_PUBLIC_BACKEND_URL: process.env.NEXT_PUBLIC_BACKEND_URL, + NEXT_PUBLIC_BACKEND_URL: NEXT_PUBLIC_BACKEND_URL, }, }; From 05056201456e0463beb5b7ea6c0463fb96b48eb0 Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 28 Feb 2025 15:23:34 +0100 Subject: [PATCH 2/2] fix: dynamic node environment variable --- .github/workflows/deploy.yml | 2 ++ backend/Dockerfile | 3 ++- docker-compose.yaml | 2 ++ frontend/Dockerfile | 6 ++++-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a0ed3d1..6c12200 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -41,6 +41,7 @@ jobs: tags: ${{ secrets.DOCKERHUB_USERNAME }}/werkzeugkiste-frontend:latest build-args: | NEXT_PUBLIC_BACKEND_URL=${{ secrets.NEXT_PUBLIC_BACKEND_URL }} + NODE_ENV=${{ secrets.NODE_ENV }} - name: Build and Push Backend uses: docker/build-push-action@v6 @@ -50,6 +51,7 @@ jobs: tags: ${{ secrets.DOCKERHUB_USERNAME }}/werkzeugkiste-backend:latest build-args: | CORS_ALLOWED_ORIGIN=${{ secrets.CORS_ALLOWED_ORIGIN }} + NODE_ENV=${{ secrets.NODE_ENV }} deploy-on-server: needs: docker-build-push diff --git a/backend/Dockerfile b/backend/Dockerfile index c6a7478..03e81f9 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -48,7 +48,8 @@ RUN \ FROM base AS runner WORKDIR /app -ENV NODE_ENV=production +ARG NODE_ENV +ENV NODE_ENV=$NODE_ENV RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 fastify diff --git a/docker-compose.yaml b/docker-compose.yaml index aeb5cd4..1d47473 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,6 +6,7 @@ services: dockerfile: Dockerfile args: NEXT_PUBLIC_BACKEND_URL: ${NEXT_PUBLIC_BACKEND_URL} + NODE_ENV: ${NODE_ENV} container_name: werkzeugkiste-frontend environment: - NODE_ENV=${NODE_ENV} @@ -22,6 +23,7 @@ services: dockerfile: Dockerfile args: CORS_ALLOWED_ORIGIN: ${CORS_ALLOWED_ORIGIN} + NODE_ENV: ${NODE_ENV} container_name: werkzeugkiste-backend environment: - NODE_ENV=${NODE_ENV} diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 6261969..7aae739 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -24,7 +24,8 @@ WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . -ENV NODE_ENV=production +ARG NODE_ENV +ENV NODE_ENV=$NODE_ENV ARG NEXT_PUBLIC_BACKEND_URL ENV NEXT_PUBLIC_BACKEND_URL=$NEXT_PUBLIC_BACKEND_URL @@ -44,7 +45,8 @@ RUN \ FROM base AS runner WORKDIR /app -ENV NODE_ENV=production +ARG NODE_ENV +ENV NODE_ENV=$NODE_ENV # Uncomment the following line in case you want to disable telemetry during runtime. # ENV NEXT_TELEMETRY_DISABLED=1