Google Cloud Build - 为 Dockerfile 传递环境变量

Google Cloud Build - Pass environment variable for Dockerfile

我正在尝试将我的 Angular 云应用程序停靠 运行,然后根据环境变量有条件地使用生产或开发配置构建它。

云构建命令:

gcloud builds submit --tag gcr.io/project-id/image-id --timeout=1200

Dockerfile:

FROM node:14

WORKDIR usr/src/app

COPY package*.json ./

RUN npm install

# Copy local code to the container
COPY . .

# Build app
RUN if [ "$ENV" = "development" ] ; then npm run build-dev:ssr ; else npm run build-prod:ssr ; fi

CMD ["npm", "run", "serve:ssr"]

目前 $ENV 不存在,但是有没有办法通过 gcloud build submit 命令传递它?

您不能像使用 docker 构建命令那样传递构建参数。为此,您需要创建一个简单的 Cloud Build 文件

steps:
  - name: 'gcr.io/cloud-builders/docker'
    args: [ 'build', '-t', 'gcr.io/project-id/image-id', '--build-arg=ENV=$_MY_VARIABLE', '.' ]
  # push the container image to Container Registry
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/project-id/image-id']
images:
  - 'gcr.io/project-id/image-id'
timeout: 1200s
substitutions:
  _MY_VARIABLE: default value if not passed in the build command

而且运行像这样

gcloud builds submit --substitutions=_MY_VARIABLE=specific_value