How to mirror a GitLab project to Google Cloud Source Repositories

Neal Sebastian
4 min readMar 23, 2021

Welcome to my 1st of hopefully many “byte”-sized how-tos or tips of things that I encounter on my job. As you can see, this 1st tip is about setting up a Cloud Source repo from a GitLab project. If you wonder why would someone need to do this, one reason is to enable tight integration with Google’s Cloud Build. For example, you might want to trigger a build whenever a commit is pushed to a protected branch.

Step 1: Create the Cloud Source mirror repository

Go to https://source.cloud.google.com and click the “Add repository” button on the top right.

Cloud Source UI

Choose “Create new repository”; and press “Continue”. Enter the repository name. I usually just append “mirror” to the GitLab project name, e.g. “my-project-mirror”. Select or create the Google Cloud project. Press the “Create” button, then GCP will show you instructions on how to upload code to the repository. Keep this page open, and open a new browser tab. We now need to configure the GitLab project to use this new repository as its mirror.

The billing account associated with the GCP project will incur Cloud Source Repositories charges. Google charges you with the size, number of users accessing the repository, and network egress. There is an always free tier of 5 users, 50GB storage and network egress per billing account. To limit the cost, I usually use the same GCP user for all my mirrors on a project.

Step 2: Configure GitLab Mirror

In a new browser tab, go to your GitLab project. Select Settings > Repository from the left side menu.

GitLab Repository Settings

In the “Git repository URL” field, enter the SSH URL for the Cloud Source repository we created in Step 1. It has the ff syntax:
ssh://<GCP user email>@source.developers.google.com:2022/p/<GCP project ID>/r/<Cloud Source repo name>

Continuing the “My Project” example, the SSH repository URL would be:
ssh://neal@myemail.com@source.developers.google.com:2022/p/my-gcp-project-id/r/my-project-mirror

You can also easily get the SSH URL by going back to the Cloud Source repo browser tab and copying the URL from one of the example snippets in the instructions. Below is a screenshot of the SSH URL from Cloud Source.

Cloud Source Example SSH URL

Make sure “Push” is used for the “Mirror direction” field; and press the “Detect host keys” button. This saves the SSH fingerprint of Cloud Source Repositories.

Switch the value of the “Authentication method” field to “SSH public key”. I don’t choose to enable “Keep divergent refs”, because I want pushes to fail if the branches I mirror suddenly diverges. But I enable “Mirror only protected branches” to save cost. The configuration will look like the image below.

GitLab Mirror Settings

When you press “Mirror repository” button, an entry will be added to the Mirrored repositories table below it.

Step 3: Add GitLab SSH key

Copy the SSH public key generated by GitLab for your mirror by clicking the clipboard button (highlighted by a green circle below).

GitLab Mirror List

Go back to the Cloud Source repository browser tab, then click the three dot menu on the top right of the page. Select “Manage SSH Keys”.

Cloud Source Menu

Push the “Register SSH key” button, then a form will pop up. Add a sensible key name to identify the GitLab key. I usually use “<GitLab project name> GitLab“. Paste the GitLab SSH key you copied earlier in the Key field. Then select “Register”.

Step 4: Start the mirror

This step is the easiest! You just have to go back to the GitLab browser tab, then push the “Update now” button.

GitLab Mirror Actions

Congratulations, you now have a GitLab mirror to Cloud Source! If you go back to your Cloud source repository, it should have the same content as your GitLab project. If not, wait a few minutes, then refresh the page.

Notes

This steps here should work on both the hosted (gitlab.com) and private GitLab servers.

At the time of writing this article, Cloud Build has no support yet for directly connecting with GitLab. The following are the supported repositories:

  • Cloud Source Repositories (of course!)
  • GitHub
  • Bitbucket (Beta)

Although one can use the Cloud Build webhook integration, I wanted my build instructions (cloudbuild.yaml) versioned in the same git repo, rather than inline in the Cloud Build UI.

--

--

Neal Sebastian

AWS and GCP Solutions Architect. My journey to the cloud started from writing web & mobile applications, to building CI/CD pipelines and data lakes.