Quickstart: Add Cloud Pub/Sub notifications to Cloud Source Repositories

This topic describes how to set up Cloud Pub/Sub notifications for Cloud Source Repositories.

Before you begin

  1. Complete the steps from Quickstart: Create a repository.

    After you complete that quickstart, you have an app you can deploy to App Engine.

  2. Enable the Compute Engine API.

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

    Enable the Compute Engine API

Create a Cloud Pub/Sub topic

To get notifications for a Cloud Source Repositories project or repository, you must create a Cloud Pub/Sub topic. You can use either the Google Cloud Platform Console or the gcloud command-line tool to create a topic.

Console

  1. In the GCP Console, go to the Cloud Pub/Sub Topics page.

    Go to the Topics page

  2. Click Create Topic .

  3. In the Name field, type csr-test.

  4. Click Create Topic.

    The Topic details page opens.

gcloud

  1. In a terminal window, set a variable that contains your GCP project ID. Ensure this is the same GCP project that contains your repository.

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

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

Create a Cloud Pub/Sub subscription

Cloud Pub/Sub subscriptions let subscribers receive messages from a repository's topic.

Console

  1. In the GCP Console, go to the Cloud Pub/Sub Topics page.

    Go to the Topics page

  2. Click the name of the GCP project topic you just created.

  3. On the Subscriptions tab, click Create Subscription.

    The Add subscription to topic page opens.

  4. For the subscription name, type csr-subscription-test.

  5. Ensure Delivery type is set to Pull.

  6. Click Create.

    The Topic details page opens. Your subscription is listed on the Subscriptions tab.

gcloud

In a terminal window, run the following command:

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

Associate a topic with a repository

Using the gcloud command-line tool, associate a Cloud Pub/Sub topic with a specific repository:

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

Output similar to the following is displayed:

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

Push a change to the hello-world repository

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

  1. In a terminal window, go 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 push a change to your repository, Cloud Pub/Sub sends a notification. To view this notification, enter the following command:

gcloud pubsub subscriptions pull csr-subscription-test

Clean up

To delete your Cloud Pub/Sub resources, follow these steps.

  1. In the GCP Console, go to the Cloud Pub/Sub Topics page.

    Go to the Topics page

  2. Select the checkbox for any topic you want to permanently delete.

  3. Click Delete .

  4. To delete any remaining subscriptions, display each one, and then click Delete .

To delete the repository you created, follow these steps.

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

    Open Cloud Source Repositories

  2. Hold the pointer over the repository you want to delete and click 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

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Source Repositories Documentation