cloudbuild.yaml 文件中的 env 步骤参数不是设置环境变量

env step parameter in cloudbuild.yaml file not settings environment variable

我的 cloudbuild.yaml 文件看起来像

steps:
  # build the container image
  - name: "gcr.io/cloud-builders/docker"
    args: ["build", "-t", "gcr.io/$PROJECT_ID/backend:$COMMIT_SHA", "."]
    env:
      - "APP_ENV=production"
  # push the container image to Container Registry
  - name: "gcr.io/cloud-builders/docker"
    args: ["push", "gcr.io/$PROJECT_ID/backend:$COMMIT_SHA"]
  # Deploy container image to Cloud Run
  - name: "gcr.io/cloud-builders/gcloud"
    args:
      - "run"
      - "deploy"
      - "backend"
      - "--image"
      - "gcr.io/$PROJECT_ID/backend:$COMMIT_SHA"
      - "--region"
      - "us-central1"
      - "--platform"
      - "managed"
images:
  - "gcr.io/$PROJECT_ID/backend:$COMMIT_SHA"

它构建了一个新容器并将其部署到云 运行,但是它没有将 APP_ENV 环境变量设置为 "production"。为什么会这样,我该如何获取它?

我正在关注 this guide

steps:
- env: [...]

approach 为运行 docker build -t 命令的 Cloud Build 容器设置环境变量,因此在这种情况下,只有 docker build 它执行获取 APP_ENV 变量(并且可能不执行任何东西)。

您不应期望这会为 Cloud 运行 设置环境变量。为此,您需要在 gcloud run deploy 步骤中指定 --set-env-vars--update-env-vars 到 Cloud 运行,方法是在上面指定额外的 args,例如:

  - name: "gcr.io/cloud-builders/gcloud"
    args:
      - "run"
      - "deploy"
      ...
      - "--set-env-vars=KEY1=VALUE1"
      - "--set-env-vars=KEY2=VALUE2"
      ...

请参阅 https://cloud.google.com/run/docs/configuring/environment-variables#command-line to learn more or read this article 了解为云 运行 应用程序指定环境变量的替代方法。