简体   繁体   中英

Environment variables are undefined during Cloud Run Build

I use Google Cloud Run to containerize the node.js app. 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 .

Could you help me to find the root problem of the issue?

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

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

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

在此处输入图像描述 (logs are shown from bottom to top)

You are mixing context here.

There are 3 contexts that you need to be aware of.

  1. The observer that launches the Cloud Build process based on Git push.
  2. The Cloud Build job is triggered by the observer, and it's executed on a sandboxed environment, it's a build process. A step/command fails in this step, because for this context you have not defined the ENV variables. When the build is finished, it places the image to GCR repository.
  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.

In Context 2, you need to end up using substitution variables read more here and here .

在此处输入图像描述

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. Fixed it by adding .gcloudignore and .dockerignore files in the root of the repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM