在 Artifact Registry 中存储其他格式

了解如何设置 Artifact Registry 通用格式代码库并上传 YAML 文件。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  8. Verify that billing is enabled for your Google Cloud project.

  9. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

所需的角色

如需获得创建和管理 Artifact Registry 软件包代码库所需的权限,请让您的管理员为您授予项目的Artifact Registry 管理员 (roles/artifactregistry.admin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

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

启动 Cloud Shell

在 Google Cloud 控制台中,激活 Cloud Shell。

激活 Cloud Shell

Cloud Shell 会话随即会在控制台的底部启动,并显示命令行提示符。 Google Cloud Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境 。该会话可能需要几秒钟时间来完成初始化。

在本快速入门中,您将使用 Cloud Shell 运行 gcloud 命令。

创建通用代码库

  1. 如需在位置 us-central1 中创建一个名为 quickstart-generic-repo 且带有说明 Generic repository 的通用格式代码库,请运行以下命令:

    gcloud artifacts repositories create quickstart-generic-repo \
        --repository-format=generic \
        --location=us-central1 \
        --description="Generic repository"
    
  2. 如需验证是否已创建代码库,请运行以下命令:

    gcloud artifacts repositories list
    
  3. 如需简化 gcloud 命令,请运行以下命令,将默认代码库设置为 quickstart-generic-repo,并将默认位置设置为 us-central1

    1. 如需将默认代码库设置为 quickstart-generic-repo,请运行以下命令:

      gcloud config set artifacts/repository quickstart-generic-repo
      
    2. 如需将默认位置设置为 us-central1,请运行以下命令:

      gcloud config set artifacts/location us-central1
      

    设置以上值之后,您无需在需要代码库或位置的 gcloud 命令中指定这些值。

将制品上传到代码库

  1. 在您的主目录中,创建一个要上传到代码库的文件:

    echo "hello world" > hello.yaml
    
  2. 如需将该文件作为制品上传到代码库,请运行以下命令:

    gcloud artifacts generic upload \
        --source=hello.yaml \
        --package=my-package \
        --version=1.0.0
    

    其中:

    • hello.yaml 是要上传的文件的路径。
    • my-package 是要上传的软件包。
    • 1.0.0 是制品的版本。您无法覆盖代码库中的现有版本。

查看代码库中的制品

如需验证制品是否已添加到代码库,您可以运行以下命令列出所有制品:

gcloud artifacts files list

响应包含格式为 PACKAGE:VERSION:FILE_NAME的文件详细信息。

在以下示例中,hello.yamlFILE_NAME

FILE: my-package:1.0.0:hello.yaml
CREATE_TIME: 2023-03-09T20:55:07
UPDATE_TIME: 2023-03-09T20:55:07
SIZE (MB): 0.000
OWNER: projects/my-project/locations/us-central1/repositories/quickstart-generic-repo/packages/my-package/versions/1.0.0

下载通用制品

如需从代码库下载通用制品,请运行以下命令:

gcloud artifacts generic download \
    --name=hello.yaml \
    --package=my-package \
    --version=1.0.0 \
    --destination=DESTINATION

其中:

  • hello.yaml 是要下载的文件的名称。
  • my-package 是要下载的软件包。
  • 1.0.0 是制品的版本。

DESTINATION 替换为本地文件系统中要保存下载内容的目录。目标文件夹必须已存在,否则该命令将失败。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留该项目但删除代码库。

删除项目

    删除项目: Google Cloud

    gcloud projects delete PROJECT_ID

删除代码库

在移除代码库之前,请确保要保留的任何软件包在其他位置提供。

  1. 如需删除 quickstart-generic-repo 代码库,请运行以下命令:

    gcloud artifacts repositories delete quickstart-generic-repo
    
  2. 如果要移除您为活跃 gcloud 配置配置的默认代码库和位置设置,请运行以下命令:

    gcloud config unset artifacts/repository
    gcloud config unset artifacts/location
    

后续步骤