简体   繁体   English

Cloud Run Build 期间未定义环境变量

[英]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 日志中的错误示例

在此处输入图像描述 (logs are shown from bottom to top) (日志从下到上显示)

You are mixing context here.你在这里混合上下文。

There are 3 contexts that you need to be aware of.您需要注意 3 个上下文。

  1. The observer that launches the Cloud Build process based on Git push.基于 Git 推送启动 Cloud Build 流程的观察者。
  2. The Cloud Build job is triggered by the observer, and it's executed on a sandboxed environment, it's a build process. Cloud Build 作业由观察者触发,并在沙盒环境中执行,这是一个构建过程。 A step/command fails in this step, because for this context you have not defined the ENV variables.步骤/命令在此步骤中失败,因为对于此上下文,您尚未定义 ENV 变量。 When the build is finished, it places the image to GCR repository.构建完成后,它将图像放置到 GCR 存储库。
  3. Then "the image" is taken and used by Cloud Run as a service, here you define the ENV variables for the service itself, for your application code and not for your build process.然后“图像”被 Cloud Run 作为服务获取并使用,在这里您可以为服务本身、您的应用程序代码而不是您的构建过程定义 ENV 变量。

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.

相关问题 Next.js 部署到 Google Cloud Run 时未定义的环境变量 - Environment variables undefined on Next.js deployment to Google Cloud Run 谷歌云构建和部署到 Kubernetes 环境变量 - Google Cloud Build and Deploy to Kubernetes environment variables 通过 vite-react 和 GCP Cloud Build 访问环境变量 - Access Environment Variables through vite-react and GCP Cloud Build Terraform - 云运行列表中的多个环境变量 - Terraform - cloud run multiple environment variables from list 是否可以在 Google Cloud Run 中添加依赖环境变量? - Is it possible to add dependent environment variables in Google Cloud Run? 如何在 Google App Engine 标准环境中使用 Google Cloud Build 或其他方法设置环境变量? - How to set environment variables using Google Cloud Build or other method in Google App Engine Standard Environment? 根据另一个环境变量设置 Google Cloud Run 环境变量值 - Set Google Cloud Run environment variables value based on another environment variable app.yaml env_variables 在云构建步骤中作为 envs 访问 - app.yaml env_variables accessible as envs during cloud build steps 部署到云运行时出现云构建错误 - Cloud build error when deploying to cloud run 在云构建中运行 gcloud 脚本 - run gcloud script in cloud build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM