在 Artifact Registry 中儲存 Node.js 套件
本快速入門導覽課程說明如何設定私人的 Artifact Registry Node.js 套件存放區,並將套件上傳至該存放區。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init - 如要從 Windows 連線至 Node.js 套件存放區,請安裝 PowerShell。
必要的角色
如要取得建立及管理 Artifact Registry Node.js 套件存放區所需的權限,請要求管理員授予您專案的 Artifact Registry 管理員 (roles/artifactregistry.admin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
啟動 Cloud Shell
在本快速入門導覽課程中,您將使用 Cloud Shell,這是用來管理Google Cloud上託管資源的殼層環境。
Cloud Shell 已預先安裝 Google Cloud CLI。gcloud CLI 是 Google Cloud的主要指令列介面。
啟動 Cloud Shell:
前往 Google Cloud 控制台。
在 Google Cloud 控制台工具列中,點按「啟用 Cloud Shell」:

此時 Cloud Shell 工作階段會在主控台底部的頁框中開啟,您可以使用這個殼層來執行 gcloud 指令。
建立 Node.js 套件存放區
為 Node.js 構件建立存放區。
建立存放區。
控制台
在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。
點選「Create Repository」。
指定
quickstart-nodejs-repo做為存放區名稱。選擇「npm」做為格式,並選擇「Standard」做為模式。
在「位置類型」下方,選取「區域」,接著選擇位置
us-central1。點選「建立」。
存放區會新增至存放區清單
gcloud
執行下列指令,在
us-central1位置建立名為quickstart-nodejs-repo的新 Node.js 套件存放區,並將說明設為「Node.js repository」。gcloud artifacts repositories create quickstart-nodejs-repo --repository-format=npm \ --location=us-central1 --description="Node.js package repository"執行下列指令來確認存放區已建立完成:
gcloud artifacts repositories list
如要進一步瞭解 Artifact Registry 指令,請執行
gcloud artifacts指令。如要簡化
gcloud指令,請將預設存放區設為quickstart-nodejs-repo,預設位置設為us-central1。 設定值後,您不需要在需要存放區或位置的gcloud指令中指定這些值。如要設定存放區,請執行下列指令:
gcloud config set artifacts/repository quickstart-nodejs-repo如要設定位置,請執行下列指令:
gcloud config set artifacts/location us-central1如要進一步瞭解這些指令,請參閱 gcloud config set 說明文件。
設定 npm
如要上傳及下載套件,您必須設定 npm 專案,以便向建立的存放區進行驗證。在本快速入門導覽課程中,您將上傳套件,然後在第二個套件中將其安裝為依附元件。
下載 emoji-regex 套件,這個套件提供規則運算式,可比對所有表情符號和序列。
npm pack emoji-regex這個指令會下載套件的封存檔。
將封存檔解壓縮至
emoji-regex目錄。下列指令適用於emoji-regex10.1.0 版的封存檔:mkdir emoji-regex && tar xvf emoji-regex-10.1.0.tgz -C emoji-regex --strip-components 1建立第二個 npm 套件,用來從 Artifact Registry 安裝
emoji-regex套件。請使用npm init指令建立這個套件的基本套件。mkdir npm-package2 cd npm-package2 npm init -y系統提示時,請接受預設值。
設定
emoji-regex和npm-package2專案,向您建立的 Artifact Registry 存放區進行驗證。執行下列指令。這項指令會傳回要新增至 npm 設定檔的設定。
gcloud artifacts print-settings npm --scope=@quickstartquickstart範圍與存放區相關聯。在發布或安裝套件的指令中加入範圍時,npm 會使用您的存放區。發布或安裝套件時,如果沒有範圍,系統會使用您設定的預設存放區。詳情請參閱 Node.js 總覽。gcloud指令的輸出內容如下列範例所示。 PROJECT 是您的 Google Cloud 專案 ID。@quickstart:registry=https://us-central1-npm.pkg.dev/PROJECT/quickstart-nodejs-repo/ //us-central1-npm.pkg.dev/PROJECT/quickstart-nodejs-repo/:always-auth=true將上一個步驟中的設定新增至每個 npm 專案的專案
.npmrc檔案。這個檔案與package.json檔案位於同一個目錄中。如要進一步瞭解
.npmrc檔案,請參閱 Node.js 總覽。每個 Artifact Registry Node.js 套件存放區都會與 npm 登錄端點
https://LOCATION-npm.pkg.dev/PROJECT/REPOSITORY建立關聯。如果您建立另一個 Artifact Registry Node.js 套件存放區,npm 會將其視為具有專屬範圍的獨立登錄檔。
在
emoji-regex和npm-package2專案中編輯package.json。確認
name的值包含quickstart範圍。- 針對
emoji-regex:
"name": "@quickstart/emoji-regex"- 針對
npm-package2:
"name": "@quickstart/npm-package2"- 針對
在
scripts下方,為 google-artifactregistry-auth 新增指令碼,這是用於更新 Artifact Registry 存放區憑證的用戶端程式庫。"scripts": { "artifactregistry-login": "npx google-artifactregistry-auth --repo-config=./.npmrc --credential-config=./.npmrc" }
將套件上傳至存放區
更新存取權杖,以便連線至存放區。
npm run artifactregistry-login將套件新增至存放區。您可以使用
npm或yarn指令。npm publishyarn publish
查看存放區中的套件
如要確認套件已新增至存放區,請按照下列步驟操作:
控制台
在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。
在存放區清單中,按一下
quickstart-nodejs-repo存放區。「Packages」(套件) 頁面會列出存放區中的套件。
按一下套件即可查看套件版本。
gcloud
如要列出預設 quickstart-nodejs-repo 存放區中的映像檔,請執行下列指令:
gcloud artifacts packages list
如要查看套件的版本,請執行下列指令:
gcloud artifacts versions list --package=PACKAGE
其中的 PACKAGE 為套件 ID。
安裝套件
從 Artifact Registry 存放區安裝 emoji-regex 套件 (位於 npm-package2 中)。
在 npm-package2 目錄中,執行 npm install 或 yarn add 指令:
npm install @quickstart/emoji-regex
yarn add @quickstart/emoji-regex
這個指令會在 npm-package2 專案中安裝 emoji-regex 套件。同時更新 package.json,將 emoji-regex 設為依附元件。
"dependencies": {
"@quickstart/emoji-regex": "^10.1.0"
}
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
移除存放區之前,請先確認要保留的套件均已存放於其他位置。
如要刪除存放區,請按照下列指示操作:
控制台
在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。
在存放區清單中,選取
quickstart-nodejs-repo存放區。點選「刪除」。
gcloud
如要刪除
quickstart-nodejs-repo存放區,請執行下列指令:gcloud artifacts repositories delete quickstart-nodejs-repo如要移除您為使用中的
gcloud設定建立的預設存放區和位置設定,請執行下列指令:gcloud config unset artifacts/repository gcloud config unset artifacts/location
後續步驟
- 進一步瞭解如何設定驗證方法
- 瞭解如何管理存放區
- 瞭解如何管理套件
- 參閱開發運作相關資源,並探索我們的研究計畫。