Developing on GCP 💻 - Managing Source Codes with Cloud Source Repositories 📁

Google Cloud Source Repositories

Google Cloud Source Repositories is a single place for your team to store, manage, and track code. Cloud Source Repositories allows developers to collaborate easily and more securely manage your code on a fully featured, scalable, private Git repository. It can also be extended by connecting to other GCP tools, including Cloud Build, App Engine, Stackdriver, and Cloud Pub/Sub. Cloud Source Repositories enables fast, indexed powerful code search across all your owned repositories to save time.

We'll be focusing on using Google Cloud Source Repositories to manage source codes during development and exploring some of its features that aids in improving developers productivity.

Benefits of using Google Cloud Source Repositories

Unlimited private Git repositories

Get free unlimited private repositories to organize your code in a way that works best for you. Mirror code from GitHub or Bitbucket repositories to get powerful code search, code browsing, and diagnostics capabilities. Cloud Source Repositories allows developers to create up to 1,000 repositories per Google Cloud Platform project.

Integrate with GCP services

Get fast feedback on your code changes with built-in integrations for continuous integration. You can easily set up triggers to automatically build and test using Cloud Build when you push changes to Cloud Source Repositories.

Use powerful regexp to search across multiple directories. You can use regular expressions to refine your search or perform a single targeted search across projects, files, and code repositories. Cloud Source Repositories uses the same search technologies like Google Search but optimizes the indexing algorithms.

Screenshot from 2019-06-29 17-49-44.png

Source browser

View repository files from within the Cloud Source Repositories using Source Browser. Filter your view to focus on a specific branch, tag, or commit.

Automatic syncing with across hosted repositories

Connect Cloud Source Repositories to a hosted repository on GitHub or Bitbucket. Automatically sync changes to Cloud Source Repositories when changes are pushed to GitHub or Bitbucket.

Git operations

Basically, you're allowed to use all Git operations e.g. Push, pull, clone and log, and perform other Git operations required by your workflow.

Proven reliability

Manage your code on systems distributed geographically across multiple data centres and being run on Google’s infrastructure with high availability.

Security & Debugging

Cloud Source Repositories has a Push-block feature enabled which checks for secret credentials on your commits and can stop you from pushing such credentials with details. In addition, Stackdriver debugger allows you debug codes on Cloud Source Repositories.


Setting up a new repository on Cloud Source Repositories

You can use Google Cloud Source Repositories to create a new, empty repository.

On your Cloud Source Repositories dashboard, follow the steps below:

  • Click Add Repository
  • Select Create New Repository and Continue
  • Enter the Repository Name and Select the Project it belongs to.

Screenshot from 2019-06-29 17-36-43.png

Alternatively, you can create a new repository using the cloud command:

gcloud source repos create SampleCodes

Mirroring a repository

As at the time of publishing this article, Cloud Source Repositories supports both GitHub and Bitbucket repositories. By enabling mirroring, commits that you push to GitHub/Bitbucket repositories are copied, or mirrored, into a repository hosted in Cloud Source Repositories.

To mirror a repository, on your Cloud Source Repositories dashboard, follow the steps below:

  • Click Add Repository
  • Select Connect external repository and Continue
  • Select the Project that owns the repository, Git provider you are mirroring from, Authorize and Connect
  • Select a Repository and Click Connect Selected Repository

Screenshot from 2019-06-29 18-38-30.png

Cloning a repository

You must set up authentication in your environment before you can access any hosted repositories from your system. Cloud Source Repositories supports the following types of authentication:

  • SSH
  • Cloud SDK
  • Manually generated credentials

You can read more about the Authentication techniques here

Download and Setup Google Cloud SDK using the steps described here

Once that's done, you can simply clone a Cloud Source Repositories repository using:

gcloud source repos clone SourceCodes --project=my-project

Where SourceCodes is the repository name and my-project is the Project ID.

If you'll like to stick to using the Google Cloud Platform to modify your source codes on Cloud Source Repositories, check out my article - Getting Started with Cloud Shell (Editor) 📝

Thanks for reading through! Let me know if I missed any step, if something didn’t work out quite right for you or if this guide was helpful.

Bharath KP's photo

Hi Timothy,

I've followed the ways that is mentioned in above to mirror a github repo wo my source repository in GCP.

However, after initial authentication with github, I select a repository and click on connect repository. I keep seeing an error message "Failed to connect repositories". I read in one of the stackoverflow comments that we need to wait for a couple of days after authenticating for the keys to spread properly. Is it true? I couldn't find any logs in stackdriver as well. The same error pops up for both private and public repos

Timothy Olaleke's photo

Hello, have you been able to resolve this? You can take a look at this also:

Sundaram Srivastava's photo

Hi, Can you please explain how can I merge the code in cloud source repository. suppose I have created 2 repository :- A & B. A belongs to project DEV & B belongs to project PROD. Now I want to merge the code from repo-A to repo-B. How can I do it, like in GITHUB we have option of merge request for 2 branches. Also how can I ensure that as soon as code gets merged to repo-B, the cloudbuild gets triggered & it deploys the code to app engine of PROD.

Timothy Olaleke's photo

Hi Sundaram Srivastava , I'm just seeing this. The way I do this is to use Cloud Build to trigger the codes copying to another repository, ensure that your Cloud Build service account has permission to the other repo. This is my config file:

  - name:
    args: ['init']
  - name:
    args: ['add', '-A']
  - name:
    args: ['config', '--global', '', '${_GIT_NAME}']
  - name:
    args: ['config', '--global', '', '${_GIT_EMAIL}']
  - name:
    args: ['commit', '-am', 'latest production commit']
  - name:
    args: ['push', '-f', '${_PROD_PROJECT}/r/${_PROD_REPO}', 'master']

Great & elegant article, I like it well..... Thanks

Ami Beni's photo

Thanks Timothy. Does GCP or CSR to be specific also offer a web interface for collaboration with teammates similar to GitHub and bitbucket where you can comment, manage pull requests etc ?