快速入门:使用 gcloud CLI 部署 Cloud Run functions 函数
本页介绍了如何使用 gcloud CLI 部署 HTTP Cloud Run functions 函数。
准备工作
- 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.
-
Install the Google Cloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
Create or select 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs:
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.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
Install the Google Cloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
Create or select 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs:
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.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com - 要为 Cloud Run 服务设置默认项目,请使用以下命令:
将 PROJECT_ID 替换为您为此快速入门创建的项目的名称。gcloud config set project PROJECT_ID
- 查看 Cloud Run 价格或使用价格计算器估算费用。
- 项目的 Cloud Run Source Developer (
roles/run.sourceDeveloper) 角色 -
服务身份的 Service Account User (
roles/iam.serviceAccountUser) 角色 -
项目的 Logs Viewer (
roles/logging.viewer) 角色 创建名为
helloworld的新目录,并转到此目录中:mkdir helloworld cd helloworld在
helloworld目录中创建一个package.json文件以指定 Node.js 依赖项:使用以下 Node.js 示例在
helloworld目录中创建一个index.js文件:创建名为
helloworld的新目录,并转到此目录中:mkdir helloworld cd helloworld在
helloworld目录中创建一个requirements.txt文件,以指定 Python 依赖项:这会添加示例所需的软件包。
使用以下 Python 示例在
helloworld目录中创建一个main.py文件:创建名为
helloworld的新目录,并转到此目录中:mkdir helloworld cd helloworld创建
go.mod文件以声明 go 模块:使用以下 Go 代码示例在
helloworld目录中创建一个hello_http.go文件:创建名为
helloworld的新目录,并转到此目录中:mkdir helloworld cd helloworld创建以下项目结构以包含源目录和源文件:
mkdir -p ~/helloworld/src/main/java/functions touch ~/helloworld/src/main/java/functions/HelloWorld.java使用以下 Java 代码示例更新
HelloWorld.java文件:在
helloworld目录中创建一个pom.xml文件,并添加以下 Java 依赖项:
所需的角色
如需获得完成本教程所需的权限,请让您的管理员为您授予以下 IAM 角色:
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
授予 Cloud Build 服务账号对项目的访问权限
除非您替换此行为,否则 Cloud Build 会自动使用 Compute Engine 默认服务账号作为默认 Cloud Build 服务账号来构建源代码和 Cloud Run 资源。
为了让 Cloud Build 能够构建来源,请向 Cloud Build 服务账号授予项目的 Cloud Run Builder (roles/run.builder) 角色:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \ --role=roles/run.builder
将 PROJECT_ID 替换为您的 Google Cloud项目 ID,并将 SERVICE_ACCOUNT_EMAIL_ADDRESS 替换为 Cloud Build 服务账号的邮箱。如果您使用 Compute Engine 默认服务账号作为 Cloud Build 服务账号,则服务账号邮箱应采用以下格式:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
将 PROJECT_NUMBER 替换为您的 Google Cloud项目编号。
如需详细了解如何查找项目 ID 和项目编号,请参阅创建和管理项目。
授予 Cloud Run Builder 角色之后,需要几分钟时间才能完成传播。
编写示例函数
如需编写应用,请按照以下步骤操作: