创建并克隆代码库

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

准备工作

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

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. 安装 alpha Google Cloud CLI 组件:
    gcloud components install alpha

所需的角色

如需获得创建代码库所需的权限,请让您的管理员为您授予以下 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. 点击导航菜单右上角的 + 创建新代码库图标。

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

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

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

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

    4. .gitignore:可选。使用下拉菜单模板选择要跟踪哪些文件。

    5. 许可:可选。从下拉菜单中选择一个通用许可。

    6. 默认分支:默认分支的名称。

  5. 点击提交

代码库是在您的 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 替换为实例的区域。如需详细了解区域,请参阅位置

当您将 Git 命令与 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"}

后续步骤