部署 Cloud Run 函数

本指南介绍了如何从源代码部署 Cloud Functions 函数。

部署过程会获取源代码和配置设置并构建可运行的映像,Cloud Functions 自动管理这一过程,以便处理对函数的请求。

部署基础知识

部署 Cloud Run functions 的用户必须具有 Cloud Functions Developer IAM 角色或具有提供相同权限的其他角色。另请参阅其他部署配置

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 使用 gcloud functions deploy 命令部署函数:

    gcloud functions deploy YOUR_FUNCTION_NAME \
     --gen2 \
     --region=YOUR_REGION \
     --runtime=YOUR_RUNTIME \
     --source=YOUR_SOURCE_LOCATION \
     --entry-point=YOUR_CODE_ENTRYPOINT \
     TRIGGER_FLAGS
    

    第一个参数 YOUR_FUNCTION_NAME 是已部署函数的名称。函数名称必须以字母开头,后面最多可跟 62 个字母、数字、连字符或下划线,但必须以字母或数字结尾。 为函数创建的 Cloud Run 服务的名称将将下划线替换为连字符,大写字母将转换为小写字母。例如,Function_1 将在 Cloud Run 中被命名为 function-1

    • --gen2 标志(可选)用于指定要部署到 Cloud Run functions。

    • --region 标志指定要在其中部署函数的区域。如需查看 Cloud Functions 支持的区域列表,请参阅位置

    • --runtime 标志指定函数使用的语言运行时。Cloud Functions 支持多种运行时;如需了解详情,请参阅运行时

    • --source 标志用于指定函数源代码的位置。如需了解详情,请参阅以下部分:

    • --entry-point 标志指定源代码中函数的入口点。这是在您的函数运行时执行的代码。此标志的值必须是源代码中存在的函数名称或完全限定类名称。如需了解详情,请参阅函数入口点

    • 如需为函数指定触发器,需要使用额外的标志(上文表示为 TRIGGER_FLAGS),具体取决于您要使用的触发器:

      触发器标志 触发器说明
      --trigger-http 通过 HTTP(S) 请求触发函数。如需了解详情,请参阅 HTTP 触发器
      --trigger-topic=YOUR_PUBSUB_TOPIC 在消息发布到指定的 Pub/Sub 主题时触发函数。如需了解详情,请参阅 Pub/Sub 触发器
      --trigger-bucket=YOUR_STORAGE_BUCKET 在指定的 Cloud Storage 存储桶中创建或覆盖对象时触发函数。如需了解详情,请参阅 Cloud Storage 触发器
      --trigger-event-filters=EVENTARC_EVENT_FILTERS 当与指定过滤条件匹配的事件发生时,通过 Eventarc 触发函数。如需了解详情及更多选项,请参阅 Eventarc 触发器

      您可以视需要在部署函数时指定其他配置网络安全选项。

      如需查看有关部署命令及其标志的完整参考信息,请参阅 gcloud functions deploy 文档。

      如需了解一些示例部署命令,请参阅命令行示例

部署成功完成后,Google Cloud 控制台的 Cloud Run 概览页面中会显示带有绿色对勾标记的函数。

函数的初始部署可能需要几分钟时间,系统会同时对底层基础架构进行预配。重新部署现有函数时速度会更快,并且传入的流量会自动迁移到最新版本。

从本地机器部署

本部分介绍如何从位于本地机器的源代码部署函数。

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 使用 gcloud functions deploy 命令,按照上面的部署说明执行操作。

    对于 --source 标志,请指定函数源代码根目录的本地文件系统路径;有关详情,请参阅源目录结构。如果省略此标志,系统会使用当前工作目录。

    您还可以视需要使用 --stage-bucket 标志指定要在部署期间将源代码上传到的 Cloud Storage 存储桶。

    在上传源代码期间,Cloud Functions 会通过 .gcloudignore 文件排除不必要的文件。

从 Cloud Storage 部署

本部分介绍如何通过保存在 Cloud Storage 存储桶中的源代码部署函数。源代码必须打包为 ZIP 文件。

为了让 Cloud Run functions 从 Cloud Storage 存储桶中读取数据,Cloud Run functions 服务代理必须具有 storage.objects.get 权限。如果源存储桶与您的函数位于同一项目中,则系统会自动授予此权限。如果存储桶位于其他项目中,您必须手动向服务代理授予此权限。

如需了解如何控制对存储桶的访问权限,请参阅 Cloud Storage 文档中的使用 IAM 权限部分。

有了此权限,您现在可以从 Cloud Storage 部署函数:

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 使用 gcloud functions deploy 命令,按照上面的部署说明执行操作。

    对于 --source 标志,请指定以 gs:// 开头的 Cloud Storage 路径。路径上的对象必须是包含函数源代码的 ZIP 文件。函数源文件必须位于 ZIP 文件的根目录下;有关详情,请参阅源目录结构

命令行示例

本部分介绍了一些示例部署场景的部署命令。

如需详细了解 Cloud Run functions 支持的各种触发器,请参阅 Cloud Run functions 触发器

本地源代码中的 HTTP 函数

假设您有一个符合如下描述的 HTTP 函数:

  • nodejs22 函数会使用 。
  • 源代码位于当前工作目录 (.) 中。
  • 代码中的入口点名为 myHttpFunction

如需在区域 us-central1 中部署名为 my-http-function 的函数,请使用以下命令:

gcloud functions deploy my-http-function \
  --gen2 \
  --region=us-central1 \
  --runtime=nodejs22 \
  --source=. \
  --entry-point=myHttpFunction \
  --trigger-http

来自 Cloud Storage 中源代码的 Pub/Sub 函数

假设您有一个事件驱动型函数,如下所示:

  • 该函数处理 Pub/Sub 消息发布事件。
  • python312 函数会使用 。
  • 源代码位于 Cloud Storage 中的以下路径下:gs://my-bucket/my_function_source.zip
  • 代码中的入口点名为 pubsub_handler

如需在区域 europe-west1 中部署名为 my-pubsub-function 的函数,并让 Pub/Sub 主题 my-topic 中的消息触发该函数,请使用以下命令:

gcloud functions deploy my-pubsub-function \
  --gen2 \
  --region=europe-west1 \
  --runtime=python312 \
  --source=gs://my-bucket/my_function_source.zip \
  --entry-point=pubsub_handler \
  --trigger-topic=my-topic

来自本地源代码的 Cloud Storage 函数

假设您有一个事件驱动型函数,如下所示:

  • 该函数处理 Cloud Storage 对象删除事件。
  • java21 函数会使用 。
  • 源代码位于本地 ./functions/storage-function 路径下。
  • 代码中的入口点名为 myproject.StorageFunction

如需在区域 asia-northeast1 中部署名为 my-storage-function 的函数,并让 Cloud Storage 存储桶 my-bucket 中的事件触发该函数,请使用以下命令:

gcloud functions deploy my-storage-function \
  --gen2 \
  --region=asia-northeast1 \
  --runtime=java21 \
  --source=./functions/storage-function \
  --entry-point=myproject.StorageFunction \
  --trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
  --trigger-event-filters="bucket=my-bucket"

后续步骤