部署具有版本控制的 Cloud Functions 函数

本页面介绍如何使用在 Cloud Source Repositories 中进行版本控制的代码部署 Cloud Functions

通过将 Cloud Functions 与 Cloud Source Repositories 集成,您可以使用 Cloud Source Repositories 对包含函数的代码进行版本控制。随着函数随时间变化,您可以继续访问先前的提交,以查看函数进行了哪些更改以及更改的时间。

准备工作

  1. 完成 在 Cloud Source Repositories 中创建代码库中的步骤。
  2. 启用 Cloud Functions API 和 Cloud Build API。
  3. 启用 Cloud Functions API 和 Cloud Build API

向代码库添加一个函数

  1. 在本地计算机上,转到 hello-world 代码库的根目录:

    cd hello-world
    
  2. 在本地系统上,为函数代码创建一个目录。

    Linux 或 macOS

    创建目录:

    mkdir gcf_hello_world

    转到该目录:

    cd gcf_hello_world

    Windows (CMD)

    创建目录:

    mkdir %HOMEDRIVE%%HOMEPATH%\gcf_hello_world

    转到该目录:

    cd %HOMEDRIVE%%HOMEPATH%\gcf_hello_world
  3. gcf_hello_world 目录中,创建一个包含以下内容的 index.js 文件:

    /* HTTP Cloud Function.
    *
    * @param {Object} req Cloud Function request context.
    * @param {Object} res Cloud Function response context.
    */
    exports.helloGET = (req, res) => {
      res.send('Hello from Cloud Functions and Cloud Source Repositories');
    };
    

    这是一个名为 helloGET 的简单函数,它以文本 Hello from Cloud Functions and Cloud Source Repositories 响应 HTTP GET 请求。

推送到 Cloud Source Repositories

将您刚创建的文件推送到 Cloud Source Repositories。

  1. 添加文件:

    git add .
    
  2. 提交带有说明此操作历史记录的注释的文件:

    git commit -m "Add Cloud Functions test to Cloud Source Repositories"
    
  1. 使用 git push 命令,将本地 Git 代码库的内容添加到 Cloud Source Repositories:

    git push origin master
    

创建和部署函数

使用 Google Cloud 控制台或 Google Cloud CLI,创建并部署您的函数。

控制台

  1. 转到 Cloud Functions 页面。

    转到 Cloud Functions 页面

    确保已选中启用了 Cloud Functions 的 Google Cloud 项目。

  2. 点击创建函数

  3. 创建函数 页面中,填写以下选项:

    • 环境字段中,选择第 1 代
    • 函数名称字段中,输入 cloud-source-repositories-test
    • 区域字段中,选择 us-central1
    • 触发器 列表中,选择HTTP
    • 身份验证列表中,选择需要身份验证
    • 确保选中需要 HTTPS 选项。
  4. 点击保存

  5. 点击下一步

    • 运行时字段中,选择 Node.js 16
    • 入口点字段中,输入 helloGET
    • 源代码列表中,选择 Cloud Source Repositories
    • 代码库 字段中,键入 hello-world
    • 分支名称字段中,输入 master
    • 带源代码的目录 字段中,输入 /gcf_hello_world
  6. 点击部署

在部署函数的过程中,它的旁边会显示一个小的旋转图标。部署完成后,旋转图标会变为绿色对勾标记。

gcloud CLI

  1. 在终端窗口中,设置一个包含您的 Google Cloud 项目 ID 的变量。确保这是与您的代码库相同的 Google Cloud 项目。

    export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
    
  2. 部署函数的方法如下:

    gcloud functions deploy helloGET \
    --source https://source.developers.google.com/projects/$PROJECT_ID/repos/hello-world/moveable-aliases/master/paths/gcf_hello_world \
    --trigger-http \
    --runtime=nodejs8;
    

如需详细了解如何部署到 Cloud Source Repositories,请参阅 从源代码控制部署

测试函数

使用 GCP Console 或 gcloud CLI 测试您的新功能。

控制台

  1. 转到 Cloud Functions 概览 页面。

    转至“概览”页面

    确保已选中启用了 Cloud Functions 的 Google Cloud 项目。

  2. 点击 cloud-source-repositories-test 函数行中的 Show actions 菜单。

  3. 点击测试函数

  4. 点击测试函数按钮。

    一两分钟后,输出 字段将更新以显示消息 Hello from Cloud Functions and Cloud Source Repositories

gcloud CLI

在终端窗口中,输入以下命令:

gcloud functions call helloGET

显示类似以下内容的输出:

executionId: owqd9fdh5od2
result: Hello from Cloud Functions and Cloud Source Repositories

清理

要删除您创建的函数和代码库,请按照下列步骤操作。

删除函数

  1. 转到 Cloud Functions 概览 页面。

    转至“概览”页面

    确保已选中启用了 Cloud Functions 的 Google Cloud 项目。

  2. 选择此快速入门的 helloGET 函数 cloud-source-repositories-test

  3. 在同一行上,点击 更多 ,然后点击 删除

删除代码库

  1. 在 GCP Console 中,为 Cloud Source Repositories 打开 所有代码库 页面。

    打开 Cloud Source Repositories

  2. 将指针放在要删除的代码库上,然后点击 设置

    常规设置 页面打开。

  3. 点击 删除此代码库

    移除代码库 对话框打开。

  4. 输入您要删除的代码库的名称。

  5. 点击删除

后续步骤