您可以使用以下兩種方法,透過建構包建構容器映像檔:
- 使用
packCLI 在本機建構函式,在本機測試函式,並在部署前快速製作變更原型。 - 使用 Cloud Build從遠端建構。如果函式的建構程序需要大量資源,使用 Cloud Build 建構就非常實用,而且還能保護軟體供應鏈。
設定專案以建構函式
如要使用 buildpack 建構函式,請按照下列步驟操作:
加入 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
小茹
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
小茹
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
小茹
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-ruby
- 瀏覽 localhost:8080,即可前往正在執行的函式。
遠端建構
使用 Cloud Build 將函式建構為容器映像檔,並使用 Artifact Registry 做為容器存放區,儲存及部署每個映像檔。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,能用於執行、測試及部署工作負載。
-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build and Artifact Registry APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
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.
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build and Artifact Registry APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
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 存放區。
從遠端建構函式
您可以使用 gcloud builds submit 指令,建構容器映像檔並上傳至存放區。
您可以選擇在指令中指定容器映像檔,或是使用設定檔。
使用指令建構
如要在沒有設定檔的情況下建構,請指定 image 標記:
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
更改項目:
- 將
LOCATION替換為容器存放區的區域名稱,例如us-west2 - 將
PROJECT_ID替換為 Google Cloud 專案 ID。 - 將
REPO_NAME換成 Docker 存放區的名稱。 IMAGE_NAME,並將其替換為容器映像檔的名稱。
範例:
gcloud builds submit --pack image=us-west2-docker.pkg.dev/my-project-id/my-buildpacks-docker-repo
使用設定檔建構
您可以使用設定檔定義映像檔存放區設定詳細資料。設定檔採用 YAML 檔案格式,且必須包含使用 pack CLI 的建構步驟。
建立名為
cloudbuild.yaml的 YAML 檔案,其中包含容器映像檔存放區的 URI。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 - --publish - name: gcr.io/cloud-builders/docker entrypoint: pack args: - pull - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME images: - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME更改項目:
LOCATION,例如us-west2。- 將
PROJECT_ID替換為 Google Cloud 專案 ID。 - 將
REPO_NAME換成 Docker 存放區的名稱。 IMAGE_NAME,並將其替換為容器映像檔的名稱。
建構應用程式。
如果設定檔名為
cloudbuild.yaml,請執行下列指令:gcloud builds submit .
範例:從遠端建構範例函式
以下範例說明如何遠端建構範例,並確認容器映像檔已推送至 Artifact Registry 中的存放區。
- 將範例存放區複製到本機電腦:
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
小茹
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
小茹
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替換為 Google Cloud 專案 ID。 - 將
REPO_NAME換成 Docker 存放區的名稱。
-
確認範例函式已成功發布至
REPO_NAME:gcloud artifacts docker images list LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME
更改項目:
LOCATION,例如us-west2。- 將
PROJECT_ID替換為 Google Cloud 專案 ID。 - 將
REPO_NAME換成 Docker 存放區的名稱。
後續步驟
- 將函式映像檔部署至 Cloud Run。
- 設定環境變數。
- 設定建構映像檔。
- 使用快取映像檔加快建構速度。