Skip to main content

Cloud Source Repositories


1 - Overview

Cloud Source Repositories (CSR) provides private Git repositories hosted on Google Cloud.


2 - Create a repository

2.1 Via gcloud

# Create a repo
gcloud source repos create mon-app

# List repos
gcloud source repos list

# Clone the repo
gcloud source repos clone mon-app

2.2 Via Console

  1. Go to Source Repositories
  2. Click "Add repository"
  3. Choose "Create new repository"
  4. Name the repo

3 - Authentication

3.1 Credential Helper

# Configure authentication
git config --global credential.https://source.developers.google.com.helper gcloud.sh

# Clone with HTTPS
git clone https://source.developers.google.com/p/mon-projet/r/mon-app

3.2 SSH

# Generate an SSH key (if needed)
ssh-keygen -t ed25519 -C "[email protected]"

# Add the public key in Cloud Console
# IAM & Admin > SSH Keys

# Clone with SSH
git clone ssh://[email protected]@source.developers.google.com:2022/p/mon-projet/r/mon-app

4 - Mirroring

4.1 From GitHub

# Configure mirroring via Console
# 1. Source Repositories > Add repository
# 2. Connect external repository
# 3. Authorize GitHub
# 4. Select the repo to mirror

4.2 Configuration

OptionDescription
Automatic syncSynchronization every minute
Manual syncManual synchronization
WebhooksTriggering via GitHub webhooks

4.3 Benefits of mirroring

  • Native Cloud Build triggers
  • Integration with GCP services
  • Code backup on GCP
  • No need to change your workflow

5 - Build Triggers

5.1 Create a trigger

# Trigger on push
gcloud builds triggers create cloud-source-repositories \
--name="build-on-push" \
--repo="mon-app" \
--branch-pattern="^main$" \
--build-config="cloudbuild.yaml"

# Trigger on tag
gcloud builds triggers create cloud-source-repositories \
--name="build-on-tag" \
--repo="mon-app" \
--tag-pattern="^v.*" \
--build-config="cloudbuild.yaml"

5.2 GitHub trigger

# Connect GitHub
gcloud builds triggers create github \
--name="github-trigger" \
--repo-owner="MonOrg" \
--repo-name="mon-app" \
--branch-pattern="^main$" \
--build-config="cloudbuild.yaml"

5.3 Pull Request trigger

gcloud builds triggers create github \
--name="pr-trigger" \
--repo-owner="MonOrg" \
--repo-name="mon-app" \
--pull-request-pattern="^main$" \
--build-config="cloudbuild-pr.yaml" \
--comment-control="COMMENTS_ENABLED"

6 - Best practices

6.1 Branch structure

6.2 Branch protection

Cloud Source Repositories does not have native branch protection like GitHub.

Alternatives:

  • Use GitHub/GitLab with mirroring
  • Implement checks in Cloud Build
  • Use server-side hooks

6.3 CI integration

# cloudbuild.yaml - PR validation
steps:
- name: 'gcr.io/cloud-builders/npm'
args: ['ci']

- name: 'gcr.io/cloud-builders/npm'
args: ['run', 'lint']

- name: 'gcr.io/cloud-builders/npm'
args: ['test']

7 - Migration from GitHub/GitLab

7.1 Initial import

# Clone the source repo
git clone https://github.com/MonOrg/mon-app.git
cd mon-app

# Create the CSR repo
gcloud source repos create mon-app

# Add the GCP remote
git remote add google https://source.developers.google.com/p/mon-projet/r/mon-app

# Push to GCP
git push google --all
git push google --tags

7.2 Continuous synchronization

Recommended: use automatic mirroring rather than managing two remotes.


Summary

In this chapter, we learned:

  • How to create repositories
  • Authentication (HTTPS, SSH)
  • Mirroring from GitHub/GitLab
  • Build Triggers
  • Best practices

Next step

In the next chapter, we will look at Cloud Build in detail.

→ Next chapter: Cloud Build


← Back to table of contents