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
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.
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 grantroles/editor
to the Compute Engine default service account.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
In the Google Cloud console, go to the Pub/Sub Topics page.
Click Create Topic add_box.
In the Name field, type
csr-test
.Click Create Topic.
The Topic details page opens.
gcloud
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)')
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
In the Google Cloud console, go to the Pub/Sub Topics page.
Click the name of the Google Cloud project topic you just created.
On the Subscriptions tab, click Create Subscription.
The Add subscription to topic page opens.
For the subscription name, type
csr-subscription-test
.Ensure Delivery type is set to Pull.
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: [email protected]
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.
In a terminal window, go to the
hello-world
repository:cd hello-world
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)
Add the file so Git can commit it:
git add main.py
Commit the file:
git commit -m "Update main.py to test Cloud Pub/Sub notifications"
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.
In the Google Cloud console, go to the Pub/Sub Topics page.
Select the checkbox for any topic you want to permanently delete.
Click Delete delete.
To delete any remaining subscriptions, display each one, and then click Delete delete.
To delete the repository you created, follow these steps.
In the Google Cloud console, open the All repositories page for Cloud Source Repositories.
Hold the pointer over the repository you want to delete and click Settings settings.
The General settings page opens.
Click Delete this repository delete.
The Remove repository dialog opens.
Type the name of the repository you want to delete.
Click Delete.
What's next
- Learn more about configuring notifications for Cloud Source Repositories.
- Learn more about Pub/Sub notifications for Cloud Source Repositories.
- Read the Pub/Sub documentation.
- For an in-depth explanation of Pub/Sub, see What is Pub/Sub?
- Learn more about Pub/Sub access control roles.