配置虚拟机以安装 Debian 软件包

本页面介绍了如何配置虚拟机以从 Artifact Registry Apt 代码库安装 Debian 软件包。

准备工作

如果目标 Apt 代码库不存在,请创建新的标准代码库。您可以创建私有代码库,也可以创建不需要身份验证的公共代码库。

准备虚拟机以访问 Apt 代码库

私有代码库

  1. 选择您将用于授予访问权限的服务账号。

    • 对于 Compute Engine 虚拟机,这可能是默认服务账号。对于使用服务账号的任何虚拟机,Artifact Registry 都会自动检索 Compute Engine 虚拟机服务账号凭据。
    • 对于其他虚拟机,请创建或选择一个服务账号来代表您的虚拟机执行操作。您需要服务账号密钥文件所在的位置,才能配置虚拟机。您可以在服务账号页面上查看和创建现有账号的密钥。
  2. 向您的服务账号授予代码库访问权限

  3. 准备虚拟机以访问代码库。

    Compute Engine 虚拟机

    Debian 虚拟机

    1. 为虚拟机分配 cloud-platform API 访问权限范围。如需了解如何设置访问权限范围,请参阅更改实例的服务账号和访问权限范围

    2. 使用以下命令更新 Apt:

      sudo apt update
      
    3. 在虚拟机上安装 apt-transport-artifact-registry 软件包:

      sudo apt install apt-transport-artifact-registry
      

    Ubuntu 虚拟机

    1. 使用以下命令安装 Apt 代码库签名密钥:

      curl https://LOCATION-apt.pkg.dev/doc/repo-signing-key.gpg | sudo apt-key add - && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      

      LOCATION 替换为代码库的区域位置

    2. 为虚拟机分配 cloud-platform API 访问权限范围。如需了解如何设置访问权限范围,请参阅更改实例的服务账号和访问权限范围

    3. 使用以下命令将虚拟机配置为访问 Artifact Registry 软件包:

      echo 'deb http://packages.cloud.google.com/apt apt-transport-artifact-registry-stable main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
      
    4. 使用以下命令更新 Apt:

      sudo apt update
      
    5. 在虚拟机上安装 apt-transport-artifact-registry 软件包:

      sudo apt install apt-transport-artifact-registry
      

    其他虚拟主机

    1. 使用以下命令安装 Apt 代码库签名密钥:

      curl https://LOCATION-apt.pkg.dev/doc/repo-signing-key.gpg | sudo apt-key add - && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      

      LOCATION 替换为代码库的区域位置

    2. 使用以下命令将虚拟机配置为访问 Artifact Registry 软件包:

      echo 'deb http://packages.cloud.google.com/apt apt-transport-artifact-registry-stable main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
      
    3. 使用以下命令更新 Apt:

      sudo apt update
      
    4. 在虚拟机上安装 apt-transport-artifact-registry 软件包:

      sudo apt install apt-transport-artifact-registry
      
    5. /etc/apt/apt.conf.d/90artifact-registry 文件中找到 #Service-Account-JSON "/path/to/creds.json"; 行,然后取消该行的注释并添加服务账号密钥的路径。

      配置文件条目:

      Service-Account-JSON  "PATH_TO_SERVICE_ACCOUNT_KEY";
      

      PATH_TO_SERVICE_ACCOUNT_KEY 替换为服务账号密钥 JSON 文件的路径。

公开代码库

  1. 配置对代码库的公共访问权限

  2. 如果您要在 Trusted Cloud之外配置虚拟机,请准备好您的机器以访问该代码库。如果您要配置 Compute Engine 虚拟机,请继续执行下一步。

    使用以下命令安装公开签名验证密钥:

    curl https://LOCATION-apt.pkg.dev/doc/repo-signing-key.gpg | sudo apt-key add -
    

    LOCATION 替换为代码库的区域位置

  3. 使用以下命令更新 Apt:

    sudo apt update
    

配置虚拟机以访问标准 Apt 代码库

配置虚拟机以访问 Apt 代码库:

  1. 运行 gcloud beta artifacts print-settings apt 命令,生成虚拟机配置命令:

    gcloud beta artifacts print-settings apt \
        --repository=REPOSITORY \
        --location=LOCATION
    

    替换以下内容:

    • LOCATION 是代码库的区域位置
    • REPOSITORY 是 Artifact Registry 代码库的名称。

    输出类似于以下内容:

    # To configure your package manager with this repository:
    
    # Update Apt:
    sudo apt update
    
    # Install the Apt credential helper:
    sudo apt install apt-transport-artifact-registry
    
    # Configure your VM to access Artifact Registry packages using the following
    # command:
    
    echo "deb ar+https://LOCATION-apt.pkg.dev/projects/PROJECT REPOSITORY main" | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
    
    # Update Apt:
    sudo apt update
    
  2. 使用 print-settings 命令输出中的 echo 命令,配置 Apt 以从您的代码库中提取软件包。

    例如,如需安装项目 my-project 中位置 u-france-east1 的公共代码库 my-repo 中的软件包,请使用以下命令:

    echo 'deb ar+https://u-france-east1-apt.pkg.dev/projects/my-project my-repo main'
    
  3. 使用以下命令更新代码库来源:

    sudo apt update
    

Apt 现在可以连接到代码库。

如果您创建了其他 Apt 代码库,可以将其添加到 artifact-registry.list 文件中,然后重新运行 sudo apt update 以更新代码库来源。

配置对 Apt 代码库的 HTTP 访问权限

如果您使用的是不支持 SSL 加密的旧版客户端工具,则可以配置对 Apt 代码库的公开访问权限,并通过 HTTP 访问该代码库。如果发送给公开可读代码库的请求使用 HTTP 协议,并且请求中包含身份验证令牌,则该请求将被拒绝。

  1. 配置对代码库的公共访问权限

  2. 如需配置 Apt 以使用 HTTP 从您的代码库中提取软件包,请运行以下命令:

     echo 'deb http://LOCATION-apt.pkg.dev/projects/PROJECT_ID \
     REPOSITORY main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
    

    替换以下内容:

    • LOCATION 是代码库的区域位置
    • PROJECT_ID 是代码库的项目 ID。
    • REPOSITORY 是代码库名称。

    例如,如需使用 HTTP 从项目 my-project 中位于位置 u-france-east1 的公共代码库 my-repo 安装软件包,请使用以下命令:

    echo 'deb http://u-france-east1-apt.pkg.dev/projects/my-project my-repo main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
    
  3. 使用以下命令更新代码库来源:

    sudo apt update
    

Apt 现在可以连接到代码库。

如果您创建了其他 Apt 代码库,可以将其添加到 artifact-registry.list 文件中,然后重新运行 sudo apt update 以更新代码库来源。

后续步骤