[英]Environment variables are undefined during Cloud Run Build
I use Google Cloud Run to containerize the node.js app.我使用 Google Cloud Run 来容器化 node.js 应用程序。 I added environment variables to the google cloud run by following this guide and expect to use them inside my application code.
我按照本指南将环境变量添加到谷歌云运行,并希望在我的应用程序代码中使用它们。 But.
但。 Whenever I run build (cloud run build) it shows me that
process.env.NODE_ENV
and other enviroenment variables are undefined
.每当我运行构建(云运行构建)时,它都会显示
process.env.NODE_ENV
和其他环境变量是undefined
的。
Could you help me to find the root problem of the issue?你能帮我找到问题的根本问题吗?
Dockerfile Dockerfile
FROM node:14.16.0
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
# Copy local code to the container image.
COPY . ./
RUN yarn install
RUN yarn build
RUN npx knex --knexfile=./src/infrastructure/knex/knex.config.ts migrate:latest --env production
# Use the official lightweight Node.js 14 image.
# https://hub.docker.com/_/node
FROM node:14.16.0
# Create and change to the app directory.
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# Copying this first prevents re-running npm install on every code change.
COPY package.json yarn.lock ./
# Install production dependencies.
# If you add a package-lock.json, speed your build by switching to 'npm ci'.
# RUN npm ci --only=production
RUN yarn install --production --frozen-lockfile
COPY --from=0 /usr/src/app/dist ./dist
EXPOSE 8080
# Run the web service on container startup.
CMD [ "yarn", "prod" ]
This line of code throws error这行代码抛出错误
RUN npx knex --knexfile=./src/infrastructure/knex/knex.config.ts migrate:latest --env production
This is knex.config.ts
这是
knex.config.ts
import 'dotenv/config'
import { Knex } from 'knex'
import { envConfig, NodeEnvEnum } from '../../configs/env.config'
console.log('ASDASD', process.env.NODE_ENV, envConfig.environment, process.env.CLOUD_SQL_CONNECTION_NAME, envConfig.databaseCloudSqlConnection)
export const knexConfig: Record<NodeEnvEnum, Knex.Config> = {
[NodeEnvEnum.Development]: {
client: 'pg',
connection: envConfig.databaseUrl,
migrations: {
extension: 'ts'
}
},
[NodeEnvEnum.Production]: {
client: 'pg',
connection: {
database: envConfig.databaseName,
user: envConfig.databaseUser,
password: envConfig.databasePassword,
host: `/cloudsql/${envConfig.databaseCloudSqlConnection}`
}
}
}
export default knexConfig
This is env.config.ts
这是
env.config.ts
export enum NodeEnvEnum {
Production = 'production',
Development = 'development'
}
interface EnvConfig {
serverPort: string
environment: NodeEnvEnum
// Database
databaseCloudSqlConnection: string
databaseUrl: string
databaseUser: string
databasePassword: string
databaseName: string
}
export const envConfig: EnvConfig = {
serverPort: process.env.SERVER_PORT as string,
environment: process.env.NODE_ENV as NodeEnvEnum,
// Database
databaseUrl: process.env.DATABASE_URL as string,
databaseCloudSqlConnection: process.env.CLOUD_SQL_CONNECTION_NAME as string,
databaseName: process.env.DB_NAME as string,
databaseUser: process.env.DB_USER as string,
databasePassword: process.env.DB_PASSWORD as string
}
Example of the error from the Cloud Run logs Cloud Run 日志中的错误示例
You are mixing context here.你在这里混合上下文。
There are 3 contexts that you need to be aware of.您需要注意 3 个上下文。
In Context 2, you need to end up using substitution variables read more here and here .在上下文 2 中,您最终需要使用替换变量,请在此处和此处阅读更多内容。
I had the same problem and the cause turned out to be that my .env
files weren't getting copied into the Docker container upon deployment.我遇到了同样的问题,原因是我的
.env
文件在部署时没有被复制到 Docker 容器中。 Fixed it by adding .gcloudignore
and .dockerignore
files in the root of the repository.通过在存储库的根目录中添加
.gcloudignore
和.dockerignore
文件来修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.