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

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

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

准备工作

  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.

    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. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  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. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  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 插件,请安装该插件。
  13. 所需的角色

    如需获得完成本快速入门所需的权限,请让您的管理员为您授予项目的以下 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 或点击 View [视图] > Command Palette [命令面板]),然后运行 Deploy to 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 进行确认,然后点击关停

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

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

后续步骤