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

Complete the steps from Quickstart Creating a Repository.

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

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:

Console

  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:

    projects/[PROJECT-ID]/topics/csr-test
    

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

  4. Click Create.

gcloud

  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:

Console

  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:

    projects/[PROJECT-ID]/subscriptions/csr-subscription-test
    

    Leave Delivery Type set to Pull.

  5. Click Create.

gcloud

From a terminal window, run the following command:

gcloud alpha 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 beta 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
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 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

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.

Delete the repository you created:

  1. Open the source view in the GCP Console:

    Open the Source View in the Console

  2. Click the More button More button next to the repository you want to delete.

    Create Repository button

  3. Select Delete.

  4. When prompted for the repository name, enter hello-csr and then click Delete.

What's next

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

Send feedback about...

Cloud Source Repositories