保護軟體供應

1. 總覽

您可以使用 Artifact Registry 儲存不同類型的構件,在單一專案中建立多個存放區,並將特定區域或多區域與每個存放區建立關聯。有幾種存放區模式。每種模式的用途都不同。下圖顯示了許多可同時使用不同模式的存放區方式之一。這張圖表顯示兩個 Google Cloud 專案之間的工作流程。在開發專案中,開發人員會建構 Java 應用程式。在另一個獨立的執行階段專案中,另一個建構作業會建立包含應用程式的容器映像檔,以便部署至 Google Kubernetes Engine。

5af5e4da3ccfdff3.png

本實驗室將說明如何執行下列工作。

  • 使用標準存放區部署私人套件
  • 使用遠端存放區快取 Maven 中央套件
  • 使用虛擬存放區,在單一設定中合併多個上游存放區

自學環境設定

  1. 登入 Google Cloud 控制台,然後建立新專案或重複使用現有專案。如果您還沒有 Gmail 或 Google Workspace 帳戶,請務必建立帳戶

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • 「Project name」是這個專案參與者的顯示名稱。這是 Google API 不會使用的字元字串。您隨時可以更新。
  • 專案 ID 在所有 Google Cloud 專案中都是不重複的值,且無法變更 (設定後即無法變更)。Cloud 控制台會自動產生唯一字串,您通常不需要特別留意。在大多數程式碼研究室中,您需要參照專案 ID (通常會標示為 PROJECT_ID)。如果您不喜歡產生的 ID,可以隨機產生另一組 ID。或者,您也可以自行嘗試,看看是否可用。這項設定在這個步驟後即無法變更,並會在專案期間維持不變。
  • 提醒您,有些 API 會使用第三個值,也就是「專案編號」。如要進一步瞭解這三個值,請參閱說明文件
  1. 接下來,您需要在 Cloud 控制台中啟用帳單功能,才能使用 Cloud 資源/API。執行本程式碼研究室時,費用應該不會太高,如要關閉資源,避免產生教學課程以外的費用,您可以刪除建立的資源,或刪除整個專案。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)')

啟用 API

gcloud services enable artifactregistry.googleapis.com

建立存放區的本機複本

git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/container-registry/container-analysis

2. 標準存放區

標準存放區可讓您儲存私人套件,並在其他應用程式中共用

建立標準 Maven 存放區

在 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,並注意您新建的 Maven 存放區名為 container-dev-java-repo,如果您點選該存放區,會發現目前為空白。

gcloud artifacts repositories describe container-dev-java-repo \
    --location=us-central1

應傳回類似以下的回應

Encryption: Google-managed key
Repository Size: 0.000MB
createTime: '2023-03-21T19:01:45.461589Z'
description: Java package repository for Container Dev Workshop
format: MAVEN
mavenConfig: {}
mode: STANDARD_REPOSITORY
name: projects/qwiklabs-gcp-03-4304110dc461/locations/us-central1/repositories/container-dev-java-repo
updateTime: '2023-03-21T19:01:45.461589Z'

為 Artifact Registry 設定 Maven

執行下列指令,即可列印要新增至 Java 專案的存放區設定:

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

上述指令會傳回 XML,以便新增至專案的 pom.xml。

  • 「repositories」 區段會指定 Maven 可下載遠端構件的位置,供目前專案使用。
  • distributionManagement 部分會指定專案在部署時會推送至哪個遠端存放區。
  • 「extensions」 部分會新增 artifactregistry-maven-wagon,啟用連線至 Artifact Registry 所需的驗證和傳輸層
  • 注意:擴充功能可存在於 pom.xml 或 extensions.xml 中。如果專案依附於父項專案,系統會先存取這些依附元件,再載入 pom.xml 中的其他項目。為確保父項可存取擴充功能,您可以將擴充功能放入 extensions.xml 檔案中,該檔案會在 pom.xml 之前載入,因此可供父項依附元件使用。

複製這三個部分,然後在 Cloud Shell 編輯器中開啟 pom.xml,並在檔案底部的結束 project 標記內,加入傳回的設定。

提示:在 Cloud Shell 中,在終端機中執行下列指令,即可在目前目錄中開啟編輯器。

cloudshell workspace .

範例:(您的專案名稱在網址中會有所不同)

  ...

  <distributionManagement>
    <snapshotRepository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </snapshotRepository>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </repository>
  </distributionManagement>

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

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

</project>

將 Java 套件上傳至 Artifact Registry

在 Maven 中設定 Artifact Registry 後,您現在可以使用 Artifact Registry 儲存 Java jar,供貴機構的其他專案使用。

執行下列指令,將 Java 套件上傳至 Artifact Registry:

mvn deploy -DskipTests

如果您想再次執行這項指令,請務必在 pom.xml 中提高版本。

在 Artifact Registry 中查看 Java 套件

前往 Cloud 控制台 - Artifact Registry - 存放區,按一下 container-dev-java-repo,確認 hello-world 二進位檔案物件是否存在:

147eac5168648db1.png

3. 遠端存放區

遠端存放區可快取第三方套件,提高可靠性和安全性。

建立遠端存放區

注意:如要進一步瞭解驗證和設定,請參閱產品說明文件

在 Cloud Shell 中執行下列指令,為 Maven Central 構件建立遠端存放區:

gcloud artifacts repositories create maven-central-cache \
    --project=$PROJECT_ID \
    --repository-format=maven \
    --location=us-central1 \
    --description="Remote repository for Maven Central caching" \
    --mode=remote-repository \
    --remote-repo-config-desc="Maven Central" \
    --remote-mvn-repo=MAVEN-CENTRAL

在控制台中查看存放區

前往 Cloud 控制台 - Artifact Registry - Repositories 點選 maven-central-cache,您會發現該項目已建立,但目前為空白

在終端機中查看存放區

gcloud artifacts repositories describe maven-central-cache \
    --location=us-central1

將儲存庫整合至專案

執行下列指令,即可列印要新增至 Java 專案的存放區設定:

gcloud artifacts print-settings mvn \
    --repository=maven-central-cache \
    --location=us-central1

將存放區部分新增至 pom.xml。請務必不要從輸出內容複製外部 <repositories> 標記。

將新增的存放區 ID 變更為「central」,確保每個存放區項目都有專屬 ID。

範例:(您的專案名稱會在網址中顯示)

  ...

  <distributionManagement>
    <snapshotRepository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </snapshotRepository>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </repository>
  </distributionManagement>

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

    <repository>
      <id>central</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/maven-central-cache</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>


  </repositories>

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

</project>

在終端機中執行下列指令,為專案建立 extensions.xml。如要使用核心擴充功能機制,請確保 Maven 能從 Artifact Registry 解析父項或外掛程式依附元件。

mkdir .mvn 
cat > .mvn/extensions.xml << EOF
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
  <extension>
    <groupId>com.google.cloud.artifactregistry</groupId>
    <artifactId>artifactregistry-maven-wagon</artifactId>
    <version>2.2.0</version>
  </extension>
</extensions>
EOF

從遠端存放區提取依附元件

執行下列指令,使用遠端存放區編譯應用程式:

rm -rf ~/.m2/repository 
mvn compile

在控制台中查看套件

前往 Cloud 控制台 - Artifact Registry - 存放區 點選 maven-central-cache,確認二進位構件是否已快取:

9deea93caa5fefd7.png

4. 虛擬存放區

虛擬存放區可做為介面,讓您透過單一設定存取多個存放區。這麼做可簡化構件使用者的用戶端設定,並透過減少依附元件混淆攻擊來提升安全性。

建立政策檔案

cat > ./policy.json << EOF
[
  {
    "id": "private",
    "repository": "projects/${PROJECT_ID}/locations/us-central1/repositories/container-dev-java-repo",
    "priority": 100
  },
  {
    "id": "central",
    "repository": "projects/${PROJECT_ID}/locations/us-central1/repositories/maven-central-cache",
    "priority": 80
  }
]

EOF

建立虛擬存放區

gcloud artifacts repositories create virtual-maven-repo \
    --project=${PROJECT_ID} \
    --repository-format=maven \
    --mode=virtual-repository \
    --location=us-central1 \
    --description="Virtual Maven Repo" \
    --upstream-policy-file=./policy.json

將儲存庫整合至專案

執行下列指令,即可列印要新增至 Java 專案的存放區設定:

gcloud artifacts print-settings mvn \
    --repository=virtual-maven-repo \
    --location=us-central1

將 pom 中的整個 repositories 區段,替換為輸出內容中的一個虛擬 repositories 區段。

範例:(您的專案名稱在網址中會有所不同)

  ...


  <distributionManagement>
    <snapshotRepository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </snapshotRepository>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </repository>
  </distributionManagement>

  <repositories>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/virtual-maven-repo</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

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

</project>


從虛擬存放區提取依附元件

由於虛擬存放區是傳遞,不會儲存任何實際套件,因此為了清楚展示這個程序,您將刪除先前建立的 maven-central-cache 存放區,然後重新建立,以空白存放區重新開始

執行下列指令,重新建立快取存放區

gcloud artifacts repositories delete maven-central-cache \
    --project=$PROJECT_ID \
    --location=us-central1 \
    --quiet

gcloud artifacts repositories create maven-central-cache \
    --project=$PROJECT_ID \
    --repository-format=maven \
    --location=us-central1 \
    --description="Remote repository for Maven Central caching" \
    --mode=remote-repository \
    --remote-repo-config-desc="Maven Central" \
    --remote-mvn-repo=MAVEN-CENTRAL

您可以在控制台中查看空白的儲存庫。前往 Cloud 控制台 - Artifact Registry - Repositories

接下來,請使用下列指令建構專案,以便測試虛擬存放區

rm -rf ~/.m2/repository 
mvn compile

在主控台中查看套件

前往 Cloud 控制台 - Artifact Registry - 存放區,點選 maven-central-cache,確認二進位構件已設定為從虛擬存放區提取,但最終從 maven-central-cache 提取:

9deea93caa5fefd7.png

5. 恭喜!

恭喜,您已完成程式碼研究室!

涵蓋內容

  • 使用標準存放區部署私人套件
  • 使用遠端存放區快取 Maven 中央套件
  • 使用虛擬存放區,在單一設定檔中合併多個上游存放區

清除所用資源

執行下列指令即可刪除專案

gcloud projects delete ${PROJECT_ID}

上次更新日期:2023 年 3 月 22 日