Artifact Registry로 종속 항목 관리

1. 개요

차세대 Container Registry인 Artifact Registry를 사용하면 조직이 컨테이너 이미지와 언어 패키지 (예: Maven 및 npm)를 한곳에서 관리할 수 있습니다. Google Cloud의 도구 및 런타임과 완전히 통합되며 npm 및 Maven과 같은 도구와 함께 사용할 수 있는 언어 기반 종속 항목 관리가 지원됩니다. 따라서 CI/CD 도구와 간단하게 통합하여 자동화된 파이프라인을 구축할 수 있습니다.

이 실습에서는 Artifact Registry에서 사용할 수 있는 몇 가지 기능을 살펴봅니다.

학습할 내용

이 실습의 학습 목표는 무엇인가요?

  • 컨테이너 및 언어 패키지를 위한 저장소 만들기
  • Artifact Registry로 컨테이너 이미지 관리하기
  • Java 종속 항목용 Artifact Registry를 사용하도록 Maven 구성

2. 설정 및 요구사항

자습형 환경 설정

  1. Google Cloud Console에 로그인하여 새 프로젝트를 만들거나 기존 프로젝트를 재사용합니다. 아직 Gmail이나 Google Workspace 계정이 없는 경우 계정을 만들어야 합니다.

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • 프로젝트 이름은 이 프로젝트 참가자의 표시 이름입니다. 이는 Google API에서 사용하지 않는 문자열이며 언제든지 업데이트할 수 있습니다.
  • 프로젝트 ID는 모든 Google Cloud 프로젝트에서 고유하며, 변경할 수 없습니다(설정된 후에는 변경할 수 없음). Cloud 콘솔이 고유한 문자열을 자동으로 생성합니다. 보통은 그게 뭔지 상관하지 않습니다. 대부분의 Codelab에서는 프로젝트 ID (일반적으로 PROJECT_ID로 식별됨)를 참조해야 합니다. 생성된 ID가 마음에 들지 않으면 무작위로 다른 ID를 생성할 수 있습니다. 또는 직접 시도해 보고 사용 가능한지 확인할 수도 있습니다. 이 단계 이후에는 변경할 수 없으며 프로젝트 기간 동안 유지됩니다.
  • 참고로 세 번째 값은 일부 API에서 사용하는 프로젝트 번호입니다. 이 세 가지 값에 대한 자세한 내용은 문서를 참고하세요.
  1. 다음으로 Cloud 리소스/API를 사용하려면 Cloud 콘솔에서 결제를 사용 설정해야 합니다. 이 Codelab 실행에는 많은 비용이 들지 않습니다. 이 튜토리얼이 끝난 후에 요금이 청구되지 않도록 리소스를 종료하려면 만든 리소스를 삭제하거나 전체 프로젝트를 삭제하면 됩니다. Google Cloud 새 사용자에게는 미화 $300 상당의 무료 체험판 프로그램에 참여할 수 있는 자격이 부여됩니다.

gcloud 설정

Cloud Shell에서 프로젝트 ID와 프로젝트 번호를 설정하고 이러한 변수를 PROJECT_IDPROJECT_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 org에 있습니다. 아래 명령어로 클론한 후 디렉터리로 변경합니다.

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 - 저장소로 이동하여 container-dev-repo라는 새로 생성된 Docker 저장소를 확인합니다. 저장소를 클릭하면 지금은 비어 있는 것을 확인할 수 있습니다.

Artifact Registry에 Docker 인증 구성

Artifact Registry 사용자 인증 정보에 연결해야 액세스 권한을 제공할 수 있습니다. 별도의 사용자 인증 정보를 설정하는 대신 gcloud 사용자 인증 정보를 원활하게 사용하도록 Docker를 구성할 수 있습니다.

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

폴더에는 간단한 웹페이지를 렌더링하는 예시 자바 애플리케이션이 포함되어 있습니다. 이 특정 실습과 관련이 없는 다양한 파일 외에도 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 태그가 지정된 이미지를 확인합니다. 취약점 스캔이 실행 중이거나 이미 완료되었으며 감지된 취약점 수를 볼 수 있습니다.

9cb46d3689b3ed2.png

취약점 수를 클릭하면 이미지에서 감지된 취약점 목록과 CVE 게시판 이름 및 심각도가 표시됩니다. 나열된 각 취약점에서 '보기'를 클릭하면 자세한 내용을 확인할 수 있습니다.

c2a961e6218d5a45.png

4. 언어 패키지 사용

이 섹션에서는 Artifact Registry 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 저장소를 확인합니다. 저장소를 클릭하면 지금은 비어 있는 것을 확인할 수 있습니다.

아티팩트 저장소에 대한 인증 설정

다음 명령어를 사용하여 Artifact Registry 사용자 인증 정보 도우미가 저장소에 연결할 때 이를 사용하여 인증할 수 있도록 애플리케이션 기본 사용자 인증 정보 (ADC)의 잘 알려진 위치를 사용자 계정 사용자 인증 정보로 업데이트합니다.

gcloud auth login --update-adc

Artifact Registry용 Maven 구성

java-hello-world 폴더에서 다음 명령어를 실행하여 Cloud Shell 편집기를 열고 애플리케이션 폴더를 작업공간에 추가합니다.

cloudshell workspace .

페이지 오른쪽 상단의 '사이트가 작동 중인가요?'를 클릭하여 서드 파티 쿠키를 사용 설정합니다. '쿠키 허용'을 선택합니다.

브라우저가 새로고침되면 Cloud Shell을 열고 위의 명령어를 한 번 더 실행하여 애플리케이션 폴더를 로드합니다.

62328383ea59b30c.png

a9d756bf08575b0d.png

Cloud Shell 편집기에서 pom.xml을 열고 '편집기 열기'를 클릭합니다.

95d98e831787b2ff.png

Cloud Shell 편집기에서 터미널을 열고 다음 명령어를 실행하여 Java 프로젝트에 추가할 저장소 구성을 출력합니다.

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

그런 다음 반환된 설정을 pom.xml 파일의 적절한 섹션에 추가합니다.

기본 제공 터미널이 있는 Cloud 편집기 뷰:

33c3bfa412b7babd.png

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>

Artifact Registry에 자바 패키지 업로드하기

Artifact Registry를 Maven에서 구성했으므로 이제 Artifact Registry를 사용하여 조직의 다른 프로젝트에서 사용할 Java Jar를 저장할 수 있습니다.

다음 명령어를 실행하여 자바 패키지를 Artifact Registry에 업로드합니다.

mvn deploy

Artifact Registry에서 자바 패키지 확인하기

Cloud Console - Artifact Registry - 저장소로 이동하여 container-dev-java-repo를 클릭하고 hello-world 바이너리 아티팩트가 있는지 확인합니다.

e348d976ac1ac107.png

5. 축하합니다.

축하합니다. Codelab을 완료했습니다.

학습한 내용

  • 컨테이너 및 언어 패키지용 저장소 생성
  • Artifact Registry를 사용한 관리형 컨테이너 이미지
  • Java 종속 항목용 Artifact Registry를 사용하도록 Maven을 구성함

삭제

다음 명령어를 실행하여 프로젝트를 삭제합니다.

gcloud projects delete $PROJECT_ID