快速入门:从 Git 代码库部署到 Cloud Run

本页面介绍如何从 Git 代码库持续部署到 Cloud Run。


如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


如需了解此过程的完整说明,请参阅使用 Cloud Build 通过 Git 进行持续部署

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Make sure that you have the following role or roles on the project: Cloud Run Developer, Cloud Run Source Developer, Cloud Run Admin, Service Account User, Service Usage Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。
  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Make sure that you have the following role or roles on the project: Cloud Run Developer, Cloud Run Source Developer, Cloud Run Admin, Service Account User, Service Usage Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。
  8. Enable the Cloud Build API.

    Enable the API

  9. 如果您通过网域限制组织政策来限制项目的未经身份验证的调用,则您需要按照测试专用服务中的说明访问已部署的服务。

基于模板创建代码库

如需创建代码库,请执行以下操作:

  1. 前往模板代码库:

  2. 点击使用此模板

  3. 选择创建新代码库

  4. 在表单中输入代码库名称

  5. 点击基于模板创建代码库

从代码库部署

如需从代码库中部署,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往 Cloud Run 页面。

    转到 Cloud Run

  2. 从菜单中选择服务,然后点击部署容器

  3. 创建服务中,确保选择从代码库持续部署选项。

  4. 在表单中,点击设置 Cloud Build

  5. 在右侧面板中:

    1. 在“代码库”下,选择新创建的代码库。如果您尚未进行身份验证,则可能需要进行身份验证。请使用代码库提供方字段下的身份验证链接。

    2. 查看有关 GitHub 和 Google Cloud 互动的确认协议。

    3. 点击下一步

    4. Build 类型下,选择 Google Cloud Buildpack

    5. 点击保存

  6. 创建服务表单中:

    1. 确认服务的名称。系统会自动填充代码库名称。

    2. 区域下拉菜单中,选择您希望服务所在的区域

    3. 身份验证下,选择允许未经过身份验证的调用。如果您没有选择此选项的权限(Cloud Run Admin 角色),系统会部署服务并要求进行身份验证。

    4. 点击创建以将示例代码库部署到 Cloud Run,然后等待部署完成。

  7. 点击显示的网址链接,以运行已部署的容器。

Cloud Run 位置

Cloud Run 是区域级的,这意味着运行 Cloud Run 服务的基础架构位于特定区域,并且由 Google 代管,以便在该区域内的所有可用区以冗余方式提供。

选择用于运行 Cloud Run 服务的区域时,主要考虑该区域能否满足您的延迟时间、可用性或耐用性要求。通常,您可以选择距离用户最近的区域,但除此之外,您还应该考虑 Cloud Run 服务使用的其他 Google Cloud产品的位置。跨多个位置使用 Google Cloud 产品可能会影响服务的延迟时间和费用。

Cloud Run 可在以下区域使用:

基于层级 1 价格

基于层级 2 价格

如果您已创建 Cloud Run 服务,可在Google Cloud 控制台的 Cloud Run 信息中心内查看区域。

更新服务

如需更改服务,请执行以下操作:

  1. 将提交推送到代码库的主分支。

  2. 刷新 Cloud Run 服务以查看更新的状态。

Cloud Run 会根据需求自动扩缩容器实例,您只需为处理请求期间所消耗的 CPU、内存和网络付费。

后续步骤