谷歌云代理商:‌如何用谷歌云服务器搭建私有Git仓库?‌

72 阅读2分钟

本文由【云老大】 TG@yunlaoda360 撰写

方法一:使用 Google Cloud Source Repositories

  1. 创建 Google Cloud 项目 :如果还没有创建项目,需在 Google Cloud Console 中创建一个新项目。
  2. 启用相关 API :在 Google Cloud Console 中启用 Cloud Source Repositories API。
  3. 安装 Git 和 Google Cloud CLI :在本地机器上安装 Git 和 Google Cloud CLI。
  4. 初始化 Google Cloud CLI :使用命令 gcloud init 初始化 CLI,按照提示登录账号并选择项目。
  5. 创建私有 Git 仓库 :运行命令 gcloud source repos create [REPO_NAME],将 [REPO_NAME] 替换为仓库名称。
  6. 将仓库添加为远程仓库 :在本地 Git 仓库中运行命令 git remote add google https://source.developers.google.com/p/[PROJECT_ID]/r/[REPO_NAME],将 [PROJECT_ID][REPO_NAME] 替换为实际的项目 ID 和仓库名称。
  7. 推送代码到私有仓库 :运行命令 git push google master,将本地代码推送到私有仓库。

方法二:手动搭建 Git 服务器

  1. 创建谷歌云服务器实例 :在 Google Cloud Console 中创建一个 Compute Engine 虚拟机实例,选择合适的操作系统和配置。
  2. 连接到服务器 :使用 SSH 工具连接到创建的实例。
  3. 安装 Git :在服务器上运行命令 sudo apt updatesudo apt install git 安装 Git。
  4. 创建 Git 用户 :运行命令 sudo adduser git 创建一个专门用于运行 Git 服务的用户。
  5. 创建证书登录 :收集所有需要登录的用户的公钥,将其导入到 /home/git/.ssh/authorized_keys 文件中。
  6. 初始化 Git 仓库 :在服务器上选定一个目录作为 Git 仓库,如 /srv/sample.git,然后运行命令 sudo git init --bare sample.git 初始化裸仓库,并将仓库的拥有者改为 git 用户。
  7. 克隆仓库到本地 :在本地机器上运行命令 git clone git@[SERVER_IP]:/srv/sample.git,将 [SERVER_IP] 替换为服务器的公网 IP 地址。