From a23ed0ffbe7c8c405667c04e92e472aaa70e955f Mon Sep 17 00:00:00 2001 From: sljeff Date: Sun, 14 Aug 2022 11:39:15 +0800 Subject: [PATCH] feat: nextjs runtime read env var --- Dockerfile | 6 ++++++ deploy/entrypoint.sh | 31 +++++++++++++++++++++++++++++++ deploy/env.production | 7 +++++++ deploy/prod/deploy.yaml | 1 - deploy/prod/secrets.yaml | 1 + 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 deploy/entrypoint.sh create mode 100644 deploy/env.production diff --git a/Dockerfile b/Dockerfile index 9806eb45..a647a0e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,7 @@ FROM deps as build WORKDIR /app COPY --from=deps /app /app +COPY ./deploy/env.production .env.production ENV NEXT_TELEMETRY_DISABLED 1 ENV NODE_ENV production @@ -36,6 +37,11 @@ WORKDIR /app COPY --from=build /app/.next/standalone /app COPY --from=build /app/public /app/public COPY --from=build /app/.next/static /app/.next/static +COPY ./deploy/entrypoint.sh . +# to identify all the configuration +COPY ./deploy/env.production .env.production +RUN ["chmod", "+x", "./entrypoint.sh"] +ENTRYPOINT ["./entrypoint.sh"] CMD ["pnpm", "start"] diff --git a/deploy/entrypoint.sh b/deploy/entrypoint.sh new file mode 100644 index 00000000..667eb189 --- /dev/null +++ b/deploy/entrypoint.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# no verbose +set +x +# config +envFilename='.env.production' +nextFolder='./.next/' +function apply_path { + # read all config file + while read line; do + # no comment or not empty + if [ "${line:0:1}" == "#" ] || [ "${line}" == "" ]; then + continue + fi + + # split + configName="$(cut -d'=' -f1 <<<"$line")" + configValue="$(cut -d'=' -f2 <<<"$line")" + # get system env + envValue=$(env | grep "^$configName=" | grep -oe '[^=]*$'); + + # if config found + if [ -n "$configValue" ] && [ -n "$envValue" ]; then + # replace all + echo "Replace: ${configValue} with: ${envValue}" + find $nextFolder \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#$configValue#$envValue#g" + fi + done < $envFilename +} +apply_path +echo "Starting Nextjs" +exec "$@" diff --git a/deploy/env.production b/deploy/env.production new file mode 100644 index 00000000..877f0270 --- /dev/null +++ b/deploy/env.production @@ -0,0 +1,7 @@ +# https://github.com/vercel/next.js/discussions/17641 +# https://raphaelpralat.medium.com/system-environment-variables-in-next-js-with-docker-1f0754e04cde +NEXT_PUBLIC_APP_NAME=APP_NEXT_PUBLIC_APP_NAME +NEXT_PUBLIC_OUR_DOMAIN=APP_NEXT_PUBLIC_OUR_DOMAIN +NEXT_PUBLIC_DISCORD_LINK=APP_NEXT_PUBLIC_DISCORD_LINK +NEXT_PUBLIC_GITHUB_LINK=APP_NEXT_PUBLIC_GITHUB_LINK +REDIS_URL=APP_NEXT_REDIS_URL diff --git a/deploy/prod/deploy.yaml b/deploy/prod/deploy.yaml index fa114b44..76f9535a 100644 --- a/deploy/prod/deploy.yaml +++ b/deploy/prod/deploy.yaml @@ -25,7 +25,6 @@ spec: - image: $IMAGE_TAG_RELEASE imagePullPolicy: Always name: xlog - command: ["npm", "run", "start"] envFrom: - secretRef: name: xlog diff --git a/deploy/prod/secrets.yaml b/deploy/prod/secrets.yaml index 960c7bb0..adb95e77 100644 --- a/deploy/prod/secrets.yaml +++ b/deploy/prod/secrets.yaml @@ -1,3 +1,4 @@ +# Remember to add it in env.production because it is the nextjs project apiVersion: v1 stringData: NEXT_PUBLIC_APP_NAME: xlog