設定環境

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

啟用 Cloud Scheduler API

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

控制台

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Scheduler API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Scheduler API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

gcloud

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 安裝 Google Cloud CLI。

  3. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  4. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  5. 建立或選取 Google Cloud 專案

    選取或建立專案所需的角色

    • 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
    • 建立專案:如要建立專案,您需要具備專案建立者角色 (roles/resourcemanager.projectCreator),其中包含 resourcemanager.projects.create 權限。瞭解如何授予角色
    • 建立 Google Cloud 專案:

      gcloud projects create PROJECT_ID

      PROJECT_ID 替換為您要建立的 Google Cloud 專案名稱。

    • 選取您建立的 Google Cloud 專案:

      gcloud config set project PROJECT_ID

      PROJECT_ID 替換為 Google Cloud 專案名稱。

  6. 確認專案已啟用計費功能 Google Cloud

  7. 啟用 Cloud Scheduler API:

    啟用 API 時所需的角色

    如要啟用 API,您需要具備服務使用情形管理員 IAM 角色 (roles/serviceusage.serviceUsageAdmin),其中包含 serviceusage.services.enable 權限。瞭解如何授予角色

    gcloud services enable cloudscheduler.googleapis.com
  8. 安裝 Google Cloud CLI。

  9. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  10. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  11. 建立或選取 Google Cloud 專案

    選取或建立專案所需的角色

    • 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
    • 建立專案:如要建立專案,您需要具備專案建立者角色 (roles/resourcemanager.projectCreator),其中包含 resourcemanager.projects.create 權限。瞭解如何授予角色
    • 建立 Google Cloud 專案:

      gcloud projects create PROJECT_ID

      PROJECT_ID 替換為您要建立的 Google Cloud 專案名稱。

    • 選取您建立的 Google Cloud 專案:

      gcloud config set project PROJECT_ID

      PROJECT_ID 替換為 Google Cloud 專案名稱。

  12. 確認專案已啟用計費功能 Google Cloud

  13. 啟用 Cloud Scheduler API:

    啟用 API 時所需的角色

    如要啟用 API,您需要具備服務使用情形管理員 IAM 角色 (roles/serviceusage.serviceUsageAdmin),其中包含 serviceusage.services.enable 權限。瞭解如何授予角色

    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