Get notified when code changes in Cloud Source Repositories

This page shows you how to set up Pub/Sub notifications for Cloud Source Repositories.

Before you begin

  1. Complete the steps from Create a code repository in Cloud Source Repositories.

    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 Pub/Sub.

    If the org policy constraint constraints/iam.automaticIamGrantsForDefaultServiceAccounts is turned on, make sure to manually grant roles/editor to the Compute Engine default service account.

    Enable the Compute Engine API

  3. Enable the Pub/Sub API.

    Enable the Pub/Sub API

Create a Pub/Sub topic

To get notifications for a Cloud Source Repositories project or repository, you must create a Pub/Sub topic. You can use either the Google Cloud console or the Google Cloud CLI to create a topic.

Console

  1. In the GCP Console, go to the 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 Google Cloud project ID. Ensure this is the same Google Cloud 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 Pub/Sub subscription

Create a pull, push, or BigQuery subscription.

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

Console

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

    Go to the Topics page

  2. Click the name of the Google Cloud 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 Google Cloud CLI, associate a 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 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, 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 Pub/Sub resources, follow these steps.

  1. In the GCP Console, go to the 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