1. 總覽
Artifact Registry 由 Container Registry 演變而來,可讓機構在同一個位置管理容器映像檔和語言套件 (例如 Maven 和 npm)。這項服務與其他 Google Cloud 工具和執行階段完全整合,並支援以語言為基礎的依附元件管理功能,以便搭配 npm 和 Maven 等工具使用。讓您輕鬆與 CI/CD 工具整合,進而設定自動化管道。
本研究室將逐步說明 Artifact Registry 提供的部分功能。
學習目標
本研究室的學習目標為何?
- 為容器和語言套件建立存放區
- 使用 Artifact Registry 管理容器映像檔
- 設定 Maven 以使用適用於 Java 依附元件的 Artifact Registry
2. 設定和需求
自修環境設定
- 登入 Google Cloud 控制台,建立新專案或重複使用現有專案。如果您還沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶。
- 「專案名稱」是這項專案參與者的顯示名稱。這是 Google API 未使用的字元字串。您隨時可以更新這項資訊。
- 所有 Google Cloud 專案的專案 ID 均不得重複,而且設定後即無法變更。Cloud 控制台會自動產生一個不重複的字串。但通常是在乎它何在在大部分的程式碼研究室中,您必須參照專案 ID (通常為
PROJECT_ID
)。如果您對產生的 ID 不滿意,可以隨機產生一個 ID。此外,您也可以自行嘗試,看看系統是否提供該付款方式。在完成這個步驟後就無法變更,而且在專案期間仍會保持有效。 - 資訊中的第三個值是專案編號,部分 API 會使用這個編號。如要進一步瞭解這三個值,請參閱說明文件。
- 接下來,您需要在 Cloud 控制台中啟用計費功能,才能使用 Cloud 資源/API。執行這個程式碼研究室並不會產生任何費用,如果有的話。如要關閉資源,以免系統產生本教學課程結束後產生的費用,您可以刪除自己建立的資源,或刪除整個專案。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/
3. 使用容器映像檔
在 Artifact Registry 上建立 Docker 存放區
Artifact Registry 支援管理容器映像檔和語言套件。不同的構件類型需要不同的規格。舉例來說,Maven 依附元件的要求與節點依附元件的要求不同。
如要支援不同的 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 - 存放區,然後發現新建立的 Docker 存放區 (名稱為「container-dev-repo
」),發現該存放區目前沒有任何內容
將 Docker 驗證功能設為 Artifact Registry
必須連線至 Artifact Registry 憑證才能提供存取權。Docker 可以設定為順暢使用 gcloud 憑證,而不必設定不同的憑證。
在 Cloud Shell 執行下列指令,將 Docker 設定為透過 Google Cloud CLI 驗證 us-central1
區域中傳送至 Artifact Registry 的要求。
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。
執行下列指令來建構容器映像檔,並正確標記,在下一個步驟中將映像檔推送至存放區:
docker build -t us-central1-docker.pkg.dev/$PROJECT_ID/container-dev-repo/java-hello-world:tag1 .
將容器映像檔推送至 Artifact Registry
執行下列指令,將容器映像檔推送至先前建立的存放區:
docker push us-central1-docker.pkg.dev/$PROJECT_ID/container-dev-repo/java-hello-world:tag1
查看 Artifact Registry 中的映像檔
前往 Google Cloud Console - Artifact Registry - 存放區.
按一下「container-dev-repo
」,然後檢查「java-hello-world
」映像檔是否存在。點選圖片,並記下標有 tag1
的圖片。您可以查看安全漏洞掃描正在執行或已完成,並查看偵測到的安全漏洞數量。
按一下安全漏洞數量,即可查看在映像檔中偵測到的安全漏洞清單、 CVE 公告名稱和嚴重程度,在每個安全漏洞上按一下「查看」即可瞭解詳情:
4. 使用語言套件
本節將說明如何設定 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 - 存放區,然後發現新建立的 Maven 存放區 (名稱為「container-dev-java-repo
」),發現該存放區目前沒有任何內容。
為 Artifact Repository 設定驗證機制
使用下列指令,以您的使用者帳戶憑證更新應用程式預設憑證 (ADC) 的已知位置,讓 Artifact Registry 憑證輔助程式在連結存放區時,使用這些憑證進行驗證:
gcloud auth login --update-adc
設定 Maven for Artifact Registry
在 java-hello-world
資料夾中執行下列指令,開啟 Cloud Shell 編輯器,並將應用程式資料夾新增至工作區:
cloudshell workspace .
按一下「網站現在運作正常嗎?」,即可啟用第三方 Cookie然後選擇 [允許 Cookie]。
瀏覽器重新載入後,開啟 Cloud Shell 並再次執行上述指令,即可載入應用程式資料夾。
在 Cloud Shell 編輯器中開啟 pom.xml,然後按一下「開啟編輯器」
透過 Cloud Shell 編輯器開啟終端機,然後執行下列指令來顯示存放區設定,以便新增至 Java 專案:
gcloud artifacts print-settings mvn \
--repository=container-dev-java-repo \
--location=us-central1
並將傳回的設定新增至 pom.xml
檔案中的適當區段。
內建終端機的 Cloud 編輯器檢視畫面:
更新 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 Jars,供貴機構的其他專案使用。
執行下列指令,將 Java 套件上傳至 Artifact Registry:
mvn deploy
在 Artifact Registry 中查看 Java 套件
前往 Cloud 控制台 - Artifact Registry - 存放區 按一下 container-dev-java-repo
,然後檢查 hello-world
二進位檔成果是否存在:
5. 恭喜!
恭喜,您已完成程式碼研究室!
涵蓋內容
- 建立容器和語言套件的存放區
- 使用 Artifact Registry 的代管容器映像檔
- 將 Maven 設定為使用適用於 Java 依附元件的 Artifact Registry
清除所用資源
執行下列指令以刪除專案
gcloud projects delete $PROJECT_ID