[英]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.