設定環境

排定及執行 Cron 工作前,請務必先為 Cloud Scheduler 設定專案和環境。 Google Cloud

啟用 Cloud Scheduler API

請按照下列操作說明建立或選取 Google Cloud 專案,並啟用 Cloud Scheduler API。

控制台

  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 Scheduler API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  7. Enable the Cloud Scheduler API.

    Enable the API

gcloud

  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. Install the Google Cloud CLI.

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

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

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  7. Enable the Cloud Scheduler API:

    gcloud services enable cloudscheduler.googleapis.com
  8. Install the Google Cloud CLI.

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

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

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  13. Enable the Cloud Scheduler API:

    gcloud services enable cloudscheduler.googleapis.com

設定驗證方法

向 Cloud Scheduler 驗證的方式,取決於您用來存取 API 的介面,以及執行程式碼的環境。

詳情請參閱「向 Cloud Scheduler 進行驗證」和「使用驗證搭配 HTTP 目標」。

新增 App Engine 應用程式

如果您要使用 App Engine HTTP 目標,則必須將 App Engine 應用程式新增至專案。這個應用程式同時做為 Cloud Scheduler 服務和工作處理常式的位置,可執行與 Cloud Scheduler 工作相關聯的工作,並傳回確認訊息做為回應。

如果您想使用在專案中的 App Engine 工作處理常式,請改為選取 HTTP 目標。在這種情況下,專案中不需要有 App Engine 應用程式。

控制台

  1. 前往 Google Cloud 控制台的「App Engine」頁面。

    前往 App Engine

  2. 在「Welcome to App Engine」(歡迎使用 App Engine) 對話方塊中,執行下列任一操作:

    • 如果您已建立 App Engine 應用程式,且系統顯示「已建立 App Engine 應用程式」訊息,即可略過本節的其餘步驟。

    • 如果您尚未建立 App Engine 應用程式,請按一下「建立應用程式」,然後繼續完成本節的其餘步驟。

  3. 選取應用程式的地區

    請注意,在 Cloud Scheduler 指令中,europe-westus-central 分別稱為 europe-west1us-central1

    App Engine 應用程式的區域在設定後即無法變更

  4. 請勿選取服務帳戶,系統會使用預設的 App Engine 服務帳戶。

  5. 點選「下一步」

    應用程式已設定並建立完成。這可能需要幾分鐘的時間。

  6. 請勿下載 Cloud SDK,而是點選「I'll do this later」(稍後再執行)

    您應該會看到「已建立 App Engine 應用程式」訊息。

gcloud

  1. 如要建立 App Engine 應用程式,請執行 gcloud app create 指令:

    gcloud app create --region=REGION
    

    REGION 替換為應用程式執行的位置。請注意,在 Cloud Scheduler 指令中,europe-westus-central 分別稱為 europe-west1us-central1

    App Engine 應用程式的區域在設定後即無法變更

  2. 您可以使用下列指令查看現有應用程式的地區:

    gcloud app describe
    

    locationId 表示區域。例如: locationId: us-central