使用 Cloud Code for VS Code 部署 Cloud Run 服务

本文档介绍如何开始使用 Cloud Code。

您将使用示例“Hello, world”应用创建服务,使用 Cloud Code 公开部署服务,并查看实时服务资源的状态。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Cloud Run API.

    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 API

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  8. Verify that billing is enabled for your Google Cloud project.

  9. Enable the Cloud Run API.

    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 API

  10. 安装 Git。 将示例复制到您的机器需要使用 Git。
  11. 安装 Docker。 构建示例代码需要使用 Docker。确保您的本地用户账号 能够构建容器。
  12. 如果您尚未安装 Cloud Code 插件 ,请进行安装。

所需的角色

如需获得完成本快速入门所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

创建服务

如需创建示例服务应用,请按照以下步骤操作:

  1. 在 Cloud Code 状态栏中,点击活跃项目名称。

    状态栏中的有效项目名称

  2. 在显示的快速选择菜单中,选择新应用

  3. 选择 Cloud Run 应用 作为应用类型。

  4. 从示例应用列表中,选择一种您偏好的语言。

    例如,选择 Node.js:Cloud Run 会在 Node.js 中创建一个“Hello, world”服务应用。

  5. 确认服务源代码的文件系统位置(或根据需要进行修改),然后点击新建应用

    系统会在 VS Code 中打开所选服务的 README.md 文件预览。

部署到 Cloud Run

如需将服务部署到 Cloud Run,请按照以下步骤操作:

  1. 打开 Command Palette(按 Ctrl/Cmd+Shift+P 或点击查看 > Command Palette ),然后运行部署到 Cloud Run 命令。

  2. 部署到 Cloud Run 窗格中,设置您的 Google Cloud 项目。

  3. 接受其余的默认设置,然后点击部署

    Cloud Code 会构建映像,将其推送到注册表,并将您的服务部署到 Cloud Run。

  4. 如需查看正在运行的服务,请打开部署到 Cloud Run 对话框顶部显示的网址。

查看资源状态

您可以使用 Cloud Run 服务浏览器查看 Cloud Run 服务的状态:

  1. 在 VS Code 活动栏中,点击 Cloud Code
  2. 展开 Cloud Run 部分以查看您的 Cloud Run 服务。
  3. 点击服务以查看其属性。

查看日志

您还可以使用 Cloud Code 附带的日志查看器查看来自已部署服务的日志。

如需查看日志,请按以下步骤操作:

  1. 在 VS Code 活动栏中,点击 Cloud Code
  2. 展开 Cloud Run 部分以查看您的 Cloud Run 服务。
  3. 右键点击您的 Cloud Run 服务,然后点击查看日志

清理

如需删除项目,请执行以下操作:

  1. 前往 控制台中的“项目”页面: Google Cloud

    转到“项目”页面

  2. 选择您为本快速入门创建的项目,然后点击删除

  3. 输入项目 ID 进行确认,然后点击关停

    这将关闭该项目并安排将其删除。

如果您使用的是现有项目,并希望删除您为本快速入门创建的资源,请以下说明操作:

  • 容器映像: 虽然 Cloud Run 不会对未在使用中的 服务计费,但您可能需要支付将容器映像存储在 Artifact Registry 中而产生的相关 费用。如需了解删除映像的步骤,请参阅 删除映像

  • Cloud Run 服务: 如需删除 Cloud Run 服务,请在 Google Cloud 控制台中使用导航菜单导航到 Cloud Run。选择要删除的服务帐号,然后点击删除

后续步骤