將函式建構至容器

Cloud Run 支援直接部署函式,但您也可以使用 Google Cloud 的 Buildpacks 將函式建構為容器映像檔,然後將這個容器映像檔部署至 Cloud Run。

將函式建構至容器的常見用途包括:

  • 持續整合和持續推送軟體更新:開發人員編寫並推送函式程式碼至來源存放區,CI/CD 系統會自動將函式建構至容器,執行測試,然後自動部署至測試環境。
  • 基礎架構做為程式碼:使用 YAML 或 Terraform 管理的 Cloud Run 資源會參照容器映像檔網址。開發人員撰寫的函式程式碼必須建構在容器映像檔中。

本頁面詳細說明如何透過以下兩種方式,複製 Cloud Run 函式的確切建構程序:

  • 使用 pack CLI
  • 使用 Cloud Build 做為遠端建構系統

函式進入點

如要使用建構套件建構函式,請按照下列步驟操作:

  • 加入 Functions Framework 程式庫。

  • GOOGLE_FUNCTION_TARGET 環境變數設為您用來做為進入點的函式名稱。您可以在原始碼所在的資料夾中加入 project.toml,即可達到此效果。project.toml 檔案必須具備下列設定:

[[build.env]]
    name = "GOOGLE_FUNCTION_TARGET"
    value =  "ENTRY_POINT"

ENTRY_POINT 替換為函式方法。

如要進一步瞭解如何搭配使用環境變數和 Cloud Run 函式,請參閱「設定 Cloud Run 函式服務」。

建構工具

Cloud Run 函式會在 Google Cloud 的 Buildpacks 下維護及發布的基礎映像檔上建構。

Builders 是包含 Buildpack 和作業系統套件 (也稱為 Stack) 的映像檔。建構工具可將函式原始碼轉換為執行中的容器。

您可以從支援的 Google Cloud 建構包建構工具清單中選擇。

使用 pack 建構

Pack 是 CNB 專案維護的 CLI 工具,可支援使用 Buildpack。使用 pack CLI 在本機將函式建構為容器映像檔。

事前準備

  1. 在工作站上安裝 Docker Community Edition (CE)pack 會使用 Docker 做為 OCI 映像檔建構工具。
  2. 安裝 Pack CLI
  3. 安裝 Git 原始碼控管工具,以便從 GitHub 中擷取範例應用程式。

在本機建構函式

您可以使用 pack build 指令並指定預設建構工具 --builder=gcr.io/buildpacks/builder,在本機上建構容器映像檔。

pack build --builder=gcr.io/buildpacks/builder IMAGE_NAME

IMAGE_NAME 替換為您的容器映像檔名稱。

您也可以擴充建構和執行映像檔,自訂容器映像檔。

在本機建構範例函式

以下範例說明如何在本機建構範例。

  1. 將範例存放區複製到本機電腦:
    git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
  2. 變更為包含應用程式範例程式碼的目錄:

    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
  3. 使用 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
  4. 使用 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
  5. 前往 localhost:8080 即可查看執行中的函式。

使用遠端建構系統進行建構

使用 Cloud Build 將函式建構為容器映像檔,並使用 Artifact Registry 做為容器存放區,以便儲存及部署每個映像檔。

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build and Artifact Registry APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  9. Make sure that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Build and Artifact Registry APIs.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 請確認 Google Cloud 專案可存取容器映像檔存放區。

    如要設定 Artifact Registry 中的 Docker 存放區存取權,請按照下列步驟操作:

    1. 在 Google Cloud 專案相同位置建立新的 Docker 存放區。
      gcloud artifacts repositories create REPO_NAME \
      --repository-format=docker \
      --location=REGION --description="DESCRIPTION"
      取代:
      • REPO_NAME 改為您為 Docker 存放區選擇的名稱。
      • REGION地點:在 Google Cloud 專案所在地點或附近。
      • DESCRIPTION 和您選擇的說明。

      舉例來說,如要在 us-west2 中建立 docker 存放區,並將說明設為「Docker repository」,請執行以下指令:

      gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \
      --location=us-west2 --description="Docker repository"
    2. 確認存放區是否已成功建立:
      gcloud artifacts repositories list

      清單中應該會顯示您為 Docker 存放區選擇的名稱。

  15. 遠端建構函式

    您可以使用 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 的建構步驟。

    1. 建立名為 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
      images:
      - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

    取代:

    • LOCATION 與容器存放區的區域名稱,例如 us-west2
    • PROJECT_ID 替換為您的 Google Cloud 專案 ID。
    • REPO_NAME 替換為 Docker 存放區的名稱。
    • IMAGE_NAME 改為容器映像檔的名稱。
    1. 建構應用程式。

      如果您將設定檔命名為 cloudbuild.yaml,可以執行下列指令:

      gcloud builds submit .
      

    範例:從遠端建構範例函式

    以下範例說明如何透過遠端建構範例,並確認容器映像檔是否已推送至 Artifact Registry 中的存放區。

    1. 將範例存放區複製到本機電腦:
      git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
    2. 變更為包含應用程式範例程式碼的目錄:

      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
    3. 使用 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 替換為您的 Google Cloud 專案 ID。
      • REPO_NAME 替換為 Docker 存放區的名稱。

    4. 確認範例函式是否已成功發布至 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 存放區的名稱。

    建構自動更新基本映像檔的函式

    函式容器也可以建構在 scratch 上,讓您能搭配使用 Cloud Run 自動安全性更新

    Dockerfile

    您可以使用現有的建構工具鍊,建立可與自動基本映像檔更新相容的函式容器映像檔。請參閱從頭開始建立容器的操作說明。

    pack CLI

    pack build IMAGE_NAME \
      --builder LOCATION-docker.pkg.dev/serverless-runtimes/google-22-full/builder/LANGUAGE:latest
      --run-image LOCATION-docker.pkg.dev/serverless-runtimes/google-22/scratch/RUNTIME_ID:latest
    

    取代:

    • LOCATION 與容器存放區的區域名稱,例如 us-west2
    • IMAGE_NAME 改為容器映像檔的名稱。
    • LANGUAGE 與函式語言,例如 nodejs
    • RUNTIME_ID 與執行階段 ID,例如 nodejs22

    Cloud Build

    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
      - LOCATION-docker.pkg.dev/serverless-runtimes/google-22-full/builder/LANGUAGE:latest
      - --run-image
      - LOCATION-docker.pkg.dev/serverless-runtimes/google-22/scratch/RUNTIME_ID:latest
      - --network
      - cloudbuild
    images:
    - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
    

    取代:

    • LOCATION 與容器存放區的區域名稱,例如 us-west2
    • PROJECT_ID 替換為您的 Google Cloud 專案 ID。
    • REPO_NAME 替換為 Docker 存放區的名稱。
    • IMAGE_NAME 改為容器映像檔的名稱。
    • LANGUAGE 與函式語言,例如 nodejs
    • RUNTIME_ID 與執行階段 ID,例如 nodejs22

    後續步驟

    • 將函式建構至容器後,請先在本機測試,再部署至 Cloud Run。詳情請參閱「在本機測試 Cloud Run 服務」。
    • 如要將您建構的容器部署到 Cloud Run,請參照「部署服務」一文。
    • 如要使用 Cloud Build 觸發條件自動執行 Cloud Run 服務的建構和部署作業,請參閱「設定持續部署」一文。