如何为 Cloud Build 用于 Cloud 运行 部署的 Cloud Storage 存储桶指定区域?

How can I specify a region for the Cloud Storage buckets used by Cloud Build for a Cloud Run deployment?

将 docker 容器映像部署到云 运行 时,我可以选择一个区域,这很好。 Cloud 运行 将构建委托给 Cloud Build,这显然创建了两个存储桶来实现这一点。意外行为是存储桶未在云 运行 部署区域创建,而是默认为美国多区域。

如何将区域指定为 "us-east1" 以便存储成本由 "always free" 层吸收?(显然是美国多区域存储存储桶将数据存储在免费套餐限制之外的区域,这导致了意外账单 - 我正在努力避免该账单。)

如果重要的话,我也在这个项目中使用了Firebase。我在 us-east1 区域创建了 Firebase 默认存储桶,希望它也可以成为其他存储桶的默认存储桶,但事实并非如此。最终的存储桶列表如下所示,您可以在其中看到使用不需要的多区域设置自动创建的两个存储桶。

这是我用来构建和部署的 shell 脚本:

#!/bin/sh

project_id=
service_id=

if [ -z "$project_id" ]; then
    echo "First argument must be the Google Cloud project ID" >&2
    exit 1
fi

if [ -z "$service_id" ]; then
    echo "Second argument must be the Cloud Run app name" >&2
    exit 1
fi

echo "Deploying $service_id to $project_id"

tag="gcr.io/$project_id/$service_id"

gcloud builds submit \
    --project "$project_id" \
    --tag "$tag" \
&& \
gcloud run deploy "$service_id" \
    --project "$project_id" \
    --image "$tag" \
    --platform managed \
    --update-env-vars "GOOGLE_CLOUD_PROJECT=$project_id" \
    --region us-central1 \
    --allow-unauthenticated

正如您提到的,Cloud Build 创建了一个或多个具有多区域的存储桶,因为在 Cloud 运行 中创建服务时,只添加了部署服务所需的标志和参数。

命令 gcloud builds submitdocumentation 提到标志 --gcs-source-staging-dir 的以下内容:

--gcs-source-staging-dir=GCS_SOURCE_STAGING_DIR

A directory in Google Cloud Storage to copy the source used for staging the build. If the specified bucket does not exist, Cloud Build will create one. If you don't set this field, gs://[PROJECT_ID]_cloudbuild/source is used.

由于未设置此标志,因此在 multi-regionus 中创建了存储桶。此行为也适用于标志 --gcs-log-dir.

现在,在您想要的双区域、区域或多区域中使用存储桶的必要步骤是使用 cloudbuild.yaml 并使用标志 --gcs-source-staging-dir。您可以执行以下操作:

  1. 在您可能需要的区域、双区域或多区域中创建存储桶。例如,我在 australia-southeast1.
  2. 中创建了一个名为“example-bucket”的存储桶
  3. 创建一个 cloudbuild.yaml 文件。如前所述 here,这是将构建的工件存储在您想要的存储桶中所必需的。举例如下:
steps:
- name: 'gcr.io/cloud-builders/gcloud'
    args:
    - 'run'
    - 'deploy'
    - 'cloudrunservice'
    - '--image'
    - 'gcr.io/PROJECT_ID/IMAGE'
    - '--region'
    - 'REGION_TO_DEPLOY'
    - '--platform'
    - 'managed'
    - '--allow-unauthenticated'
artifacts:
    objects:
    location: 'gs://example-bucket'
    paths: ['*']
  1. 最后你可以运行以下命令:
gcloud builds submit --gcs-source-staging-dir="gs://example-bucket/cloudbuild-custom" --config cloudbuild.yaml

前面提到的步骤可以适应您的脚本。请试一试:) 你会发现即使 Cloud 运行 服务部署在亚洲、欧洲或美国,之前指定的存储桶也可以在其他位置。

看起来只有按照您在评论中提到的操作才能做到这一点:

  1. us-east1中创建一个存储桶作为源桶($SOURCE_BUCKET);
  2. us-east1 中创建一个 Artifact Registry 存储库;
  3. 创建以下内容cloudbuild.yaml
    steps:
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '-t', 'us-east1-docker.pkg.dev/$PROJECT_ID/my-repo/my-image', '.']
    images:
    - 'us-east1-docker.pkg.dev/$PROJECT_ID/my-repo/my-image'
    
  4. 部署:
    $ gcloud builds submit --config cloudbuild.yaml --gcs-source-staging-dir=gs://$SOURCE_BUCKET/source
    

此处有更多详细信息:https://cloud.google.com/artifact-registry/docs/configure-cloud-build

我认为至少应该可以使用 --tag 选项指定 Artifact Registry 存储库并自动创建它,但它目前拒绝任何不是 gcr.io 的域.