Artifact Registry の詳細

1. 概要

Container Registry の進化形である Artifact Registry は、組織がコンテナ イメージと言語パッケージ(Maven や NPM など)を 1 か所で管理できる場所になりました。Google Cloud のツールやランタイムと完全に統合されており、npm や Maven などのツールで使用する言語ベースの依存関係管理をサポートしています。これにより、CI/CD ツールと簡単に統合して自動化パイプラインを設定できるようになりました。

このラボでは、Artifact Registry で利用できるいくつかの機能について説明します。

学習内容

このラボの学習目標は何ですか。

  • コンテナと言語パッケージのリポジトリを作成する
  • Artifact Registry を使用してコンテナ イメージを管理する
  • Artifact Registry を Cloud Code と統合する
  • Java の依存関係に Artifact Registry を使用するように Maven を構成する

2. 設定と要件

セルフペース型の環境設定

  1. Google Cloud Console にログインして、プロジェクトを新規作成するか、既存のプロジェクトを再利用します。Gmail アカウントも Google Workspace アカウントもまだお持ちでない場合は、アカウントを作成してください。

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • プロジェクト名は、このプロジェクトの参加者に表示される名称です。Google API では使用されない文字列で、いつでも更新できます。
  • プロジェクト ID は、すべての Google Cloud プロジェクトにおいて一意でなければならず、不変です(設定後は変更できません)。Cloud Console により一意の文字列が自動生成されます(通常は内容を意識する必要はありません)。ほとんどの Codelab では、プロジェクト ID を参照する必要があります(通常、プロジェクト ID は「PROJECT_ID」の形式です)。好みの文字列でない場合は、別のランダムな ID を生成するか、独自の ID を試用して利用可能であるかどうかを確認することができます。プロジェクトの作成後、ID は「フリーズ」されます。
  • 3 つ目の値として、一部の API が使用するプロジェクト番号があります。これら 3 つの値について詳しくは、こちらのドキュメントをご覧ください。
  1. 次に、Cloud のリソースや API を使用するために、Cloud Console で課金を有効にする必要があります。この Codelab の操作をすべて行って、費用が生じたとしても、少額です。このチュートリアルを終了した後に課金が発生しないようにリソースをシャットダウンするには、Codelab の最後にある「クリーンアップ」の手順を行います。Google Cloud の新規ユーザーは、300 米ドル分の無料トライアル プログラムをご利用いただけます。

gcloud を設定する

Cloud Shell で、プロジェクト ID とプロジェクト番号を設定します。これらを PROJECT_ID 変数と PROJECT_NUMBER 変数として保存します。

export PROJECT_ID=$(gcloud config get-value project)
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')

Google サービスを有効にする

gcloud services enable \
  cloudresourcemanager.googleapis.com \
  container.googleapis.com \
  artifactregistry.googleapis.com \
  containerregistry.googleapis.com \
  containerscanning.googleapis.com

ソースコードを取得する

このラボのソースコードは GitHub の GoogleCloudPlatform 組織にあります。以下のコマンドでクローンを作成し、そのディレクトリに移動します。

git clone https://github.com/GoogleCloudPlatform/cloud-code-samples/

このラボで使用するインフラストラクチャをプロビジョニングする

このラボでは、GKE にコードをデプロイします。以下の設定スクリプトは、このインフラストラクチャを準備します。

gcloud container clusters create container-dev-cluster --zone=us-central1-b

3. コンテナ イメージの操作

Artifact Registry に Docker リポジトリを作成する

Artifact Registry は、コンテナ イメージと言語パッケージの管理をサポートしています。アーティファクトのタイプが異なれば、必要な仕様も異なります。たとえば、Maven の依存関係のリクエストは Node の依存関係のリクエストとは異なります。

さまざまな API 仕様をサポートするには、Artifact Registry で API レスポンスの形式を認識する必要があります。これを行うには、リポジトリを作成し、目的のリポジトリのタイプを示す --repository-format フラグを渡します。

Cloud Shell で次のコマンドを実行して、Docker イメージのリポジトリを作成します。

gcloud artifacts repositories create container-dev-repo --repository-format=docker \
--location=us-central1 --description="Docker repository for Container Dev Workshop"

Cloud Shell の承認プロンプトが表示されたら、[承認] をクリックします。

Google Cloud コンソールの Artifact Registry - [リポジトリ] に移動し、container-dev-repo という名前の新しく作成した Docker リポジトリを確認します。これをクリックすると、この時点で空になっています。

Artifact Registry に対して Docker 認証を構成する

Artifact Registry に接続する場合、アクセスするには認証情報が必要です。個別の認証情報を設定するのではなく、gcloud 認証情報をシームレスに使用するように Docker を構成できます。

Cloud Shell で次のコマンドを実行して、Google Cloud CLI を使用して us-central1 リージョンの Artifact Registry へのリクエストを認証するように Docker を構成します。

gcloud auth configure-docker us-central1-docker.pkg.dev

Cloud Shell の Docker 構成の変更を確認するメッセージが表示されたら、Enter キーを押します。

サンプル アプリケーションを確認する

サンプル アプリケーションは、前の手順でクローンを作成した Git リポジトリに用意されています。Java ディレクトリに移動して、アプリケーション コードを確認します。

cd cloud-code-samples/java/java-hello-world

このフォルダには、シンプルなウェブページをレンダリングするサンプル Java アプリケーションが含まれています。この特定のラボに関係のないさまざまなファイルに加えて、src フォルダの下にあるソースコードと、コンテナ イメージをローカルでビルドするために使用する Dockerfile が含まれています。

コンテナ イメージをビルドする

Artifact Registry にコンテナ イメージを保存する前に、イメージを作成する必要があります。

次のコマンドを実行してコンテナ イメージをビルドし、適切なタグ付けを行い、次のステップでリポジトリに push します。

docker build -t us-central1-docker.pkg.dev/$PROJECT_ID/container-dev-repo/java-hello-world:tag1 .

コンテナ イメージを Artifact Registry に push する

次のコマンドを実行して、先ほど作成したリポジトリにコンテナ イメージを push します。

docker push us-central1-docker.pkg.dev/$PROJECT_ID/container-dev-repo/java-hello-world:tag1

Artifact Registry でイメージを確認する

Google Cloud コンソール - Artifact Registry - リポジトリに移動します。. container-dev-repo をクリックして、java-hello-world イメージがあることを確認します。画像をクリックし、tag1 タグの付いた画像をメモします。脆弱性スキャンが実行中またはすでに完了していること、検出された脆弱性の数が表示されます。

9cb46d3689b3ed2.png

脆弱性の数をクリックすると、イメージで検出された脆弱性のリストと CVE 公開情報の名前と重大度が表示されます。表示された各脆弱性の [表示] をクリックすると、詳細が表示されます。

2b17e9d26d9dd7ea.png

4. Cloud Code とのインテグレーション

このセクションでは、Cloud Code で Artifact Registry Docker イメージ リポジトリを使用する方法について説明します。

Cloud Code から GKE クラスタにアプリケーションをデプロイする

java-hello-world フォルダから次のコマンドを実行して、Cloud Shell エディタを開き、アプリケーション フォルダをワークスペースに追加します。

cloudshell workspace .

アプリケーション フォルダ内にエクスプローラの状態で Cloud Shell エディタが開きます。

Java プロジェクト設定ファイルをワークスペースから除外するよう求めるポップアップが表示されたら、[Exclude in workspace] をクリックします。

次の手順では、Artifact Registry リポジトリのロケーションを入力する必要があります。ロケーションの形式は次のとおりです。

us-central1-docker.pkg.dev/<PROJECT_ID>/container-dev-repo

PROJECT_ID を確認するには、ターミナルで次のコマンドを実行します。

gcloud config get project

左下にある Cloud Code のステータスバーをクリックし、[Run on Kubernetes] を選択します。

e6e2b06467228e18.png

プロンプトが表示されたら、ラボ用にプロビジョニングされた container-dev-cluster GKE クラスタを指す kubeconfig の現在のコンテキストを使用するように Yes を選択します。

イメージ レジストリのプロンプトで、検索したアドレスの場所を入力してから <PROJECT_ID> に置き換えます。(実際の値)

us-central1-docker.pkg.dev/<PROJECT_ID>/container-dev-repo

Run on Kubernetes を初めて実行すると、ターゲット イメージ リポジトリの場所の入力を求められます。指定すると、リポジトリの URL がアプリケーション フォルダに作成された .vscode/launch.json ファイルに保存されます。

出力ペインで、アプリケーション イメージのビルドが開始されたことを確認できます。java-hello-world,イメージは、前に構成した Artifact Registry リポジトリにアップロードされます。

Cloud コンソール - Artifact Registry - リポジトリに移動します。container-dev-repo をクリックして、java-hello-world イメージを確認し、latest というタグが付いた新しいイメージをメモします。

デプロイされたアプリケーションを確認する

Cloud Shell エディタに戻ります。デプロイが完了すると、Skaffold または Cloud Code によって、サービスが転送された公開 URL が出力されます。[Open Web Preview] リンクをクリックします。

33257a43826b88ff.png

新しいブラウザ ウィンドウに Hello World アプリのページが表示されます。

d3e49693b0383a5d.png

アプリケーション コードを更新する

アプリケーションを更新して、変更がクラスタの Deployment にすぐに実装されることを確認します。

Cloud Shell エディタで src/main/java/cloudcode/helloworld/web フォルダの HelloWorldController.java を開きます。

20 行目のテキストを「It's running!」から「It's running!」に変更します。「It's updated!」に変更されると、ビルドとデプロイのプロセスが直ちに開始されます。

デプロイの最後に、転送された URL をもう一度クリックするか、アプリケーションでブラウザ ウィンドウを更新して、変更がデプロイされたことを確認します。

41787b1da54ff137.png

もう一度 Cloud コンソール - Artifact Registry - リポジトリに移動します。container-dev-repo をクリックして、java-hello-world イメージを確認し、新しいイメージをメモします。

5. 言語パッケージの使用

このセクションでは、Artifact Registry Java リポジトリを設定し、そこにパッケージをアップロードして、さまざまなアプリケーションで利用する方法を説明します。

Java パッケージ リポジトリを作成する

Cloud Shell で次のコマンドを実行して、Java アーティファクトのリポジトリを作成します。

gcloud artifacts repositories create container-dev-java-repo \
    --repository-format=maven \
    --location=us-central1 \
    --description="Java package repository for Container Dev Workshop"

Cloud Shell の承認プロンプトが表示されたら、[承認] をクリックします。

Google Cloud コンソールの Artifact Registry - [リポジトリ] に移動し、container-dev-java-repo という名前の新しく作成した Maven リポジトリを確認します。これをクリックすると、この時点で空であることがわかります。

アーティファクト リポジトリの認証を設定する

次のコマンドを使用して、アプリケーションのデフォルト認証情報(ADC)のよく知られた場所をユーザー アカウントの認証情報で更新します。これにより、Artifact Registry 認証情報ヘルパーがリポジトリに接続するときに、ADC を使用して認証できるようになります。

gcloud auth login --update-adc

Artifact Registry 用に Maven を構成する

次のコマンドを実行して、Java プロジェクトに追加するリポジトリ構成を出力します。

gcloud artifacts print-settings mvn \
    --repository=container-dev-java-repo \
    --location=us-central1

Cloud Shell エディタで pom.xml を開き、返された設定をファイル内の適切なセクションに追加します。

distributionManagement セクションを更新する

<distributionManagement>
   <snapshotRepository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </snapshotRepository>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </repository>
</distributionManagement>

repositories セクションを更新します

 <repositories>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
     <releases>
       <enabled>true</enabled>
     </releases>
     <snapshots>
       <enabled>true</enabled>
     </snapshots>
   </repository>
 </repositories>

拡張機能を更新する

<extensions>
     <extension>
       <groupId>com.google.cloud.artifactregistry</groupId>
       <artifactId>artifactregistry-maven-wagon</artifactId>
       <version>2.1.0</version>
     </extension>
   </extensions>

ファイル全体の例を次に示します。<PROJECT> はをプロジェクト ID に置き換えます。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <artifactId>hello-world</artifactId>
 <packaging>jar</packaging>
 <name>Cloud Code Hello World</name>
 <description>Getting started with Cloud Code</description>
 <version>1.0.0</version>
<distributionManagement>
   <snapshotRepository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </snapshotRepository>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </repository>
 </distributionManagement>
 
 <repositories>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
     <releases>
       <enabled>true</enabled>
     </releases>
     <snapshots>
       <enabled>true</enabled>
     </snapshots>
   </repository>
 </repositories>
 
 <parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.6.3</version>
 </parent>
 
 <properties>
   <java.version>1.8</java.version>
   <checkstyle.config.location>./checkstyle.xml</checkstyle.config.location>
 </properties>
 
 <build>
   <plugins>
     <plugin>
       <groupId>com.google.cloud.tools</groupId>
       <artifactId>jib-maven-plugin</artifactId>
       <version>3.2.0</version>
     </plugin>
     <plugin>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-checkstyle-plugin</artifactId>
       <version>3.1.2</version>
     </plugin>
   </plugins>
   <extensions>
     <extension>
       <groupId>com.google.cloud.artifactregistry</groupId>
       <artifactId>artifactregistry-maven-wagon</artifactId>
       <version>2.1.0</version>
     </extension>
   </extensions>
 </build>
 
 <!-- The Spring Cloud GCP BOM will manage spring-cloud-gcp version numbers for you. -->
 <dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-gcp-dependencies</artifactId>
       <version>1.2.8.RELEASE</version>
       <type>pom</type>
       <scope>import</scope>
     </dependency>
   </dependencies>
 </dependencyManagement>
  
 <dependencies>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-gcp-starter-logging</artifactId>
   </dependency>
      
 </dependencies>
 
</project>

Java パッケージを Artifact Registry にアップロードする

Maven で Artifact Registry を構成したら、組織内の他のプロジェクトで使用できるように Artifact Registry を使用して Java JAR を保存できるようになりました。

次のコマンドを実行して、Java パッケージを Artifact Registry にアップロードします。

mvn deploy

Artifact Registry で Java パッケージを確認する

Cloud コンソール - Artifact Registry - リポジトリに移動します。container-dev-java-repo をクリックして、hello-world バイナリ アーティファクトがあることを確認します。

e348d976ac1ac107.png

6. 完了

お疲れさまでした。これでこの Codelab は終了です。

学習した内容

  • コンテナと言語パッケージ用のリポジトリを作成
  • Artifact Registry を使用したマネージド コンテナ イメージ
  • Artifact Registry と Cloud Code の統合
  • Java の依存関係に Artifact Registry を使用するように Maven を構成する

クリーンアップ

次のコマンドを実行してプロジェクトを削除します。

gcloud projects delete $PROJECT_ID