快速入门:使用 gcloud CLI 将函数部署到 Cloud Run
本页介绍了如何使用 Cloud Run 通过 gcloud CLI 部署 HTTP 函数。
准备工作
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 要为 Cloud Run 服务设置默认项目,请使用以下命令:
将 PROJECT_ID 替换为您为此快速入门创建的项目的名称。gcloud config set project PROJECT_ID
- 向 Cloud Build 服务账号授予以下 IAM 角色。
点击查看 Cloud Build 服务账号需要的角色
除非您替换此行为,否则 Cloud Build 会自动使用 Compute Engine 默认服务账号作为默认 Cloud Build 服务账号来构建源代码和 Cloud Run 资源。为了让 Cloud Build 能够构建来源,请让您的管理员向项目的 Compute Engine 默认服务账号授予 Cloud Run Builder (
roles/run.builder
) 角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/run.builder
将
PROJECT_NUMBER
替换为您的 Google Cloud项目编号,将PROJECT_ID
替换为您的 Google Cloud项目 ID。如需详细了解如何查找项目 ID 和项目编号,请参阅创建和管理项目。向 Compute Engine 默认服务账号授予 Cloud Run 构建器角色需要几分钟时间才能传播。
编写示例函数
如需编写应用,请按照以下步骤操作:
Node.js
创建名为
helloworld
的新目录,并转到此目录中:mkdir helloworld cd helloworld
在
helloworld
目录中创建一个package.json
文件以指定 Node.js 依赖项:使用以下 Node.js 示例在
helloworld
目录中创建一个index.js
文件:
Python
创建名为
helloworld
的新目录,并转到此目录中:mkdir helloworld cd helloworld
在
helloworld
目录中创建一个requirements.txt
文件,以指定 Python 依赖项:这会添加示例所需的软件包。
使用以下 Python 示例在
helloworld
目录中创建一个main.py
文件:
Go
创建名为
helloworld
的新目录,并转到此目录中:mkdir helloworld cd helloworld
创建
go.mod
文件以声明 go 模块:使用以下 Go 代码示例在
helloworld
目录中创建一个hello_http.go
文件:
Java
创建名为
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 依赖项:
Ruby
创建名为
helloworld
的新目录,并转到此目录中:mkdir helloworld cd helloworld
创建名为
app.rb
的文件,并将以下代码粘贴到其中:创建名为
Gemfile
的文件,并将以下内容复制到其中:如果您未安装 Bundler 2.0 或更高版本,请安装 Bundler。
通过运行以下命令生成
Gemfile.lock
文件:bundle install
PHP
创建名为
helloworld
的新目录,并转到此目录中:mkdir helloworld cd helloworld
创建名为
index.php
的文件,并将以下代码粘贴到其中:如果您不使用 Cloud Shell,请创建一个
composer.json
文件,并将以下代码粘贴到其中:
.NET
安装 .NET SDK 6.0。 本快速入门仅适用于 .NET 版本 6。
在 Console 中,使用 dotnet 命令新建一个空 Web 项目。
dotnet new web -o helloworld-csharp
转到
helloworld-csharp
目录:将项目文件
helloworld-csharp.csproj
中的示例代码替换为以下代码:将
Program.cs
文件中的示例代码替换为以下代码:
部署该函数
重要提示:本快速入门假定您在快速入门中使用的项目中拥有所有者或编辑者角色。否则,请参阅 Cloud Run Source Developer 角色,了解从源代码部署 Cloud Run 资源所需的权限。
如需部署 Cloud Run 函数,请按以下步骤操作:
在包含示例代码的目录中运行以下命令,以部署函数:
Node.js
gcloud beta run deploy nodejs-http-function \ --source . \ --function helloGET \ --base-image nodejs20 \ --region REGION \ --allow-unauthenticated
将 REGION 替换为要在其中部署函数的服务的 Google Cloud 区域。例如
us-central1
。Python
gcloud beta run deploy python-http-function \ --source . \ --function hello_get \ --base-image python312 \ --region REGION \ --allow-unauthenticated
将 REGION 替换为要在其中部署函数的服务的 Google Cloud 区域。例如
us-central1
。Go
gcloud beta run deploy go-http-function \ --source . \ --function HelloGet \ --base-image go122 \ --region REGION \ --allow-unauthenticated
将 REGION 替换为要在其中部署函数的服务的 Google Cloud 区域。例如
us-central1
。Java
在包含
pom.xml
文件的目录中运行以下命令:gcloud beta run deploy java-http-function \ --source . \ --function functions.HelloWorld \ --base-image java21 \ --region REGION \ --allow-unauthenticated
将 REGION 替换为要在其中部署函数的服务的 Google Cloud 区域。例如
us-central1
。Ruby
gcloud beta run deploy ruby-http-function \ --source . \ --function hello_get \ --base-image ruby33 \ --region REGION \ --allow-unauthenticated
将 REGION 替换为要在其中部署函数的服务的 Google Cloud 区域。例如
us-central1
。PHP
gcloud beta run deploy php-http-function \ --source . \ --function helloGet \ --base-image php83 \ --region REGION \ --allow-unauthenticated
将 REGION 替换为要在其中部署函数的服务的 Google Cloud 区域。例如
us-central1
。.NET
gcloud beta run deploy csharp-http-function \ --source . \ --function HelloWorld.Function \ --base-image dotnet6 \ --region REGION \ --allow-unauthenticated
将 REGION 替换为要在其中部署函数的服务的 Google Cloud 区域。例如
us-central1
。部署完成后,Google Cloud CLI 会显示一个运行服务的网址。在浏览器中打开该网址,查看函数的输出。
如需了解如何将 Eventarc 触发器添加到函数,请参阅部署函数指南以查看相关说明。
清理
虽然 Cloud Run 不会对未在使用中的服务计费,但您可能仍然需要支付将容器映像存储在 Artifact Registry 中而产生的相关费用。为避免产生费用,您可以删除容器映像或删除 Google Cloud 项目。删除 Google Cloud 项目后,系统会停止对该项目中使用的所有资源计费。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
后续步骤
如需使用 Google Cloud 控制台将示例函数部署到 Cloud Run,请参阅快速入门:使用 Google Cloud 控制台将函数部署到 Cloud Run。
如需使用 Google Cloud 控制台和 Google Cloud CLI 部署函数和创建触发器,请参阅部署函数。
如需查看和删除现有函数,请参阅管理服务修订版本。
如需在您自己的工具链中构建函数容器并将其部署到 Cloud Run,请参阅构建函数。