1. ภาพรวม
จากการพัฒนาของ Container Registry ทำให้ Artifact Registry เป็นจุดเดียวสำหรับองค์กรของคุณในการจัดการอิมเมจคอนเทนเนอร์และแพ็กเกจภาษา (เช่น Maven และ NPM) โดยผสานรวมเข้ากับเครื่องมือและรันไทม์ของ Google Cloud อย่างเต็มรูปแบบ ทั้งยังรองรับการจัดการทรัพยากร Dependency ตามภาษาเพื่อใช้กับเครื่องมือต่างๆ เช่น npm และ Maven ซึ่งช่วยให้คุณผสานรวมเครื่องมือ CI/CD เข้ากับเครื่องมือ CI/CD เพื่อตั้งค่าไปป์ไลน์อัตโนมัติได้อย่างง่ายดาย
ห้องทดลองนี้จะแนะนำฟีเจอร์บางอย่างที่มีอยู่ใน Artifact Registry
สิ่งที่คุณจะได้เรียนรู้
วัตถุประสงค์การเรียนรู้ของห้องทดลองนี้คืออะไร
- สร้างที่เก็บสำหรับคอนเทนเนอร์และแพ็กเกจภาษา
- จัดการอิมเมจคอนเทนเนอร์ด้วย Artifact Registry
- กำหนดค่า Maven เพื่อใช้ Artifact Registry สำหรับการขึ้นต่อกันของ Java
2. การตั้งค่าและข้อกำหนด
การตั้งค่าสภาพแวดล้อมตามเวลาที่สะดวก
- ลงชื่อเข้าใช้ Google Cloud Console และสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
- ชื่อโครงการคือชื่อที่แสดงของผู้เข้าร่วมโปรเจ็กต์นี้ เป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ โดยคุณจะอัปเดตได้ทุกเมื่อ
- รหัสโปรเจ็กต์จะไม่ซ้ำกันในทุกโปรเจ็กต์ของ Google Cloud และจะเปลี่ยนแปลงไม่ได้ (เปลี่ยนแปลงไม่ได้หลังจากตั้งค่าแล้ว) Cloud Console จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ปกติแล้วคุณไม่สนว่าอะไรเป็นอะไร ใน Codelab ส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยปกติจะระบุเป็น
PROJECT_ID
) หากคุณไม่ชอบรหัสที่สร้างขึ้น คุณสามารถสร้างรหัสแบบสุ่มอื่นได้ หรือคุณจะลองดำเนินการเองแล้วดูว่าพร้อมให้บริการหรือไม่ และไม่สามารถเปลี่ยนแปลงได้หลังจากขั้นตอนนี้และจะยังคงอยู่ตลอดระยะเวลาของโปรเจ็กต์ - สำหรับข้อมูลของคุณ ค่าที่ 3 คือหมายเลขโปรเจ็กต์ที่ API บางตัวใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 ค่าได้ในเอกสารประกอบ
- ถัดไป คุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของระบบคลาวด์ การใช้งาน Codelab นี้น่าจะไม่มีค่าใช้จ่ายใดๆ หากมี หากต้องการปิดทรัพยากรเพื่อไม่ให้มีการเรียกเก็บเงินนอกเหนือจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่คุณสร้างหรือลบทั้งโปรเจ็กต์ได้ ผู้ใช้ใหม่ของ Google Cloud จะมีสิทธิ์เข้าร่วมโปรแกรมทดลองใช้ฟรี$300 USD
ตั้งค่า gcloud
ตั้งค่ารหัสและหมายเลขโปรเจ็กต์ใน Cloud Shell บันทึกเป็นตัวแปร 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
ดูซอร์สโค้ด
ซอร์สโค้ดของห้องทดลองนี้อยู่ในองค์กร GoogleCloudPlatform บน GitHub โคลนไฟล์ด้วยคำสั่งด้านล่างแล้วเปลี่ยนเป็นไดเรกทอรี
git clone https://github.com/GoogleCloudPlatform/cloud-code-samples/
3. การทำงานกับอิมเมจคอนเทนเนอร์
สร้างที่เก็บ Docker บนรีจิสทรี Artifact
Artifact Registry รองรับการจัดการอิมเมจคอนเทนเนอร์และแพ็กเกจภาษา อาร์ติแฟกต์แต่ละประเภทมีข้อกำหนดที่แตกต่างกัน ตัวอย่างเช่น คำขอสำหรับทรัพยากร Dependency ของ Maven แตกต่างจากคำขอทรัพยากร Dependency ของโหนด
Artifact Registry จำเป็นต้องทราบว่าคุณต้องการให้การตอบกลับ API ใช้รูปแบบใดเพื่อรองรับข้อกำหนด 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 Console - Artifact Registry - Repositories และสังเกตเห็นที่เก็บ Docker ที่สร้างขึ้นใหม่ชื่อ container-dev-repo
หากคลิก คุณจะเห็นว่าพื้นที่เก็บข้อมูลว่างเปล่า
กำหนดค่าการตรวจสอบสิทธิ์ Docker ไปยัง Artifact Registry
คุณต้องระบุข้อมูลเข้าสู่ระบบ Artifact Registry เพื่อให้สิทธิ์เข้าถึงเมื่อเชื่อมต่อกับข้อมูลเข้าสู่ระบบ Artifact Registry คุณจะกำหนดค่า Docker ให้ใช้ข้อมูลเข้าสู่ระบบ gcloud ได้อย่างราบรื่นแทนการตั้งค่าข้อมูลเข้าสู่ระบบแยกต่างหาก
จาก Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้เพื่อกำหนดค่า Docker ให้ใช้ Google Cloud CLI เพื่อตรวจสอบสิทธิ์คำขอไปยัง Artifact Registry ในภูมิภาค us-central1
gcloud auth configure-docker us-central1-docker.pkg.dev
คำสั่งจะแสดงข้อความให้ยืนยันเพื่อเปลี่ยนการกำหนดค่า Docker ของ Cloud Shell แล้วกด Enter
สำรวจแอปพลิเคชันตัวอย่าง
แอปพลิเคชันตัวอย่างจะมีให้ในที่เก็บ Git ที่คุณโคลนในขั้นตอนก่อนหน้า เปลี่ยนเป็นไดเรกทอรี Java และตรวจสอบโค้ดของแอปพลิเคชัน
cd cloud-code-samples/java/java-hello-world
โฟลเดอร์มีตัวอย่างแอปพลิเคชัน Java ที่แสดงหน้าเว็บแบบง่ายๆ นอกจากไฟล์ต่างๆ ที่ไม่เกี่ยวข้องกับ Lab นี้โดยเฉพาะแล้ว ยังมีซอร์สโค้ดในโฟลเดอร์ 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 - Repositories.
คลิก 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 Console - Artifact Registry - Repositories และสังเกตเห็นที่เก็บ Maven ที่สร้างขึ้นใหม่ชื่อ container-dev-java-repo
หากคลิก คุณจะเห็นว่าตอนนี้ว่างเปล่า
ตั้งค่าการตรวจสอบสิทธิ์กับที่เก็บอาร์ติแฟกต์
ใช้คำสั่งต่อไปนี้เพื่ออัปเดตตำแหน่งที่รู้จักสำหรับข้อมูลรับรองเริ่มต้นของแอปพลิเคชัน (ADC) ด้วยข้อมูลเข้าสู่ระบบของบัญชีผู้ใช้ เพื่อให้ผู้ช่วยข้อมูลเข้าสู่ระบบ Artifact Registry สามารถตรวจสอบสิทธิ์การใช้ตำแหน่งดังกล่าวเมื่อเชื่อมต่อกับที่เก็บ
gcloud auth login --update-adc
กำหนดค่า Maven สำหรับ Artifact Registry
เรียกใช้คำสั่งต่อไปนี้จากโฟลเดอร์ java-hello-world
เพื่อเปิด Cloud Shell Editor และเพิ่มโฟลเดอร์แอปพลิเคชันไปยังพื้นที่ทำงาน
cloudshell workspace .
เปิดใช้คุกกี้ของบุคคลที่สามโดยคลิก "เว็บไซต์ทำงานหรือยัง" แล้วคลิก "อนุญาตคุกกี้"
หลังจากที่เบราว์เซอร์โหลดซ้ำแล้ว ให้เปิด Cloud Shell และเรียกใช้คำสั่งด้านบนอีกครั้งเพื่อโหลดโฟลเดอร์แอปพลิเคชัน
เปิด pom.xml ใน Cloud Shell Editor คลิก "เปิด Editor"
เปิดเทอร์มินัลจาก Cloud Shell Editor และเรียกใช้คำสั่งต่อไปนี้เพื่อพิมพ์การกำหนดค่าที่เก็บเพื่อเพิ่มในโปรเจ็กต์ Java
gcloud artifacts print-settings mvn \
--repository=container-dev-java-repo \
--location=us-central1
แล้วเพิ่มการตั้งค่าที่แสดงผลลงในส่วนที่เหมาะสมในไฟล์ 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>
<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> โดยใช้รหัสโปรเจ็กต์
<?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
ตอนนี้คุณสามารถใช้ Artifact Registry เพื่อเก็บ Java Jars สำหรับโปรเจ็กต์อื่นๆ ในองค์กรด้วยการกำหนดค่า Artifact Registry ใน Maven
เรียกใช้คำสั่งต่อไปนี้เพื่ออัปโหลดแพ็กเกจ Java ไปยัง Artifact Registry
mvn deploy
ตรวจสอบแพ็กเกจ Java ใน Artifact Registry
ไปที่ Cloud Console - Artifact Registry - Repositories คลิก container-dev-java-repo
แล้วตรวจสอบว่ามีอาร์ติแฟกต์ไบนารี hello-world
อยู่ไหม
5. ยินดีด้วย
ยินดีด้วย คุณศึกษา Codelab จบแล้ว
สิ่งที่คุณได้ครอบคลุม
- ที่เก็บที่สร้างขึ้นสำหรับคอนเทนเนอร์และแพ็กเกจภาษา
- อิมเมจคอนเทนเนอร์ที่มีการจัดการด้วย Artifact Registry
- กำหนดค่า Maven เพื่อใช้ Artifact Registry สำหรับการขึ้นต่อกันของ Java
ล้างข้อมูล
เรียกใช้คำสั่งต่อไปนี้เพื่อลบโปรเจ็กต์
gcloud projects delete $PROJECT_ID