設定環境

排定及執行 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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    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,您必須具備 serviceusage.services.enable 權限。如果您建立了專案,可能已透過「擁有者」角色 (roles/owner) 取得這項權限。否則,您可以透過「服務使用情形管理員」角色 (roles/serviceusage.serviceUsageAdmin) 取得這項權限。瞭解如何授予角色。

    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,您必須具備 serviceusage.services.enable 權限。如果您建立了專案,可能已透過「擁有者」角色 (roles/owner) 取得這項權限。否則,您可以透過「服務使用情形管理員」角色 (roles/serviceusage.serviceUsageAdmin) 取得這項權限。瞭解如何授予角色。

    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. 在「歡迎使用 App Engine」對話方塊中,執行下列任一操作:

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

      或

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

  3. 選取應用程式的區域。

    請注意,在 Cloud Scheduler 指令中,europe-west 和 us-central 分別稱為 europe-west1 和 us-central1。

    為 App Engine 應用程式設定區域後,就無法變更。

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

  5. 點選「下一步」。

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

  6. 請勿下載 Cloud SDK,而是點選「稍後再執行」。

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

gcloud

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

    gcloud app create --region=REGION
    

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

    為 App Engine 應用程式設定區域後,就無法變更。

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

    gcloud app describe
    

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