Skip to main content

Migrating Source Code to a Google Cloud Source Repository

Hands-On Lab


Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content





Working with code is an everyday reality for cloud application developers, and Google Cloud provides a service that makes it easy to access your code for Google Cloud projects: Source Repositories. This hands-on lab is divided into two parts. In the first part of the lab, you’ll create a new Source Repository and populate it with example code using the git command. Once you have pushed the code to the repo, you’ll use the Source Repository interface to inspect the files. In the second part of the lab, you’ll set up another Source Repository to mirror your own existing GitHub repository. Please note it is a prerequisite for this part of the lab to have your own GitHub account and at least one repository.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Migrating Source Code to a Google Cloud Source Repository

This hands-on lab is divided into two parts. The first part, which focuses on using the git command and Google Cloud Shell, has no prerequisites and can be fully completed by anyone. The second part, in which a GitHub repository is mirrored by a Google Source Repository, requires you have an established GitHub account and at least one repository. Please have your GitHub login credentials and name of the repository available before beginning this hands-on lab if you wish to complete it entirely.

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says "Open Link in Incognito Window"). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.

On the Welcome to your new account screen, review the text, and click Accept. In the "Welcome L.A.!" pop-up once you're signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

You're now logged in to the Google Cloud Platform console.

Migrating Source Code to a Google Source Repository

Now, on to the lab!

Enable the Source Repository API.

  1. Navigate to APIs & Services > Library.
  2. Search for "source repository", select Cloud Source Repositories API, and click Enable.

Create a Source Repository.

  1. Click the Activate Cloud Shell icon in the top navigation bar.
  2. Use the command:

    gcloud source repos create gcpro_repo

Clone the Source Repository.

  1. Use the command:

    gcloud source repos clone gcpro_repo

Confirm the cloned repo.

  1. Use the ls command to view the new cloned repo.
  2. Change into the new directory:

    cd gcpro_repo

Create an example file.

  1. Create a new file:

    echo "<h1> Greetings from Linux Academy</h1>" > greetings.txt

Push the new content to the Source Repository repo.

  1. Configure a variable, using the cloud_user username you were assigned as part of the lab:

    git config --global "<CLOUD_USER>"
  2. Configure a variable, using your name:

    git config --global "<YOUR_NAME>"
  3. Add the file:

    git add greetings.txt
  4. Commit the file:

    git commit -m "Committed initial file" greetings.txt

Push the file from the master branch to origin.

  1. Use the command:

    git push origin master

Confirm the operation.

  1. Navigate to the Cloud Source Repositories dashboard.
  2. Click the gcpro_repo repo.
  3. Click the greetings.txt file to review the contents.

Return to the main repositories page.

  1. Click Cloud Source Repositories in the upper-left. Return to the main repository page where you have at least one repository.

Create new repository.

  1. From the Cloud Source Repositories dashboard, click Add repository.
  2. In the Add a Repository dialog, select Connect external repository, and click Continue.
  3. On the Connect external repository page, select the current project and then, from the Git provider list, choose GitHub.
  4. When the consent checkbox appears, select it.
  5. Click Connect to GitHub.
  6. From the list of your repos, select one.
  7. Click Connect selected repository, and then OK.

Confirm the operation.

  1. When the files of the repo are displayed, select one to open and confirm its contents.


Congratulations on completing this lab!