[英]ENV_VARS "undefined" when deploying Next JS app through Gitlab CI/CD to a Kubernetes cluster:
I stick to this problem quite long now:我现在很长时间以来一直坚持这个问题:
I have a standard NextJS app, which uses environment variables (for client side NEXT_PUBLIC_MY_VAR
as well for server side ones MY_OTHER_VAR
).我有一个标准的 NextJS 应用程序,它使用环境变量(用于客户端
NEXT_PUBLIC_MY_VAR
以及服务器端MY_OTHER_VAR
)。
I use the Gitlab CI-CD AutoDevOps with an tiny custom .gitlab-ci.yml
-file (see below).我将 Gitlab CI-CD AutoDevOps 与一个微小的自定义
.gitlab-ci.yml
文件一起使用(见下文)。
I have a successful connection to my Kube.netes cluster with Gitlab, and my NextJS app is getting also deployed successfully.我使用 Gitlab 成功连接到我的 Kube.netes 集群,我的 NextJS 应用程序也成功部署。 (Gitlab/K8s cache cleaned, too)
(Gitlab/K8s 缓存也清理了)
The only thing where I struggle is to get the process.env.ENV_VARS
running.我唯一挣扎的是让
process.env.ENV_VARS
运行。 Whatever I tried they are undefined
.无论我尝试过什么,它们都是
undefined
。
I deployed my app manually into the cluster, and mounted a configMap to my deployment ( so a .env.local
-file is present at /app/.env.local
ONLY THEN the ENV_VARS
are set correctly.我手动将我的应用程序部署到集群中,并将一个 configMap 安装到我的部署中(因此
.env.local
文件存在于/app/.env.local
只有然后ENV_VARS
设置正确。
So how to set the ENV_VARS
when deploying my NextJS app via Gitlab Auto DevOps?那么如何在通过 Gitlab Auto DevOps 部署我的 NextJS 应用程序时设置
ENV_VARS
?
I've tried so far:到目前为止我已经尝试过:
ENV_VARS
in Gitlab -> Settings -> CI/CD -> VariablesENV_VARS
-> 设置 -> CI/CD -> 变量FROM node:alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# ...
# ..
# docker build --build-arg API_URL=http://myApiEndpoint
ARG API_URL
ENV API_URL=$API_URL
RUN npm run build
FROM node:alpine AS runner
WORKDIR /app
ENV NODE_ENV production
# COPY --from=builder /app/next.config.js ./ # TRIED WITH AND WITHOUT
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /app/.next
USER nextjs
EXPOSE 5000
CMD ["npm", "run", "start"]
next.config.js
next.config.js
module.exports = withSvgr({
serverRuntimeConfig: {},
publicRuntimeConfig: {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
API_URL: process.env.API_URL,
},
});
.gitlab-ci.yml
file (complete file here):.gitlab-ci.yml
文件(完整文件在这里): yaml include: - template: Auto-DevOps.gitlab-ci.yml yaml 包括: - 模板:Auto-DevOps.gitlab-ci.yml
# added vars for build
build:
stage: build
variables:
API_URL: $API_URL
This is the error message in my POD这是我的 POD 中的错误消息
> metashop-frontend.react@1.8.0 start
> next start -p 5000
ready - started server on 0.0.0.0:5000, url: http://localhost:5000
ApiURL alias: undefined #### <<<----
API_URL: undefined #### <<<----
TypeError: Only absolute URLs are supported
at getNodeRequestOptions (/app/node_modules/node-fetch/lib/index.js:1305:9)
at /app/node_modules/node-fetch/lib/index.js:1410:19
at new Promise (<anonymous>)
at fetch (/app/node_modules/node-fetch/lib/index.js:1407:9)
at Object.getItems (/app/.next/server/pages/_app.js:1194:12)
at getServerSideProps (/app/.next/server/pages/index.js:2952:55)
at renderToHTML (/app/node_modules/next/dist/next-server/server/render.js:40:221)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /app/node_modules/next/dist/next-server/server/next-server.js:109:97
at async /app/node_modules/next/dist/next-server/server/next-server.js:102:142
And is the code that the error refers to:并且是错误所指的代码:
const ApiUrl = process.env.API_URL;
console.log("ApiURL alias:", ApiUrl);
console.log("API_URL:", process.env.API_URL);
console.log("NEXT_PUBLIC_API_URL:", process.env.NEXT_PUBLIC_API_URL);
return fetch(`${ApiUrl}/items.json?${qs.stringify(options)}`).then(
(response) => response.json()
);
and for completeness (but mostly useless) the tail of the failing job (which seems the normal error when K8s not responding):为了完整性(但大部分没用)失败作业的尾部(这似乎是 K8s 没有响应时的正常错误):
Error: release production failed, and has been uninstalled due to atomic being set: timed out waiting for the condition
Uploading artifacts for failed job
00:01
Uploading artifacts...
WARNING: environment_url.txt: no matching files
WARNING: tiller.log: no matching files
ERROR: No files to upload
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1
.env.local
the only way to use NextJS ENV_VARS in Kube.netes? .env.local
的唯一方法吗? Thank you in advance, any help would be appreciated在此先感谢您,我们将不胜感激
If you stick on Auto Devops on GITLAB then YOU HAVE TO set this Gitlab ENV Variable AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS
and set it to如果您坚持使用 GITLAB 上的 Auto Devops ,那么您必须设置此 Gitlab ENV 变量
AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS
并将其设置为
--build-arg=NEXT_PUBLIC_API_URL=http://your.domain.com/api
In your Dockerfile you can assign it via在您的 Dockerfile 中,您可以通过
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
More information here:更多信息在这里:
https://docs.gitlab.com/ee/topics/autodevops/customize.html#passing-arguments-to-docker-build https://docs.gitlab.com/ee/topics/autodevops/customize.html#passing-arguments-to-docker-build
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.