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

时间:2020-05-21 22:12:57

标签: google-cloud-platform google-cloud-build google-cloud-run

我的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"

,它将构建新容器并将其部署到Cloud Run,但是没有将APP_ENV环境变量设置为"production"。为什么会这样,怎么去?

我正在关注this guide

1 个答案:

答案 0 :(得分:1)

steps:
- env: [...]

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

您不应期望它为Cloud Run设置环境变量。为此,您需要在--set-env-vars步骤中通过在上面指定其他--update-env-vars来指定gcloud run deployargs到Cloud Run,例如:

  - 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,以了解更多信息或阅读this article,以了解为Cloud Run应用程序指定环境变量的替代方法。