このページでは、Artifact Registry に保存されているパッケージ化された Go モジュールを管理する方法について説明します。
準備
- ターゲット リポジトリが存在しない場合は、新しいリポジトリを作成します。リポジトリ形式として Go を選択します。
- リポジトリに必要な権限が付与されていることを確認します。
- (省略可)gcloud コマンドのデフォルトを構成します。
- バージョン 1.15 以降の Go をインストールします。
package-go-module gcloud CLI アドオンをインストールします。
gcloud components install package-go-module
Artifact Registry で認証するように Go を構成します。
必要なロール
モジュールの管理に必要な権限を取得するには、リポジトリに対する次の IAM ロールの付与を管理者に依頼してください。
-
モジュールを表示する: Artifact Registry 閲覧者 (
roles/artifactregistry.reader) -
モジュールをダウンロードまたはインストールする:
Artifact Registry 閲覧者 (
roles/artifactregistry.reader) -
リポジトリにモジュールを追加する:
Artifact Registry 書き込み (
roles/artifactregistry.writer) -
モジュールを削除する:
Artifact Registry リポジトリ管理者 (
roles/artifactregistry.repoAdmin)
ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。
必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
モジュールをアップロードする
リポジトリ モード: 標準
モジュールをパッケージ化してリポジトリにアップロードするには、次のコマンドを実行します。
gcloud artifacts go upload --project=PROJECT \
--repository=REPOSITORY \
--location=LOCATION \
--module-path=MODULE_PATH \
--version=VERSION \
--source=SOURCE_LOCATION
以下を置き換えます。
- PROJECT は、 Google Cloudプロジェクト ID に置き換えます。
- REPOSITORY は、パッケージが保存されるリポジトリの名前に置き換えます。
- LOCATION は、リポジトリのリージョンまたはマルチリージョンのロケーションに置き換えます。
- MODULE_PATH は、モジュールパスに置き換えます。例:
example.com/foo。詳細については、Go モジュール リファレンスをご覧ください。 - VERSION は、
vX.Y.Z形式のモジュールのセマンティック バージョンに置き換えます。ここで、Xはメジャー バージョン、Yはマイナー バージョン、Zはパッチ バージョンです。 - SOURCE_LOCATION は、Go モジュールのルート ディレクトリのパスに置き換えます。
--sourceフラグを省略した場合、デフォルトは現在のディレクトリです。
モジュールが Artifact Registry にアップロードされます。
Go モジュールの作成方法について詳しくは、こちらのチュートリアルをご覧ください。
新しいモジュール バージョンをアップロードする
リポジトリ モード: 標準
デフォルト値が構成されているときに、モジュールの新しいバージョンをデフォルトのプロジェクト、リポジトリ、ロケーションにアップロードするには、新しいバージョン番号を指定して次のコマンドを実行します。
gcloud artifacts go upload \
--module-path=MODULE_PATH \
--version=VERSION \
--source=SOURCE_LOCATION
VERSION は、更新されたモジュールのバージョンに置き換えます。たとえば、パスが example.com/foo のモジュールのバージョン 0.1.1 をアップロードするには、次のコマンドを実行します。
gcloud artifacts go upload \
--module-path=example.com/foo \
--version=v0.1.1 \
--source=SOURCE_LOCATION
モジュールをプレリリース バージョンとしてマークするには、VERSION の後にダッシュを追加し、モジュールのプレリリース識別子を追加します。
gcloud artifacts go upload \
--module-path=MODULE_PATH \
--version=VERSION-PRE_RELEASE_IDENTIFIERS \
--source=SOURCE_LOCATION
PRE_RELEASE_IDENTIFIERS は、ドットで区切られた ASCII 英数字とハイフンに置き換えます。たとえば、パス example.com/foo モジュールで alpha.x.12m.5 によって識別されるモジュールのプレリリースをアップロードするには、次のコマンドを実行します。
gcloud artifacts go upload \
--module-path=example.com/foo \
--version=v1.0.0-alpha.x.12m.5 \
--source=SOURCE_LOCATION
新しいメジャー バージョンをアップロードする
リポジトリ モード: 標準
メジャー バージョンには、以前のバージョンとの下位互換性はありません。互換性を破る変更をインポートしないようにするには、v1 以降のメジャー バージョンに、以前のバージョンとは異なるモジュール パスを設定する必要があります。v2 から、メジャー バージョンはモジュールパスの末尾に追加されます。
たとえば、example.com/foo の v2.0.0 のモジュール パスは example.com/foo/v2 になります。
v1 より後のメジャー バージョンは、メジャー バージョンのサフィックスが付いた名前の別のディレクトリに開発することをおすすめします。
デフォルト値が構成されているときに、パスが example.com/foo であるモジュールの新しいメジャー バージョン 2.0.0 をデフォルトのプロジェクト、リポジトリ、ロケーションにアップロードするには、次のようにします。
gcloud artifacts go upload --module-path=example.com/foo/v2 --version=v2.0.0
モジュールを一覧表示する
リポジトリ モード: 標準、リモート、仮想
次のコマンドを実行して、デフォルト値が構成されているときに、デフォルトのプロジェクト、リポジトリ、ロケーションにあるアップロードされた Go モジュールを検査します。
gcloud artifacts packages list
出力は次のようになります。
Listing items under project my-project, location us-west1, repository my-repo. PACKAGE CREATE_TIME UPDATE_TIME example.com/foo 2022-06-03T20:43:39 2022-06-20T20:37:40
モジュールのバージョンの詳細を表示する
リポジトリ モード: 標準、リモート、仮想
デフォルト値が構成されているときに、次のコマンドを実行して、デフォルトのプロジェクト、リポジトリ、ロケーションにあるモジュールのバージョンを表示します。
gcloud artifacts versions list --package=MODULE_PATH
出力は次のようになります。
Listing items under project my-project, location us-west1, repository my-repo, package example.com/foo. VERSION DESCRIPTION CREATE_TIME UPDATE_TIME v0.1.0 2022-06-03T20:43:39 2022-06-03T20:43:39 v0.1.1 2022-06-20T20:37:40 2022-06-20T20:37:40
モジュールを依存関係として使用する
リポジトリ モード: 標準、リモート、仮想
Artifact Registry に保存されているモジュールをインポートするには、Artifact Registry から依存関係を探し、チェックサム データベースをバイパスするように Go に指示する必要があります。Go の認証を設定するの手順に沿って、認証と Go 環境を設定します。
Artifact Registry に対する認証に有効期間の短い認証情報を使用している場合は、次のコマンドを実行して OAuth トークンを更新する必要があります。
GOPROXY=proxy.golang.org \ go run github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@latest refresh
モジュールにまだ
go.modファイルがない場合は、ディレクトリをモジュールのフォルダに変更し、go mod initを実行してパッケージのgo.modファイルを作成します。go mod init MODULE_PATH
MODULE_PATH は、Artifact Registry に保存されているモジュールのモジュールパスに置き換えます。詳細については、Go モジュール リファレンスをご覧ください。
Artifact Registry に保存されているモジュールのバージョンを必須にするには、
go.modファイルを編集して次のように設定します。module example.com/bar go 1.19 require example.com/foo v0.1.0以下を置き換えます。
example.com/fooは、必要なモジュールのモジュールパスですv0.1.0は Artifact Registry に保存されているバージョンです
main.goファイルのimportセクションに、モジュールパスを通常どおりに含めます。たとえば、Artifact Registry に保存されている、パスが
example.com/fooのモジュールをインポートする場合、インポート セクションは次のようになります。package main import ( foo "example.com/foo" ) func main() { ... }go mod tidyを実行して依存関係をダウンロードします。go mod tidy
モジュールを通常どおり実行します。
go run .
Artifact Registry に格納されているモジュールが依存関係としてダウンロードされ、使用されます。
パッケージ化された Go モジュールを削除する
リポジトリ モード: 標準、リモート
パッケージとそのすべてのバージョンを削除することや、特定のバージョンを削除することができます。
- パッケージを削除すると、その操作は元に戻せません。
Before you delete a package or package version, verify that any you have communicated or addressed any important dependencies on it.
To delete a package:
Console
Open the Repositories page in the Google Cloud console.
In the repository list, click the appropriate repository.
The Packages page lists the packages in the repository.
Select the package that you want to delete.
Click DELETE.
In the confirmation dialog box, click DELETE.
gcloud
Run the following command:
gcloud artifacts packages delete PACKAGE \
[--repository=REPOSITORY] [--location=LOCATION] [--async]
Replace the following:
PACKAGEis the name of the package in the repository.REPOSITORYis the name of the repository. If you configured a default repository, then you can omit this flag to use the default.-
LOCATIONis the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.
The --async flag causes the command to return immediately,
without waiting for the operation in progress to complete.
To delete versions of a package:
Console
Open the Repositories page in the Google Cloud console.
In the repository list, click the appropriate repository.
The Packages page lists the packages in the repository.
Click a package to view versions of that package.
Select versions that you want to delete.
Click DELETE.
In the confirmation dialog box, click DELETE.
gcloud
Run the following command:
gcloud artifacts versions delete VERSION \
--package=PACKAGE \
[--repository=REPOSITORY] [--location=LOCATION] \
[--async]
Replace the following:
VERSIONis the name of the version to delete.PACKAGEis the name of the package in the repository.REPOSITORYis the name of the repository. If you configured a default repository, then you can omit this flag to use the default.-
LOCATIONis the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.
The --async flag causes the command to return immediately,
without waiting for the operation in progress to complete.
次のステップ
- Go Quickstartを試す。
- Go コマンドを使わずに、モジュールまたはバージョンに関連付けられた個々のファイルをダウンロードする。