我有一个NextJS "^11.1.2“应用程序,它可以在Dockerfile中构建并通过CI/CD部署到生产中。但我的process.env
变量没有呈现。
在我的客户端代码中有这样的内容,应该在运行时呈现:
const PublicApiUrl = process.env.NEXT_PUBLIC_API_URL;
在我的(Gitlab) CI/CD管道中,我通过AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS添加了一些--build-args
,以及ENV
和ARG
AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS --build-arg=NEXT_PUBLIC_API_URL=https://my.api.com --build-arg=NEXT_PUBLIC_API_URL=https://my.api.com --build-arg=NEXT_PUBLIC_BUILDER_KEY=XXXXXX
NEXT_PUBLIC_API_URL=https://my.api.com
API_URL=https://my.api.com
Dockerfile
ARG API_URL
ENV API_URL=$API_URL
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_BUILDER_KEY
ENV NEXT_PUBLIC_BUILDER_KEY=$NEXT_PUBLIC_BUILDER_KEY
RUN npm run build # which resolves in "build": "next build"
下面的值肯定会被选中(我做了一个RUN env
,可以看到变量在那里)。
这是我在Kubernetes的configMap
,它将.env.local
文件挂载到容器中:
apiVersion: v1
kind: ConfigMap
metadata:
name: frontend-env-local
annotations:
"helm.sh/resource-policy": keep
data:
.env: |-
NEXT_PUBLIC_API_URL=https://my.api.url
API_URL=https://my.api.url
这是我的部署,它将configMap
作为.env.local
挂载到容器中。
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
labels:
app: frontend
spec:
replicas: 1
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
volumes:
- configMap:
defaultMode: 420
items:
- key: .env
path: .env.local
name: frontend-env-local
name: frontend-env-local
imagePullSecrets:
- name: gitlab-credentials
containers:
- name: frontend
image: "registry.gitlab.com/myapp:latest"
imagePullPolicy: Always
ports:
- name: http
containerPort: 5000
protocol: TCP
volumeMounts:
- mountPath: /app/.env.local
name: frontend-env-local
readOnly: true
subPath: .env.local
当我在本地构建next build
时,它可以工作,并且呈现我的变量。
但是,当我推送、构建和部署它并运行该应用程序时,它是一个空字符串:
const PublicApiUrl = "";
为什么NextJS不能识别变量?
我登录生产(Kubernetes pod)终端并运行env
。变量也是存在的。
知道为什么会这样吗?
发布于 2021-10-28 16:40:05
我还必须在next.config.js
中定义变量,如下所示:
module.exports = {
serverRuntimeConfig: {
API_URL: process.env.API_URL,
},
// Will be available on both server and client
publicRuntimeConfig: {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
}
}
在改变之后,似乎不需要configMap
或安装的卷.只有我的CI/CD中的--build-arg
和Dockerfile
中的ARG
和ENV
发布于 2021-10-29 16:38:02
您不需要在next.config.js
中定义变量--糟糕的实践。
分别定义env
文件
本地的:
您需要创建environments
文件夹,然后在该文件夹中创建.env.local
;在这里定义本地变量。完成此操作后,在package.json
文件中填充scripts
部分:
"scripts": {
//you need to add this line - defining your .env.local file inside
"local": "env-cmd -f environments/.env.local node server.js",
"build": "next build",
},
因此,在以npm命令作为local server
启动npm run local
之后
服务器的:
您需要在服务器上以某种方式定义environment variables
。关于vercel,在settings/environment variables
里
由于process.env.NEXT_PUBLIC_API_URL
文件通常被GitHub忽略,所以您“丢失”了GitHub的值--新的GitHub公开了环境变量。您的文件process.env.NEXT_PUBLIC_API_URL
丢失在服务器上,这就是为什么您有null
值。
发布于 2022-03-19 16:13:04
如果使用Docker
,在进行项目构建之前,请检查.env
文件是否在DockerFile中。
Example DockerFile:
FROM node:16.14.0-alpine3.14 as build
WORKDIR /app
# Copy in only the parts needed to install dependencies
# (This avoids rebuilds if the package.json hasn’t changed)
COPY package.json .
COPY package-lock.json .
COPY .env.production .
# Install dependencies (including dev dependencies)
RUN npm install
# Copy in the rest of the project
# (include node_modules in a .dockerignore file)
COPY . .
# Build the project
RUN npm run build
# Second stage: runtime
FROM node:16.14.0-alpine3.14
WORKDIR /app
ENV NODE_ENV=production
# Again get dependencies, but this time only install
# runtime dependencies
COPY package.json .
COPY package-lock.json .
RUN npm install --only=production
# Get the built application from the first stage
COPY --from=build /app/.next/ /app/.next/
# Set runtime metadata
ENV PORT 3000
EXPOSE 3000
https://stackoverflow.com/questions/69751831
复制相似问题