在 Cloud Source Repositories 中的代码发生更改时收到通知

本页面介绍如何为 Cloud Source Repositories 设置 Pub/Sub 通知。

准备工作

  1. 完成 在 Cloud Source Repositories 中创建代码库中的步骤。

    完成该快速入门后,您将拥有一个可以部署到 App Engine 的应用。

  2. 启用 Compute Engine API。

    在此快速入门中,使用 Compute Engine 默认服务账号将消息发布到 Pub/Sub。

    如果组织政策限制条件 constraints/iam.automaticIamGrantsForDefaultServiceAccounts 已开启,请务必手动将 roles/editor 授予 Compute Engine 默认服务帐号。

    启用 Compute Engine API

  3. 启用 Pub/Sub API。

    启用 Pub/Sub API

创建 Pub/Sub 主题

要获取有关 Cloud Source Repositories 项目或代码库的通知,您必须创建一个 Pub/Sub 主题。您可以使用 Google Cloud 控制台或 Google Cloud CLI 来创建主题。

控制台

  1. 在 Cloud Console 中,转到 Pub/Sub 主题 页面。

    转到“主题”页面

  2. 点击 创建主题

  3. 名称字段中,输入 csr-test

  4. 点击创建主题

    主题详细信息 页面打开。

gcloud

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

    export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
    
  2. 运行以下命令:

    gcloud pubsub topics create projects/$PROJECT_ID/topics/csr-test
    

创建 Pub/Sub 订阅

创建pullpushBigQuery 订阅。

Pub/Sub 订阅可让订阅者接收来自代码库主题的消息。

控制台

  1. 在 Cloud Console 中,转到 Pub/Sub 主题 页面。

    转到“主题”页面

  2. 点击您刚刚创建的 Google Cloud 项目主题的名称。

  3. 订阅 标签页上,点击 创建订阅

    将打开 向主题添加订阅 的页面。

  4. 对于订阅名称,输入 csr-subscription-test

  5. 确保将 投放类型 设置为拉动

  6. 点击创建

    主题详细信息 页面打开。 您的订阅列在 订阅 标签页上。

gcloud

在终端窗口中,运行以下命令:

gcloud pubsub subscriptions create csr-subscription-test --topic=csr-test

将主题与代码库相关联

使用 Google Cloud CLI,将 Pub/Sub 主题与特定代码库关联:

gcloud source repos update hello-world --add-topic=csr-test

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

name: projects/csr-quickstart-test/repos/hello-world
pubsubConfigs:
  projects/csr-quickstart-test/topics/csr-test:
    messageFormat: JSON
    serviceAccountEmail: 604898361241-compute@developer.gserviceaccount.com
    topic: projects/csr-quickstart-test/topics/csr-test
url: https://source.developers.google.com/p/csr-quickstart-test/r/hello-world

将更改推送到 hello-world 代码库

设置 Pub/Sub 主题和订阅后,将更改推送到 hello-world 代码库。通过此更改,您可以查看工作中的通知。

  1. 在终端窗口中,转到 hello-world 代码库:

    cd hello-world
    
  2. 编辑 main.py 文件,内容如下:

    #!/usr/bin/env python
    
    import webapp2
    
    class MainHandler(webapp2.RequestHandler):
        def get(self):
            self.response.write('Hello, Cloud Pub/Sub!')
    
    app = webapp2.WSGIApplication([
        ('/', MainHandler)
    ], debug=True)
    
  3. 添加该文件,以便 Git 将其提交。

    git add main.py
    
  4. 提交此文件:

    git commit -m "Update main.py to test Cloud Pub/Sub notifications"
    
  5. 将文件推送到 Cloud Source Repositories。

    git push origin master
    

查看通知

当您将更改推送到代码库时,Pub/Sub 将发送通知。要查看此通知,请输入以下命令:

gcloud pubsub subscriptions pull csr-subscription-test

清理

若要删除您的 Pub/Sub 资源,请按照下列步骤操作。

  1. 在 Cloud Console 中,转到 Pub/Sub 主题 页面。

    转到“主题”页面

  2. 选中要永久删除的任何主题的复选框。

  3. 点击删除

  4. 要删除所有剩余的订阅,请显示每个订阅,然后点击 删除

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

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

    打开 Cloud Source Repositories

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

    常规设置 页面打开。

  3. 点击 删除此代码库

    移除代码库 对话框打开。

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

  5. 点击删除

后续步骤