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.
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
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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. -
Install the Google Cloud CLI.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して 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
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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. -
Install the Google Cloud CLI.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init - Windows から Node.js パッケージ リポジトリに接続する場合は、PowerShell をインストールします。
Google Cloud コンソールに移動します。
Google Cloud コンソールのツールバーで、「Cloud Shell をアクティブにする」をクリックします:

リポジトリを作成します。
コンソール
Google Cloud コンソールで [リポジトリ] ページを開きます。
[リポジトリを作成] をクリックします。
リポジトリ名として
quickstart-nodejs-repoを指定します。形式として npm を選択し、モードとして Standard を選択します。
[ロケーション タイプ] で、[リージョン] を選択し、ロケーション
us-central1を選択します。[作成] をクリックします。
このリポジトリがリポジトリ リストに追加されます。
gcloud
次のコマンドを実行して、「Node.js リポジトリ」という説明の付いたロケーション
us-central1にquickstart-nodejs-repoという新しい 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 のドキュメントをご覧ください。
すべての絵文字記号とシーケンスに一致する正規表現を提供するシンプルなパッケージである 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 1Artifact Registry から
emoji-regexパッケージをインストールするために使用する 2 番目の npm パッケージを作成します。このパッケージでは、npm initコマンドを使用して基本パッケージを作成します。mkdir npm-package2 cd npm-package2 npm init -yプロンプトが表示されたら、デフォルト値を受け入れます。
作成した Artifact Registry リポジトリで認証するように
emoji-regexプロジェクトとnpm-package2プロジェクトの両方を構成します。以下のコマンドを実行します。このコマンドは、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に、Artifact Registry リポジトリの認証情報を更新するクライアント ライブラリである google-artifactregistry-auth のスクリプトを追加します。"scripts": { "artifactregistry-login": "npx google-artifactregistry-auth --repo-config=./.npmrc --credential-config=./.npmrc" }
リポジトリに接続するためのアクセス トークンを更新します。
npm run artifactregistry-loginパッケージをリポジトリに追加します。
npmコマンドまたはyarnコマンドを使用できます。npm publishyarn publishGoogle Cloud コンソールで [リポジトリ] ページを開きます。
リポジトリ リストで、
quickstart-nodejs-repoリポジトリをクリックします。[パッケージ] ページに、リポジトリ内のパッケージが一覧表示されます。
パッケージをクリックして、パッケージのバージョンを表示します。
Google Cloud コンソールで [リポジトリ] ページを開きます。
リポジトリ リストで、
quickstart-nodejs-repoリポジトリを選択します。[削除] をクリックします。
quickstart-nodejs-repoリポジトリを削除するには、次のコマンドを実行します。gcloud artifacts repositories delete quickstart-nodejs-repoアクティブな
gcloud構成のデフォルトのリポジトリとロケーション設定を削除するには、次のコマンドを実行します。gcloud config unset artifacts/repository gcloud config unset artifacts/location- 認証の構成の詳細を確認する
- リポジトリの管理について確認する
- パッケージの管理について確認する
- DevOps に関するリソースを読む。また、研究プログラムについて確認する。
Cloud Shell を起動する
このクイックスタートでは、Google Cloudでホストされているリソースを管理するためのシェル環境である Cloud Shell を使用します。
Cloud Shell には、Google Cloud CLI がプリインストールされています。gcloud CLI は、 Google Cloudの主要なコマンドライン インターフェースを提供します。
Cloud Shell を起動します。
コンソールの下部にあるフレーム内で Cloud Shell セッションが開きます。このシェルで gcloud コマンドを実行します。
Node.js パッケージ リポジトリを作成する
Node.js アーティファクトのリポジトリを作成します。
npm を構成する
パッケージをアップロードしてダウンロードするには、作成したリポジトリで認証するための設定を使用して npm プロジェクトを構成する必要があります。このクイックスタートでは、簡単なパッケージをアップロードし、依存関係として別のパッケージにインストールします。
パッケージをリポジトリにアップロードする
リポジトリ内のパッケージを表示する
パッケージがリポジトリに追加されたことを確認するには:
コンソール
gcloud
デフォルトの quickstart-nodejs-repo リポジトリ内のイメージを一覧表示するには、次のコマンドを実行します。
gcloud artifacts packages list
パッケージのバージョンを表示するには、次のコマンドを実行します。
gcloud artifacts versions list --package=PACKAGE
PACKAGE はパッケージ ID です。
パッケージのインストール
emoji-regex パッケージを npm-package2 の Artifact Registry リポジトリからインストールします。
npm-package2 ディレクトリで、次の npm install コマンドまたは yarn add コマンドを実行します。
npm install @quickstart/emoji-regex
yarn add @quickstart/emoji-regex
このコマンドにより、emoji-regex パッケージが npm-package2 プロジェクトにインストールされます。また、package.json を更新して emoji-regex を依存関係として設定します。
"dependencies": {
"@quickstart/emoji-regex": "^10.1.0"
}
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。
リポジトリを削除する前に、保持する必要があるパッケージが別の場所で使用可能であることを確認してください。
リポジトリを削除するには、次の手順を行います。