使用 Cloud Code for VS Code 部署 Cloud Run 服务
本文档介绍如何开始使用 Cloud Code。
您将使用示例“Hello, world”应用创建服务,使用 Cloud Code 公开部署服务,并查看实时服务资源的状态。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.- 安装 Git。 将示例复制到您的机器需要使用 Git。
- 安装 Docker。 构建示例代码需要使用 Docker。确保您的本地用户账号 能够构建容器。
- 如果您尚未安装 Cloud Code 插件 ,请进行安装。
所需的角色
如需获得完成本快速入门所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:
-
Cloud Run Admin (
roles/run.admin) -
Cloud Run Builder (
roles/run.builder) -
Logs Viewer (
roles/logging.viewer) -
Service Account User (
roles/iam.serviceAccountUser)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
创建服务
如需创建示例服务应用,请按照以下步骤操作:
在 Cloud Code 状态栏中,点击活跃项目名称。

在显示的快速选择菜单中,选择新应用 。
选择 Cloud Run 应用 作为应用类型。
从示例应用列表中,选择一种您偏好的语言。
例如,选择 Node.js:Cloud Run 会在 Node.js 中创建一个“Hello, world”服务应用。
确认服务源代码的文件系统位置(或根据需要进行修改),然后点击新建应用 。
系统会在 VS Code 中打开所选服务的
README.md文件预览。
部署到 Cloud Run
如需将服务部署到 Cloud Run,请按照以下步骤操作:
打开 Command Palette(按
Ctrl/Cmd+Shift+P或点击查看 > Command Palette ),然后运行部署到 Cloud Run 命令。在部署到 Cloud Run 窗格中,设置您的 Google Cloud 项目。
接受其余的默认设置,然后点击部署 。
Cloud Code 会构建映像,将其推送到注册表,并将您的服务部署到 Cloud Run。
如需查看正在运行的服务,请打开部署到 Cloud Run 对话框顶部显示的网址。
查看资源状态
您可以使用 Cloud Run 服务浏览器查看 Cloud Run 服务的状态:
- 在 VS Code 活动栏中,点击
Cloud Code 。
- 展开 Cloud Run 部分以查看您的 Cloud Run 服务。
- 点击服务以查看其属性。
查看日志
您还可以使用 Cloud Code 附带的日志查看器查看来自已部署服务的日志。
如需查看日志,请按以下步骤操作:
- 在 VS Code 活动栏中,点击
Cloud Code 。
- 展开 Cloud Run 部分以查看您的 Cloud Run 服务。
- 右键点击您的 Cloud Run 服务,然后点击查看日志 。
清理
如需删除项目,请执行以下操作:
前往 控制台中的“项目”页面: Google Cloud
选择您为本快速入门创建的项目,然后点击删除。
输入项目 ID 进行确认,然后点击关停。
这将关闭该项目并安排将其删除。
如果您使用的是现有项目,并希望删除您为本快速入门创建的资源,请以下说明操作:
后续步骤
- 探索 Cloud Code 提供的特定语言的调试支持。
- 通过配置相关设置来自定义您的 Cloud Code 体验。
- 尝试使用 Cloud Code 创建并运行 Kubernetes 应用。