Quickstart adding Pub/Sub notifications to Cloud Source Repositories

This topic describes how to add Cloud Pub/Sub to Cloud Source Repositories.

Before you begin

  1. Complete the steps from Quickstart Creating a Repository.

    After you complete the quickstart, you should have an application you can deploy to App Engine.

  2. Enable Compute Engine API.

    In this quickstart, we use the Compute Engine default service account to publish messages to Cloud Pub/Sub.

Create a Cloud Pub/Sub topic

For each Cloud Source Repositories project or repository for which you'd like notifications, you need to create a Cloud Pub/Sub topic. You can create a topic using either the console or the gcloud command-line tool.

To create a topic:


  1. Go to the Cloud Pub/Sub topics page in the GCP Console.

    Go to the Cloud Pub/Sub topics page

  2. Click Create Topic.

  3. Enter a topic name with the URI:


    where [PROJECT-ID] is your Google Cloud Platform project ID.

  4. Click Create.


  1. From a terminal window, set a variable that contains your Project ID. Make sure this is the same project that contains your repository.

    export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
  2. Run the following command:

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

Create a Cloud Pub/Sub subscription

Next, you will create a Pub/Sub subscription. A subscription allows subscribers to receive messages from your repository's topic.

To create a subscription:


  1. Go to the Cloud Pub/Sub topics page in the GCP Console.

    Go to the Cloud Pub/Sub topics page

  2. Click your project's topic.

  3. Click Create Subscription.

  4. Enter a subscription name:


    Leave Delivery Type set to Pull.

  5. Click Create.


From a terminal window, run the following command:

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

Associate a topic with a repository

You can add an association between a project or repository and a Cloud Pub/Sub topic using the gcloud command-line tool.

To associate a topic to a specific repository, type the following command:

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

You will receive a response similar to the following:

name: projects/csr-quickstart-test/repos/hello-world
    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

Push a change to the hello-world repository

With your Cloud Pub/Sub topics and subscriptions set up, push a change to the hello-world repository. This change will allow you to see the notification at work.

  1. From a terminal window, navigate to the hello-world repository.

    cd ~/hello-world
  2. Edit the main.py file to read as follows:

    #!/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. Add the file so Git can commit it.

    git add main.py
  4. Commit the file.

    git commit -m "Update main.py to test Cloud Pub/Sub notifications"
  5. Push the file to Cloud Source Repositories.

    git push origin master

View the notification

When you pushed the change to your repository, Cloud Pub/Sub sent a notification. To view this notification, type the following command:

gcloud alpha pubsub subscriptions pull csr-subscription-test

Clean up

To delete your Cloud Pub/Sub resources:

  1. Go to the Cloud Pub/Sub topics page in the GCP Console.

    Go to the Cloud Pub/Sub topics page

  2. Check the checkbox next to the topic that you created.

  3. Click Delete to permanently delete the topic.

  4. Delete any remaining subscriptions by displaying each subscription and clicking its trash can icon.

To delete the repository you created:

  1. In the GCP Console, open the All repositories page for Cloud Source Repositories.

    Open Cloud Source Repositories

  2. Hover over the repository you want to delete and click Settings settings.

    The General settings page opens.

  3. Click Delete this repository.

    The Remove repository dialog opens.

  4. Type the name of the repository you want to delete.

  5. Click Delete.

What's next

Bu sayfayı yararlı buldunuz mu? Lütfen görüşünüzü bildirin:

Şunun hakkında geri bildirim gönderin...

Cloud Source Repositories Documentation