在 Artifact Registry 中儲存 RPM 套件

本快速入門導覽說明如何設定私有 Artifact Registry Yum 存放區、將 RPM 套件新增至存放區,以及在執行 CentOS 7 作業系統的 Compute Engine VM 上安裝套件。如要瞭解如何設定使用 DNF 的 Yum 存放區,請參閱「設定 VM 來安裝 RPM 套件」。

如要進一步瞭解如何管理 RPM 套件,請參閱「使用 RPM 套件」。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API.

    Enable the API

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

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Artifact Registry API.

    Enable the API

啟動 Cloud Shell

Cloud Shell 已預先安裝 Google Cloud CLI。gcloud CLI 是 Google Cloud的主要指令列介面。

啟動 Cloud Shell:

  1. 前往 Google Cloud 控制台。

    Google Cloud console

  2. 在 Google Cloud 控制台中,按一下「啟用 Cloud Shell」 「啟用 Cloud Shell」按鈕

此時 Cloud Shell 工作階段會在主控台底部的頁框中開啟,您將使用這個殼層執行 gcloud 指令,建立 VM 和存放區。

建立存放區

建立構件存放區。

  1. 建立存放區:

    主控台

    1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

      開啟「存放區」頁面

    2. 點選 [Create Repository] (建立存放區)

    3. 指定 quickstart-yum-repo 做為存放區名稱。

    4. 選取「Yum」做為格式。

    5. 在「位置類型」下方,選取「區域」,接著選擇位置 us-west1

    6. 點選「建立」

    存放區會新增至存放區清單。

    gcloud

    1. 在 Cloud Shell 中執行下列指令,以便在 us-west1 位置中的 quickstart-yum-repo 當前專案內建立新的 Yum 存放區。

      gcloud artifacts repositories create quickstart-yum-repo \
          --repository-format=yum \
          --location=us-west1 \
          --description="Yum repository"
      
    2. 執行下列指令來確認存放區已建立完成:

      gcloud artifacts repositories list
      

您現在可以將套件新增至存放區。

將套件新增至存放區

您可以使用 Google Cloud CLI 將套件上傳至存放區,也可以匯入儲存在 Cloud Storage 中的套件。如果您使用 Cloud Build 建構套件,建構作業會將套件儲存在 Cloud Storage 中,供您匯入。

在本快速入門導覽課程中,您將使用 gcloud artifacts yum upload 指令上傳範例檔案。

  1. 在 Cloud Shell 中,使用下列指令下載 nano 文字編輯器:

    sudo yum install --downloaddir=. --downloadonly nano
    

    Yum 會從您設定的 CentOS 存放區下載最新版本的套件。

    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirror.dal.nexril.net
     * epel: ord.mirror.rackspace.com
     * extras: centos.mirror.lstn.net
     * updates: ftpmirror.your.org
    Resolving Dependencies
    --> Running transaction check
    ---> Package nano.x86_64 0:2.3.1-10.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package        Arch             Version                   Repository      Size
    ================================================================================
    Installing:
     nano           x86_64           2.3.1-10.el7              base           440 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package
    
    Total download size: 440 k
    Installed size: 1.6 M
    Background downloading packages, then exiting:
    nano-2.3.1-10.el7.x86_64.rpm                               | 440 kB   00:00
    exiting because "Download Only" specified
    

    執行 ls 取得套件的檔案名稱。檔案名稱類似於 nano-2.3.1-10.el7.x86_64.rpm

  2. 如要簡化 gcloud 指令,請將預設存放區設為 quickstart-yum-repo,預設位置設為 us-west1。 設定值後,您不需要在需要存放區或位置的 gcloud 指令中指定這些值。

    如要設定存放區,請執行下列指令:

    gcloud config set artifacts/repository quickstart-yum-repo
    

    如要設定位置,請執行下列指令:

    gcloud config set artifacts/location us-west1
    

    如要進一步瞭解這些指令,請參閱 gcloud config set 說明文件。

  3. 執行 gcloud artifacts yum upload,將套件上傳至存放區:

    gcloud artifacts yum upload quickstart-yum-repo \
        --source=FILE_NAME
    

    FILE_NAME 替換為 Nano 套件的路徑。

查看存放區中的套件

確認套件已新增至存放區。

主控台

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「存放區」頁面

  2. 在存放區清單中,按一下「quickstart-yum-repo」存放區。

    「Packages」頁面會列出存放區中的套件。

gcloud

如要列出 quickstart-yum-repo 存放區中的套件,請執行下列指令:

 gcloud artifacts packages list

如要查看 quickstart-yum-repo 中套件的版本,請執行下列指令:

gcloud artifacts versions list --package=nano

建立 VM

建立新的 Compute Engine VM,並在其中安裝範例套件。

在 Cloud Shell 中執行下列指令,建立名為 quickstart-yum-vm 的 VM 執行個體。

gcloud compute instances create quickstart-yum-vm \
    --image-family=centos-7 \
    --image-project=centos-cloud \
    --scopes=cloud-platform

根據預設,VM 沒有使用存放區所需的存取權範圍--scopes 旗標會將 VM 的存取範圍設為 cloud-platform

設定套件管理工具

如要在 VM 上安裝套件,請將您建立的存放區新增至定義套件存放區的 Yum 檔案。

  1. 前往「VM instances」(VM 執行個體) 頁面。

    開啟「VM instances」(VM 執行個體) 頁面

  2. 在 VM 所在資料列中,按一下「SSH」SSH。新視窗會隨即開啟,顯示 VM 上的終端機工作階段。

  3. 更新 Yum:

    sudo yum makecache
    
  4. 在 VM 上安裝 Yum 憑證輔助程式,讓 Yum 能夠執行驗證,請使用下列指令:

    sudo yum install yum-plugin-artifact-registry
    
  5. 使用下列指令,將 VM 設為存取 Artifact Registry 套件:

    sudo tee -a /etc/yum.repos.d/artifact-registry.repo << EOF
    [yum-quickstart]
    name=My Repository
    baseurl=https://us-west1-yum.pkg.dev/projects/PROJECT/quickstart-yum-repo
    enabled=1
    repo_gpgcheck=0
    gpgcheck=0
    EOF
    

    PROJECT 替換為 Google Cloud專案 ID

安裝套件

安裝您新增至存放區的套件。

  1. 更新可用套件清單:

    sudo yum makecache
    
  2. 在存放區中安裝套件。

    sudo yum --enablerepo=yum-quickstart install nano
    

    在系統提示時輸入 y

    傳回的安裝資訊如下列範例所示:

    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.cmich.edu
     * epel: ord.mirror.rackspace.com
     * extras: mirror.team-cymru.com
     * updates: mirror.us-midwest-1.nexcess.net
    quickstart-yum-repo                                      | 1.3 kB     00:00
    Resolving Dependencies
    --> Running transaction check
    ---> Package nano.x86_64 0:2.3.1-10.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package        Arch             Version                   Repository      Size
    ================================================================================
    Installing:
     nano           x86_64           2.3.1-10.el7              base           440 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package
    
    Total size: 440 k
    Installed size: 1.6 M
    Is this ok [y/d/N]: y
    Downloading packages:
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : nano-2.3.1-10.el7.x86_64                                     1/1
      Verifying  : nano-2.3.1-10.el7.x86_64                                     1/1
    
    Installed:
      nano.x86_64 0:2.3.1-10.el7
    
    Complete!
    

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

移除存放區之前,請先確認要保留的套件均已存放於其他位置。

如要刪除存放區,請按照下列指示操作:

主控台

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「Repositories」(存放區) 頁面

  2. 在存放區清單中,選取 quickstart-yum-repo 存放區。

  3. 點選「刪除」。

gcloud

  1. 如要刪除 quickstart-yum-repo 存放區,請執行下列指令:

    gcloud artifacts repositories delete quickstart-yum-repo
    
  2. 如要移除您為使用中的 gcloud 設定建立的預設存放區和位置設定,請執行下列指令:

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

如要刪除您建立的 VM,請執行下列指令:

gcloud compute instances delete quickstart-yum-vm

後續步驟