使用 Cloud Build 部署到 Cloud Run

本页面介绍了如何使用 Cloud Build 自动部署 Cloud Run 服务。如果您是刚接触 Cloud Build,请先阅读快速入门build 配置概览

借助 Cloud Run,您可以在无服务器环境中运行无状态映像。使用 Cloud Build,您可以将 Artifact Registry 中的映像部署到 Cloud Run。您可以部署现有映像、构建和部署映像,或自动执行部署。

准备工作

  • Enable the Cloud Build, Cloud Run, Artifact Registry, and Resource Manager APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

所需的 Identity and Access Management 权限

  1. 在 Google Cloud 控制台中,前往 Cloud Build 权限页面:

    前往权限

  2. 对于指定的 Cloud Build 服务账号默认的 Cloud Build 服务账号,请将以下角色的状态设置为已启用

    • Cloud Run Admin (roles/run.admin) | 使 Cloud Build 能够将新服务部署到 Cloud Run。
    • Storage Admin (roles/storage.admin) | 允许从 Cloud Storage 读取和写入数据。
    • Artifact Registry Writer (roles/artifactregistry.writer) | 允许从 Artifact Registry 拉取映像以及向 Artifact Registry 写入映像。
    • 日志写入者 (roles/logging.logWriter) | 允许将日志条目写入 Cloud Logging。
    • Cloud Build Editor (roles/cloudbuild.builds.editor) | 允许您的服务账号运行 build。

构建和部署映像

Cloud Build 可让您构建映像,将构建的映像存储到 Artifact Registry 中,然后将映像部署到 Cloud Run。

如需构建和部署映像,请执行以下操作:

  1. 在项目根目录中,创建一个名为 cloudbuild.yaml 的配置文件。

  2. 在构建配置文件中,添加 docker 构建步骤来构建映像并将其推送到 Artifact Registry,然后添加 gcloud 构建步骤来调用 gcloud run deploy 命令,以便在 Cloud Run 上部署该映像:

    steps:
    # Build the image
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '-t', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE', '.']
    # Push the image to Artifact Registry
    - name: 'gcr.io/cloud-builders/docker'
      args: ['push', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE']
    # Deploy image to Cloud Run
    - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
      entrypoint: gcloud
      args: ['run', 'deploy', 'SERVICE_NAME', '--image', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE', '--region', 'SERVICE_REGION']
    images:
    - 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE'
    

    其中:

    • REPOSITORY 是您从中部署映像的 Artifact Registry 代码库的名称。
    • LOCATION 是 Artifact Registry 代码库的位置,例如 us-east1
    • PROJECT_ID 是存储映像的 Google Cloud项目 ID。
    • SERVICE_NAME 是 Cloud Run 服务的名称。
    • SERVICE_REGION 是要部署的 Cloud Run 服务的区域。
    • IMAGE 是 Artifact Registry 中的映像名称。
  3. 前往项目根目录,然后运行以下命令,其中 LOCATION 是用于运行 build 的受支持的 build 区域之一:

     gcloud builds submit --region=LOCATION
    

成功完成后,系统会显示一条成功消息以及已部署服务的网址。

持续部署

通过创建 Cloud Build 触发器,您可以将软件自动部署到 Cloud Run。您可以将触发器配置为每次您在更新源代码时都会构建和部署映像。

如需自动执行部署,请执行以下操作:

  1. 在您的代码库根目录中,添加一个名为 cloudbuild.yaml 的配置文件,其中包含一些步骤,用于构建映像,接着将映像推送到 Artifact Registry,然后调用 gcloud run deploy 命令:

    steps:
    # Build the image
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '-t', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA', '.']
    # Push the image to Artifact Registry
    - name: 'gcr.io/cloud-builders/docker'
      args: ['push', 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA']
    # Deploy image to Cloud Run
    - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
      entrypoint: gcloud
      args: 
        - 'run'
        - 'deploy'
        - 'SERVICE_NAME'
        - '--image'
        - 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA'
        - '--region'
        - 'SERVICE_REGION'
    images:
    - 'LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE/SERVICE_NAME:$COMMIT_SHA'
    

    其中:

    • REPOSITORY 是您从中部署映像的 Artifact Registry 代码库的名称。
    • LOCATION 是 Artifact Registry 代码库的位置,例如 us-east1
    • PROJECT_ID 是存储映像的 Google Cloud项目 ID。
    • SERVICE_NAME 是 Cloud Run 服务的名称。
    • SERVICE_REGION 是要部署的 Cloud Run 服务的区域。
    • IMAGE 是 Artifact Registry 中的映像名称。

    从 Git 代码库触发时,Cloud Build 会填充 $COMMIT_SHA 替换变量。

  2. 使用上一步中创建的配置文件创建一个构建触发器:

    1. 打开触发器页面

      转到“触发器”页面

    2. 点击创建触发器

    3. 名称字段中,为触发器输入名称。

    4. 区域下,为触发器选择区域

    5. 事件下方,选择要启动触发器的代码库事件。

    6. 来源下方,选择您的代码库以及用于启动触发器的分支或标记名称。如需详细了解如何指定要自动构建的分支,请参阅创建构建触发器

    7. 配置下,选择 Cloud Build 配置文件(YAML 或 JSON)

    8. Cloud Build 配置文件位置字段中的 / 后面,输入 cloudbuild.yaml

    9. 点击创建以保存您的构建触发器。

    现在,当您将新代码推送到代码库时,Cloud Build 会调用构建并部署服务到 Cloud Run。

如需详细了解如何创建 Cloud Build 触发器,请参阅创建和管理构建触发器

代码示例

以下是一些示例代码库,每个代码库都包含一个示例应用和一个将应用部署到 Cloud Run 的构建配置文件:

  • deploy-prebuilt:用于演示如何将预构建的映像部署到 Cloud Run 的代码示例。
  • run-example-builddeploy:演示了如何构建映像并将其部署到 Cloud Run 的代码示例。

后续步骤