1. 概览
作为 Container Registry 的下一代产品,Artifact Registry 让组织可以在一个位置集中管理容器映像及语言包(如 Maven 和 npm)。它与 Google Cloud 的工具和运行时完全集成,并支持基于语言的依赖项管理,可与 npm 和 Maven 等工具搭配使用。因此,您可以轻松地将其与 CI/CD 工具集成,以便设置自动化流水线。
本实验将引导您了解 Artifact Registry 中提供的一些功能。
学习内容
本实验的学习目标是什么?
- 为容器和语言包创建代码库
- 使用 Artifact Registry 管理容器映像
- 将 Maven 配置为使用 Artifact Registry for Java 依赖项
2. 设置和要求
自定进度的环境设置
- 登录 Google Cloud 控制台,然后创建一个新项目或重复使用现有项目。如果您还没有 Gmail 或 Google Workspace 账号,则必须创建一个。
- 项目名称是此项目参与者的显示名称。它是 Google API 尚未使用的字符串。您可以随时对其进行更新。
- 项目 ID 在所有 Google Cloud 项目中是唯一的,并且是不可变的(一经设置便无法更改)。Cloud 控制台会自动生成一个唯一字符串;通常您不在乎这是什么在大多数 Codelab 中,您都需要引用项目 ID(它通常标识为
PROJECT_ID
)。如果您不喜欢生成的 ID,可以再随机生成一个 ID。或者,您也可以尝试自己的项目 ID,看看是否可用。完成此步骤后便无法更改该 ID,并且该 ID 在项目期间会一直保留。 - 此外,还有第三个值,即某些 API 使用的项目编号,供您参考。如需详细了解所有这三个值,请参阅文档。
- 接下来,您需要在 Cloud 控制台中启用结算功能,以便使用 Cloud 资源/API。运行此 Codelab 应该不会产生太多的费用(如果有费用的话)。如需关停资源,以免产生超出本教程范围的结算费用,您可以删除自己创建的资源或删除整个项目。Google Cloud 的新用户符合参与 $300 USD 免费试用计划的条件。
设置 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 依赖项的请求与对 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 - Repositories,并注意您新创建的名为 container-dev-repo
的 Docker 代码库。点击该代码库后,您会看到该代码库目前为空
配置对 Artifact Registry 的 Docker Authentication
连接到 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 控制台 - 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 - Repositories,并注意您新建的名为 container-dev-java-repo
的 Maven 代码库;如果点击该代码库,您会看到该代码库当前为空。
设置向 Artifact Registry 仓库的身份验证
使用以下命令,使用您的用户账号凭据更新应用默认凭据 (ADC) 的已知位置,以便 Artifact Registry 凭据帮助程序可以在与代码库连接时使用这些凭据进行身份验证:
gcloud auth login --update-adc
为 Artifact Registry 配置 Maven
从 java-hello-world
文件夹中运行以下命令,以打开 Cloud Shell Editor 并将应用文件夹添加到他的工作区:
cloudshell workspace .
通过点击“网站现在可以正常运行?”来启用第三方 Cookie。和“允许 Cookie”
浏览器重新加载后,打开 Cloud Shell 并再次运行上述命令,以加载应用文件夹。
在 Cloud Shell Editor 中打开 pom.xml,然后点击“Open Editor”(打开编辑器)
从 Cloud Shell Editor 打开终端并运行以下命令,以输出要添加到 Java 项目的代码库配置:
gcloud artifacts print-settings mvn \
--repository=container-dev-java-repo \
--location=us-central1
并将返回的设置添加到 pom.xml
文件的相应部分。
带有内置终端的 Cloud Editor 视图:
更新 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. 恭喜!
恭喜,您已完成此 Codelab!
所学内容
- 为容器和语言包创建了代码库
- 使用 Artifact Registry 的代管式容器映像
- 将 Maven 配置为使用 Artifact Registry for Java 依赖项
清理
运行以下命令以删除项目
gcloud projects delete $PROJECT_ID