在 Artifact Registry 中儲存 Node.js 套件
本快速入門說明如何設定私人 Artifact Registry Node.js 套件存放區,並上傳套件至該存放區。
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 如果您想透過 Windows 連線至 Node.js 套件存放區,請安裝 PowerShell。
啟動 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」做為格式,並選擇「標準」做為模式。
在「位置類型」下方,選取「區域」,然後選擇位置
us-central1
。按一下 [建立]。
存放區已新增至存放區清單
gcloud
執行下列指令,在
us-central1
位置建立名為quickstart-nodejs-repo
的新 Node.js 套件存放區,並將說明設為「Node.js 存放區」。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-regex
10.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=@quickstart
quickstart
範圍與您的存放區相關聯。當您在指令中加入範圍來發布或安裝套件時,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 publish
yarn publish
查看存放區中的套件
如要確認套件是否已新增至存放區,請按照下列步驟操作:
主控台
在 Google Cloud 控制台中開啟「Repositories」頁面。
在存放區清單中,按一下
quickstart-nodejs-repo
存放區。「Packages」頁面會列出存放區中的套件。
按一下套件即可查看套件版本。
gcloud
如要列出預設 quickstart-nodejs-repo
存放區中的映像檔,請執行下列指令:
gcloud artifacts packages list
如要查看套件的版本,請執行下列指令:
gcloud artifacts versions list --package=PACKAGE
其中 PACKAGE 是套件 ID。
安裝套件
從 npm-package2
中的 Artifact Registry 存放區安裝 emoji-regex
套件。
在 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
後續步驟
- 進一步瞭解如何設定驗證方法
- 瞭解如何管理存放區
- 瞭解如何管理套件
- 參閱開發運作相關資源,並探索我們的研究計畫。