設定環境變數來設定建構作業

您可以設定在建構容器映像檔時設定的環境變數。

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

本機建構作業

如要執行本機建構,您必須安裝 Pack CLI 和 Docker。

事前準備

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

設定環境變數

如要為本機建構作業設定環境變數,請為每個環境變數在 pack 指令中附加 --env 旗標。

您可以使用一或多個執行階段支援的環境變數

pack build SERVICE_NAME \
    --env ENVIRONMENT_VARIABLE

取代:

  • SERVICE_NAME 改為應用程式或函式的服務名稱。
  • ENVIRONMENT_VARIABLE,並在建構期間設定環境變數。
範例

如要在容器映像檔中設定 GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app" 環境變數,請執行下列指令:

pack build my-app \
    --builder gcr.io/buildpacks/builder:v1 \
    --env GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app"
    --env MY-LOCAL-ENV-VARIABLE

遠端建構作業

如要為遠端建構設定環境變數,請使用project.toml專案描述元。Cloud Build 會在建構容器映像檔時使用 project.toml 專案描述元。

事前準備

  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. 選用:下載範例應用程式

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

      Go

      cd buildpack-samples/sample-go

      Java

      cd buildpack-samples/sample-java-gradle

      Node.js

      cd buildpack-samples/sample-node

      PHP

      cd buildpack-samples/sample-php

      Python

      cd buildpack-samples/sample-python

      Ruby

      cd buildpack-samples/sample-ruby

      .NET

      cd buildpack-samples/sample-dotnet

    使用環境變數建構應用程式

    1. 在服務的根目錄中,建立或更新 project.toml 專案描述元,加入 [[build.env]] 區段和任何支援的環境變數
      [[build.env]]
          name = "ENVIRONMENT_VARIABLE_NAME"
          value = "ENVIRONMENT_VARIABLE_VALUE"

      取代:

      • ENVIRONMENT_VARIABLE_NAME,其中包含支援的環境變數名稱。
      • ENVIRONMENT_VARIABLE_VALUE,並為指定的環境變數提供對應值。

      示例

      [[build.env]]
          name = "GOOGLE_ENTRYPOINT"
          value = "gunicorn -p :8080 main:app"
    2. 使用 gcloud 將應用程式原始碼提交至 Cloud Build:
      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

      取代:

      • LOCATION 替換為容器存放區的區域名稱。範例:us-west2-docker.pkg.dev
      • PROJECT_ID 替換為專案的 ID。 Google Cloud
      • REPO_NAME 替換為 Docker 存放區的名稱。
      • IMAGE_NAME 改為您的容器映像檔名稱。

      範例:如果您下載了範例應用程式,請執行對應的指令:

      Go

      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-go

      Java

      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-java-gradle

      Node.js

      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-node

      PHP

      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-php

      Python

      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-python

      Ruby

      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-ruby

      .NET

      gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-dotnet
    3. 確認範例應用程式已成功發布至 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 存放區的名稱。