简体   繁体   English

通过 Gitlab CI/CD 将 Next JS 应用程序部署到 Kube.netes 集群时,ENV_VARS“未定义”:

[英]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:到目前为止我已经尝试过:

  • setting ENV_VARS in Gitlab -> Settings -> CI/CD -> Variables在 Gitlab 中设置ENV_VARS -> 设置 -> CI/CD -> 变量

Gitlab 环境变量

  • I added an ARG in my Dockerfile which should pick up the Gitlab CI vars when building the Docker image:我在我的 Dockerfile 中添加了一个 ARG,它应该在构建 Docker 图像时获取 Gitlab CI 变量:
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"]
  • I have also added the ENV_VARS also in my next.config.js我还在我的 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,
    },
});

  • I also added a custom .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

is .env.local the only way to use NextJS ENV_VARS in Kube.netes? .env.local 是在 Kube.netes 中使用 NextJS .env.local的唯一方法吗?

Do I have to customize the Gilab AutoDevOps for this particular (and common) app deployment?我是否必须为此特定(和常见)应用程序部署自定义 Gilab AutoDevOps?

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.

相关问题 使用gitlab的CI / CD(在线)到kubernetes集群(动态) - CI/CD with gitlab (online) to kubernetes cluster (kismatic) 从 Gitlab CI 构建 docker 图像时环境变量丢失 - Env vars lost when building docker image from Gitlab CI 如何将Azure AKS Kubernetes Cluster自签名CA添加到GitLab CI / CD Kubernetes集成? - How to add an Azure AKS Kubernetes Cluster self-signed CA to GitLab CI/CD Kubernetes integration? Kubernetes集群上的Gitlab CI(Openstack) - Gitlab CI on Kubernetes Cluster (Openstack) GitLab CI/CD、Kubernetes 和私有卷 - GitLab CI/CD, Kubernetes and, Private Volumes 我在集群内使用 GItLab CI/CD kubernetes 执行程序运行作业时出错 - I have an error while running jobs with GItLab CI/CD kubernetes executor within the cluster 为什么在 Kube.netes 集群中部署时无法通过入口在浏览器中打开 React web 应用程序 - Why can I not open a React web app in a browser through an ingress when deploying it in a Kubernetes cluster 使用来自 GitLab 的运行器部署到 Kubernetes 集群 - Deploying to Kubernetes cluster using runner from GitLab 如何使用Kubernetes和Gitlab CI / CD在Google Cloud Platform中部署登台? - How to deploy staging in Google Cloud Platform with Kubernetes and Gitlab CI/CD? Gitlab 使用 Kube.netes 代理和私有容器注册表的 CI/CD - Gitlab CI/CD using Kubernetes agent and the private container registry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM