创建并克隆代码库

在 Secure Source Manager 中创建代码库,并将其克隆到本地机器。

准备工作

  1. 创建 Secure Source Manager 实例或请求访问该实例的权限。如需了解详情,请参阅创建 Secure Source Manager 实例
  2. 安装 Google Cloud CLI。

  3. 配置 gcloud CLI 以使用您的联合身份。

    如需了解详情,请参阅使用联合身份登录 gcloud CLI

  4. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

所需角色

如需获得创建代码库所需的权限,请让管理员向您授予以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

如需了解如何授予 Secure Source Manager 角色,请参阅使用 IAM 进行 访问权限控制向用户授予实例访问权限

创建代码库

网页界面

  1. 如需通过网页界面访问 Secure Source Manager 实例,请将以下网址复制到浏览器地址栏中。

    INSTANCE_ID-PROJECT_NUMBER.LOCATION.sourcemanager.dev

    请替换以下内容:

    • INSTANCE_ID 替换为实例名称。
    • PROJECT_NUMBER 替换为实例的 Google Cloud 项目编号。如需了解如何识别项目,请参阅 识别项目
    • LOCATION 替换为实例的区域。

  2. 根据需要使用 Secure Source Manager 凭据进行身份验证。

  3. 在导航菜单中,点击 + Create new repository 图标。

  4. 填写代码库详细信息:

    1. 代码库 ID:输入代码库的名称。

    2. 说明:可选:输入代码库的说明。

    3. Default branch:默认分支的名称。

  5. Initialize repository:可选:如果您 想初始化代码库并添加 .gitignore、许可和 README 文件,请选择此选项。

  6. .gitignore:可选:使用下拉菜单模板选择要排除的文件。

  7. license:可选:从下拉菜单中选择一个常用许可。

  8. 点击创建代码库

代码库将在您的 Secure Source Manager 实例中创建。

创建新代码库时,系统会向您授予 Repo Admin 角色 (roles/securesourcemanager.repoAdmin)。新的 IAM 权限可能需要长达 2 分钟才能传播,因此,如果您在创建后尝试访问代码库时收到权限错误,请等待几分钟,然后重试。

您创建的代码库会列在 Secure Source Manager 网页界面中的我的代码库 页面上。

设置凭据

运行以下命令,将 Secure Source Manager 身份验证帮助程序添加到全局 Git 配置:

Linux

git config --global credential.'https://*.*.sourcemanager.dev'.helper gcloud.sh

早期版本的 Git 可能不支持通配符。如需添加不带通配符的身份验证帮助程序,请运行以下命令:

git config --global credential.'https://INSTANCE_ID-PROJECT_NUMBER-git.LOCATION.sourcemanager.dev'.helper gcloud.sh

Windows

git config --global credential.https://*.*.sourcemanager.dev.helper gcloud.cmd

早期版本的 Git 可能不支持通配符。如需添加不带通配符的身份验证帮助程序,请运行以下命令:

git config --global credential.https://INSTANCE_ID-PROJECT_NUMBER-git.LOCATION.sourcemanager.dev.helper gcloud.cmd

请替换以下内容:

  • INSTANCE_ID 替换为 Secure Source Manager 实例的名称。
  • PROJECT_NUMBER 替换为您的项目编号。如需查找项目编号方面的帮助,请参阅 识别项目
  • LOCATION 替换为实例的区域。如需详细了解区域,请参阅位置

使用 Secure Source Manager 时,身份验证帮助程序会使用 gcloud CLI 来提取您的 Google Cloud 凭据。

如需在初始凭据设置后重新进行身份验证,请运行以下 gcloud CLI 命令:

gcloud auth login

克隆代码库

  1. 在 Secure Source Manager 网页界面中,前往您的代码库页面。

  2. 复制代码库页面顶部的 HTTPS 网址。

  3. 运行以下命令来克隆代码库:

    git clone REPOSITORY_URL
    

    其中 REPOSITORY_URL 是您要克隆的代码库页面顶部显示的 HTTPS 网址。

    忽略有关克隆空代码库的警告。

推送到代码库

  1. 在终端窗口中,将目录更改为克隆的代码库。

    cd REPOSITORY_NAME
    

    其中 REPOSITORY_NAME 是您的代码库名称。

  2. 创建一个空的 README 文件:

    touch README.md
    
  3. 将 README 文件添加到 Git 并创建提交消息:

    git add README.md
    git commit -m "initial commit"
    
  4. 将更改推送到远程代码库:

    git push -u origin main
    

    Git 将文件从主分支推送到 origin 远程分支。输出类似于以下内容:

    Enumerating objects: 3, done.
    Counting objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 211 bytes | 211.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    remote: . Processing 1 references
    remote: Processed 1 references in total
    To https://instance-id-123456789012-git.us-central1.sourcemanager.dev/my-project/my-repo.git
    * [new branch]      main -> main
    branch 'main' set up to track 'origin/main'.
    

在 Secure Source Manager 中查看文件

在 Secure Source Manager 网页界面中,点击代码库的名称。

您的代码库页面随即打开,您的 README.md 文件会列在 <> Code 标签页中。

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

在移除代码库之前,请确保已在其他位置保存了您要保留的任何文件。

在使用任何请求数据之前, 请先进行以下替换:

  • PROJECT_ID:运行 Secure Source Manager 的 Google Cloud 项目的项目 ID
  • LOCATION:实例所在的区域。如需了解支持的位置,请参阅位置
  • REPOSITORY_ID:代码库 ID。

HTTP 方法和网址:

DELETE https://securesourcemanager.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/repositories/REPOSITORY_ID

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

"name":"operations/cc3ea26c-9b57-11ed-be0a-2e3b5910efef","metadata":{"@type":"type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata","createTime":"2023-01-23T19:54:49.904779921Z","endTime":"2023-01-23T19:54:50.947030402Z","target":"projects/my-project/locations/us-central1/repositories/my-repo","verb":"delete","apiVersion":"v1"},"done":true,"response":{"@type":"type.googleapis.com/google.protobuf.Empty"}
```

响应类似如下内容:

{"name":"operations/cc3ea26c-9b57-11ed-be0a-2e3b5910efef","metadata":{"@type":"type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata","createTime":"2023-01-23T19:54:49.904779921Z","endTime":"2023-01-23T19:54:50.947030402Z","target":"projects/my-project/locations/us-central1/repositories/my-repo","verb":"delete","apiVersion":"v1"},"done":true,"response":{"@type":"type.googleapis.com/google.protobuf.Empty"}

后续步骤