在 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

Google Cloud 主控台底部會開啟一個 Cloud Shell 工作階段,並顯示指令列提示。Cloud Shell 是已安裝 Google Cloud CLI 的殼層環境,並已針對您目前的專案設定好相關值。工作階段可能要幾秒鐘的時間才能初始化。

在本快速入門導覽課程中,您將使用 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
    

後續步驟