建構工具是包含所有必要元件的版本化映像檔,可用於建立可執行的容器。每個建構工具都會使用特定的 OS 發行版本做為基礎映像檔 (例如 Ubuntu 22),並支援多個程式設計語言版本。
如果您需要:
- 僅適用於特定建構工具版本的作業系統專屬依附元件。
- 程式設計語言的特定版本,僅適用於特定建構工具版本。
本機建構作業
如要執行本機建構,您必須安裝 Pack CLI 和 Docker。
事前準備
- 在工作站上安裝 Docker Community Edition (CE)。
pack
會使用 Docker 做為 OCI 映像檔建構工具。 - 安裝 Pack CLI。
- 安裝 Git 原始碼控管工具,以便從 GitHub 中擷取範例應用程式。
使用 pack
指定建構工具版本
您可以在 pack 指令中附加 --builder
標記,指定要使用的建構工具版本:
pack build SERVICE_IMAGE_NAME --builder=BUILDER_IMAGE_URL
取代:
BUILDER_IMAGE_URL
,並提供建構工具的網址。範例:gcr.io/buildpacks/builder:google-22
SERVICE_IMAGE_NAME
改為您為應用程式映像檔選擇的名稱。
如要進一步瞭解 pack
指令,請參閱 CLI 說明文件
使用 project.toml
指定建構工具版本
使用 pack
建構時,您可以透過建構包專案描述元 (project.toml
) 設定建構工具
- 在應用程式根目錄中,建立名為
project.toml
的檔案,並加入下列設定:[build] builder = "BUILDER_IMAGE_URL"
-
執行 `pack` 指令來建構應用程式:
pack build SERVICE_IMAGE_NAME
取代:
BUILDER_IMAGE_URL
,並提供產生器的網址,例如gcr.io/buildpacks/builder:google-22
SERVICE_IMAGE_NAME
改為您為應用程式映像檔選擇的名稱。
遠端建構作業
如要在 Cloud Build 中使用特定建構工具,請在提交專案時附加 --pack
旗標。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
- 確認 Google Cloud 專案有權存取容器映像檔存放區。
如要設定 Artifact Registry 中的 Docker 存放區存取權:
- 在 Google Cloud 專案的相同位置建立新的 Docker 存放區。
取代:gcloud artifacts repositories create REPO_NAME \ --repository-format=docker \ --location=REGION --description="DESCRIPTION"
REPO_NAME
替換成您為 Docker 存放區選擇的名稱。REGION
,其中位置位於專案位置或最接近專案位置。 Google CloudDESCRIPTION
,並提供您選擇的說明。
舉例來說,如要在
us-west2
中建立docker
存放區,並將說明設為「Docker repository」,請執行下列指令:gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \ --location=us-west2 --description="Docker repository"
- 確認存放區是否已成功建立:
gcloud artifacts repositories list
清單中應該會顯示您為 Docker 存放區選擇的名稱。
- 在 Google Cloud 專案的相同位置建立新的 Docker 存放區。
- 使用
gcloud
將應用程式原始碼提交至 Cloud Build:gcloud builds submit --pack builder=BUILDER_IMAGE_URL,image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/SERVICE_IMAGE_NAME
取代:
- 將
BUILDER_IMAGE_URL
替換為建構工具的網址。 範例:gcr.io/buildpacks/builder:google-22
LOCATION
替換為容器存放區的區域名稱。範例:us-west2-docker.pkg.dev
- 將
PROJECT_ID
替換為專案的 ID。 Google Cloud REPO_NAME
替換為 Docker 存放區的名稱。SERVICE_IMAGE_NAME
改為您建立的容器映像檔名稱。
如要進一步瞭解
submit
指令,請參閱 Cloud Build 說明文件。 - 將
-
確認範例應用程式已成功發布至
REPO_NAME
:gcloud artifacts docker images list LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME
取代:
LOCATION
替換為容器存放區的區域名稱。範例:us-west2-docker.pkg.dev
- 將
PROJECT_ID
替換為專案的 ID。 Google Cloud REPO_NAME
替換為 Docker 存放區的名稱。
- 在應用程式目錄的根層級初始化
project.toml
檔案,然後將下列設定貼到該檔案中:[build] builder = "BUILDER_IMAGE_URL"
-
從來源部署應用程式
gcloud run deploy --source . SERVICE_IMAGE_NAME
BUILDER_IMAGE_URL
,並提供建構工具的網址。範例:gcr.io/buildpacks/builder:google-22
SERVICE_IMAGE_NAME
改為您建立的容器映像檔名稱。
使用特定建構工具建構應用程式
透過 Cloud Run 從來源部署
使用建構包專案描述元檔案 (例如 project.toml
),在透過 Cloud Run 從來源部署時設定建構工具
取代: