使用建構包建立容器映像檔的方法有兩種:
- 使用
pack
CLI 在本機建構,以便在本機測試函式,並在部署前快速製作變更原型。 - 使用 Cloud Build 在遠端建構。如果函式的建構程序需要大量資源,使用 Cloud Build 建構函式就非常實用,而且還能保護軟體供應鏈。
設定專案以建構函式
如要使用建構套件建構函式,請按照下列步驟操作:
加入 Functions Framework 程式庫。
將
GOOGLE_FUNCTION_TARGET
環境變數設為您做為進入點的函式名稱。如要這麼做,請在與原始碼相同的資料夾中加入project.toml
。project.toml
檔案必須包含下列設定:
[[build.env]]
name = "GOOGLE_FUNCTION_TARGET"
value = "ENTRY_POINT"
將 ENTRY_POINT 替換為函式方法。
如要進一步瞭解如何搭配使用環境變數和 Cloud Run functions,請參閱設定 Cloud Run functions 服務。
本機建構作業
Pack 是由 CNB 專案維護的 CLI 工具,可支援使用 Buildpack。使用 pack
CLI 在本機將函式建構為容器映像檔。
事前準備
- 在工作站上安裝 Docker Community Edition (CE)。
pack
會使用 Docker 做為 OCI 映像檔建構工具。 - 安裝 Pack CLI。
- 安裝 Git 原始碼控管工具,以便從 GitHub 中擷取範例應用程式。
在本機建構函式
您可以使用 pack build
指令並指定預設建構工具 --builder=gcr.io/buildpacks/builder
,在本機建構容器映像檔。
pack build --builder=gcr.io/buildpacks/builder IMAGE_NAME
將 IMAGE_NAME 換成容器映像檔名稱。
您也可以擴充建構和執行映像檔,自訂容器映像檔。
在本機建構範例函式
下列範例說明如何在本地建構範例。
- 將範例存放區複製到本機電腦:
git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
- 變更為包含應用程式範例程式碼的目錄:
Go
cd buildpack-samples/sample-functions-framework-go
Java
cd buildpack-samples/sample-functions-framework-java-mvn
Node.js
cd buildpack-samples/sample-functions-framework-node
Python
cd buildpack-samples/sample-functions-framework-python
Ruby
cd buildpack-samples/sample-functions-framework-ruby
- 使用
pack
建構範例函式:Go
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-go
Java
pack build --builder gcr.io/buildpacks/builder:v1 sample-functions-java-mvn
Node.js
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-node
Python
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-python
Ruby
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-ruby
- 使用
docker
執行映像檔:Go
docker run -p8080:8080 sample-functions-framework-go
Java
docker run -it -ePORT=8080 -p8080:8080 sample-functions-java-mvn
Node.js
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-node
Python
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-python
Ruby
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-ruby
- 瀏覽 localhost:8080,即可查看執行中的函式。
遠端建構作業
使用 Cloud Build 將函式建構為容器映像檔,並使用 Artifact Registry 做為容器存放區,儲存及部署每個映像檔。
事前準備
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 確認 Google Cloud 專案有權存取容器映像檔存放區。
如要設定 Artifact Registry 中的 Docker 存放區存取權:
- 在 Google Cloud 專案的相同位置建立新的 Docker 存放區。
取代:gcloud artifacts repositories create REPO_NAME \ --repository-format=docker \ --location=REGION --description="DESCRIPTION"
舉例來說,如要在
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 存放區。
LOCATION
為容器存放區的區域名稱,例如us-west2
- 將
PROJECT_ID
替換為專案的 ID。 Google Cloud REPO_NAME
替換為 Docker 存放區的名稱。IMAGE_NAME
改為您的容器映像檔名稱。- 建立名為
cloudbuild.yaml
的 YAML 檔案,其中包含容器映像檔存放區的 URI。 - 將
LOCATION
換成容器存放區的區域名稱,例如us-west2
。 - 將
PROJECT_ID
替換為專案的 ID。 Google Cloud REPO_NAME
替換為 Docker 存放區的名稱。IMAGE_NAME
改為您的容器映像檔名稱。建構應用程式。
如果您將設定檔命名為
cloudbuild.yaml
,可以執行下列指令:gcloud builds submit .
- 將範例存放區複製到本機電腦:
git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
- 變更為包含應用程式範例程式碼的目錄:
Go
cd buildpack-samples/sample-functions-framework-go
Java
cd buildpack-samples/sample-functions-framework-java-mvn
Node.js
cd buildpack-samples/sample-functions-framework-node
Python
cd buildpack-samples/sample-functions-framework-python
Ruby
cd buildpack-samples/sample-functions-framework-ruby
- 使用
gcloud
將應用程式原始碼提交至 Cloud Build:Go
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-go
Java
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-gradle
Node.js
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-node
Python
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-python
Ruby
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-ruby
取代:
LOCATION
替換為容器存放區的區域名稱。範例:us-west2-docker.pkg.dev
- 將
PROJECT_ID
替換為專案的 ID。 Google Cloud REPO_NAME
替換為 Docker 存放區的名稱。
-
確認範例函式已成功發布至
REPO_NAME
:gcloud artifacts docker images list LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME
取代:
- 將
LOCATION
換成容器存放區的區域名稱,例如us-west2
。 - 將
PROJECT_ID
替換為專案的 ID。 Google Cloud REPO_NAME
替換為 Docker 存放區的名稱。
- 將
- 將函式映像檔部署至 Cloud Run。
- 設定環境變數。
- 設定建構圖片。
- 使用快取映像檔加快建構速度。
遠端建構函式
您可以使用 gcloud builds submit
指令,建構容器映像檔並上傳至存放區。
您可以選擇在指令本身中指定容器映像檔,也可以使用設定檔。
使用指令建構
如要在沒有設定檔的情況下建構,請指定 image
標記:
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
取代:
範例:
gcloud builds submit --pack image=us-west2-docker.pkg.dev/my-project-id/my-buildpacks-docker-repo
使用設定檔建構
您可以使用設定檔定義映像檔存放區設定詳細資料,簡化建構指令。設定檔使用 YAML 檔案格式,且必須包含使用 pack
CLI 的建構步驟。
options: logging: CLOUD_LOGGING_ONLY pool: {} projectId: PROJECT_ID steps: - name: gcr.io/k8s-skaffold/pack entrypoint: pack args: - build - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME - --builder - gcr.io/buildpacks/builder:latest - --network - cloudbuild images: - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
取代:
範例:從遠端建構範例函式
下列範例示範如何遠端建構範例,並確認容器映像檔已推送至 Artifact Registry 中的存放區。