![](/img/trans.png)
[英]CI/CD with gitlab (online) to kubernetes cluster (kismatic)
[英]ENV_VARS "undefined" when deploying Next JS app through Gitlab CI/CD to a Kubernetes cluster:
我现在很长时间以来一直坚持这个问题:
我有一个标准的 NextJS 应用程序,它使用环境变量(用于客户端NEXT_PUBLIC_MY_VAR
以及服务器端MY_OTHER_VAR
)。
我将 Gitlab CI-CD AutoDevOps 与一个微小的自定义.gitlab-ci.yml
文件一起使用(见下文)。
我使用 Gitlab 成功连接到我的 Kube.netes 集群,我的 NextJS 应用程序也成功部署。 (Gitlab/K8s 缓存也清理了)
我唯一挣扎的是让process.env.ENV_VARS
运行。 无论我尝试过什么,它们都是undefined
。
我手动将我的应用程序部署到集群中,并将一个 configMap 安装到我的部署中(因此.env.local
文件存在于/app/.env.local
只有然后ENV_VARS
设置正确。
那么如何在通过 Gitlab Auto DevOps 部署我的 NextJS 应用程序时设置ENV_VARS
?
到目前为止我已经尝试过:
ENV_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
module.exports = withSvgr({
serverRuntimeConfig: {},
publicRuntimeConfig: {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
API_URL: process.env.API_URL,
},
});
.gitlab-ci.yml
文件(完整文件在这里):yaml 包括: - 模板:Auto-DevOps.gitlab-ci.yml
# added vars for build
build:
stage: build
variables:
API_URL: $API_URL
这是我的 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
并且是错误所指的代码:
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()
);
为了完整性(但大部分没用)失败作业的尾部(这似乎是 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
的唯一方法吗?在此先感谢您,我们将不胜感激
如果您坚持使用 GITLAB 上的 Auto Devops ,那么您必须设置此 Gitlab ENV 变量AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS
并将其设置为
--build-arg=NEXT_PUBLIC_API_URL=http://your.domain.com/api
在您的 Dockerfile 中,您可以通过
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
更多信息在这里:
https://docs.gitlab.com/ee/topics/autodevops/customize.html#passing-arguments-to-docker-build
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.