【Google Cloud】手を動かして学ぶArtifact Registry
この記事で伝えたいこと(ポイント)
- Artifact Registryについて説明しているよ
- Artifact Registryについてハンズオンしているよ
はじめに
この記事ではGoogle Cloudが提供するArtifact Registryを学習していく内容となっています。
主な内容としては実践したときのメモを中心に書きます。(忘れやすいことなど)
誤りなどがあれば修正していく想定です。
Artifact Registryとは
ではさっそく、Artifact Registryについて見ていきましょう。
公式ドキュメントでは以下のように説明されています。
Artifact Registry は、パッケージと Docker コンテナ イメージを 1 か所で保管し管理できる場所として機能します。次のことが可能です。
Artifact Registry の概要 | Artifact Registry documentation | Google Cloud
Artifact Registryはパッケージとコンテナイメージを管理できる場所として機能します。
2025年現在においてはGoogle Cloudに推奨されるレジストリであり、Container Registryの機能を拡張したものとなっています。
なお、Container Registryは非推奨となっており、提供終了となります。
Container Registry は非推奨で、提供終了となります。2024 年 5 月 15 日以降、Artifact Registry は、Container Registry の使用歴がないGoogle Cloud プロジェクトで gcr.io ドメインのイメージをホストします。2025 年 3 月 18 日以降、Container Registry は提供終了となります。
では、実際にどんなものなのか見ていきましょう。
ハンズオン(コンテナイメージを管理する)
Docker コンテナ イメージを Artifact Registry に保存するを参考に進めていきます。
まずは、Artifact Registryにコンテナイメージを保存してみましょう。
リージョンを指定して、Artifact Registryを認証します。
export LOCATION=asia-northeast1
gcloud auth configure-docker $LOCATION-docker.pkg.dev
次に、コンテナイメージをpullして、タグを付けて、Artifact Registryにpushします。
docker pull us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
プロジェクトIDを取得して、タグを付けます。
export PROJECT_ID=`gcloud config list --format 'value(core.project)'` && echo $PROJECT_ID
docker tag us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 \
$LOCATION-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1
プッシュ先のリポジトリをArtifact Registryに作成します。
gcloud artifacts repositories create quickstart-docker-repo \
--repository-format=docker \
--location=$LOCATION \
--description="DESCRIPTION"
これで、Artifact Registryにpushできるようになりました。
試しにpushしてみましょう。
docker push $LOCATION-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1
pushが完了したら、以下のコマンドでpullが実行できるはずです。
pullを実行します。
docker pull $LOCATION-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1
これで、Artifact Registryにコンテナイメージを保存、取得体験できました。
ハンズオン(ソフトウェアパッケージを管理する)
Go モジュールを Artifact Registry に保存するを参考に進めていきます。
まずはgoのバージョンを確認します。
go version
実行結果
go version go1.22.5 darwin/arm64
リージョンとリポジトリ名を指定します。
export LOCATION=asia-northeast1
export REPOSITORY=quickstart-go-repo
export PROJECT_ID=`gcloud config list --format 'value(core.project)'` && echo $PROJECT_ID
gcloud artifacts repositories create $REPOSITORY --repository-format=go --location=$LOCATION --description="sample"
以下のエラーが出る場合はリポジトリ名に問題があります。
Names may only contain lowercase letters, numbers, and hyphens, and must begin with a letter and end with a letter or number.
リポジトリが作成できているかを確認します。
gcloud artifacts repositories describe --location=$LOCATION $REPOSITORY
次に、GoモジュールをArtifact Registryに保存します。
mkdir foo
cd foo
go mod init example.com/foo
package foo
const HelloWorld = "Hello World!"
go mod edit -replace=example.com/foo=../foo
※ --source=SOURCE_LOCATION
を指定するとルートディレクトリを変更できます。
gcloud artifacts go upload --project=$PROJECT_ID \
--repository=$REPOSITORY \
--location=$LOCATION \
--module-path=example.com/foo \
--version="v1.0.0"
以下のエラーが出る場合は、
package-go-module
がインストールされていない可能性があります。
ERROR: (gcloud.artifacts.go.upload) Executable [package-go-module] not found.
gcloud components install package-go-moduleを実行してインストールします。
gcloud components install package-go-module
これで、GoモジュールをArtifact Registryに保存できました。
最後に保存したモジュールを確認してみましょう。
gcloud artifacts packages list --location=$LOCATION --repository=$REPOSITORY
まとめ
Artifact Registryはパッケージとコンテナイメージを管理できる場所として機能します。
Discussion