# Cloud Source Repositories 使用簡介 [`Cloud Source Repositories`](https://source.cloud.google.com/) 是 GCP 提供的 code repo service 類似 github 的代碼倉庫 功能介面相對簡單(陽春),也支援簡單的 CI 功能 除了建立新的 repo ,他也支援將現有的 repo(如 github 或 Bitbucket 等)連結進來 好處是透過 GCP 的 IAM 整合,管理專案權限相對輕鬆,搭配 `Cloud Build` 可以做到自動整合/部署 使用前記得先產一組 ssh key 註冊到 Source Repo [利用 ssh-keygen 產生金鑰範例](https://docs.microsoft.com/zh-tw/azure/virtual-machines/linux/create-ssh-keys-detailed#generate-keys-with-ssh-keygen) :::warning :musical_note: 這邊建議使用現成的 `id_rsa.pub` ::: [註冊金鑰](https://source.cloud.google.com/user/ssh_keys) 註冊完之後就可以 create/pull/push repo --- ## 現有專案新增 remote url 若是本來就有的專案想要推到 `Cloud Source Repositories`,只要新增一個 remote push url 就好 參考 [Git 如何設定一次 Push 至多個 Remote Repository](https://blog.yowko.com/git-push-multiple-remote-repository/) ```shell= #https git remote set-url --add --push origin https://[gitserver/repository.git] #ssh git remote set-url --add --push origin ssh://[git@gitserver:username/application.git/] ``` 若是使用 `SourceTree` Repository -> Add Remote ![](https://i.imgur.com/LxwYh25.png) 手動點選 Edit Config File... ![](https://i.imgur.com/Zw8GU5x.jpg) 在 [remote "origin"] 裡新增一行 ```shell= pushurl = ssh://[git@gitserver:username/application.git/] ``` 然後存檔 再來就可以新增一筆 commit 測試 push --- ## 透過 Gcloud SDK clone repo 這邊紀錄一下踩雷的過程,在 GCE 上想透過 gcloud sdk clone repo 時,遇到權限不足的情形 這邊有兩個設定必須打開 1. GCE 的 __Cloud API access scopes__ 的權限 (VM Instance 裡的編輯功能) ![](https://i.imgur.com/67OlrRW.png) 2. Cloud Source Repo 的權限列表加入對應的 SA ![](https://i.imgur.com/lfuACWT.png) 之後就可以透過 sdk 指令下載: ```shell= gcloud source repos clone REPO_NAME --project=PROJECT_ID ``` ###### tags: `GCP` `git` `Cloud Source Repositories`