How to mirror a GitLab project to Google Cloud Source Repositories

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.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Kubernetes Integration with python CGI

Database — Different kinds of Index

Selectively excluding dependency in SBT

Finally, InsureDAO testnet is out on Rinkeby testnet today, 6th August at 4:00 PM (UTC) !!

Newcomer Text Editor Packs a Punch

Installing FreeBSD on Raspberry Pi

Quick Overview of C Programming

Laravel : One To Many Relation in Single Query

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Neal Sebastian

Neal Sebastian

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

More from Medium

Composite GitHub Actions for Deploying GCP resources to multiple projects

How to Pass GCP Professional Cloud Developer

How to use GCP artifact registry

How to add custom domain to Google Cloud Function