使用 Google Cloud 控制台建立工作流程

本快速入門導覽課程說明如何使用 Google Cloud 控制台,建立、部署及執行您的第一項工作流程。範例工作流程會向公用 API 傳送要求,然後傳回 API 的回應。


如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導)

「Guide me」(逐步引導)


事前準備

貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。

  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Workflows 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

  6. 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

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  9. Enable the Workflows 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

  10. 設定驗證方法:
      <
    1. 確認您具備「建立服務帳戶」身分與存取權管理角色 (roles/iam.serviceAccountCreator) 和「專案 IAM 管理員」角色 (roles/resourcemanager.projectIamAdmin)。瞭解如何授予角色
    2. 前往 Google Cloud 控制台的「Create service account」(建立服務帳戶) 頁面。

      前往「建立服務帳戶」
    3. 選取專案。
    4. 在「Service account name」(服務帳戶名稱) 欄位中輸入名稱。 Google Cloud 控制台會將這個名稱填入「Service account ID」(服務帳戶 ID) 欄位。

      在「Service account description」(服務帳戶說明) 欄位中輸入說明。例如:Service account for quickstart

    5. 按一下「建立並繼續」
    6. 將「記錄」>「記錄檔寫入者」角色授予服務帳戶。

      如要授予角色,請找出 選取角色 清單,然後依序選取 記錄 > 記錄寫入者

    7. 按一下 [繼續]
    8. 按一下「Done」(完成),即完成建立服務帳戶。

  11. 如要進一步瞭解服務帳戶角色和權限,請參閱授予工作流程存取 Google Cloud 資源的權限

必要的角色

如要取得完成本快速入門導覽課程所需的權限,請要求管理員在專案中授予您下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

建立及部署工作流程

  1. 前往 Google Cloud 控制台的「Workflows」頁面:

    前往「Workflows」頁面

  2. 在「Workflows」頁面中,按一下 「Create」(建立)

  3. 輸入新工作流程的名稱,例如 myFirstWorkflow

  4. 選擇「us-central1」做為區域。

  5. 在「服務帳戶」部分,選取您先前建立的服務帳戶。

  6. 選取「下一步」

  7. 在工作流程編輯器中,複製並貼上下列工作流程:

    YAML

    main:
      params: [input]
      steps:
        - checkSearchTermInInput:
            switch:
              - condition: '${"searchTerm" in input}'
                assign:
                  - searchTerm: '${input.searchTerm}'
                next: readWikipedia
        - getLocation:
            call: sys.get_env
            args:
              name: GOOGLE_CLOUD_LOCATION
            result: location
        - setFromCallResult:
            assign:
              - searchTerm: '${text.split(location, "-")[0]}'
        - readWikipedia:
            call: http.get
            args:
              url: 'https://en.wikipedia.org/w/api.php'
              query:
                action: opensearch
                search: '${searchTerm}'
            result: wikiResult
        - returnOutput:
            return: '${wikiResult.body[1]}'

    JSON

    {
      "main": {
        "params": [
          "input"
        ],
        "steps": [
          {
            "checkSearchTermInInput": {
              "switch": [
                {
                  "condition": "${\"searchTerm\" in input}",
                  "assign": [
                    {
                      "searchTerm": "${input.searchTerm}"
                    }
                  ],
                  "next": "readWikipedia"
                }
              ]
            }
          },
          {
            "getLocation": {
              "call": "sys.get_env",
              "args": {
                "name": "GOOGLE_CLOUD_LOCATION"
              },
              "result": "location"
            }
          },
          {
            "setFromCallResult": {
              "assign": [
                {
                  "searchTerm": "${text.split(location, \"-\")[0]}"
                }
              ]
            }
          },
          {
            "readWikipedia": {
              "call": "http.get",
              "args": {
                "url": "https://en.wikipedia.org/w/api.php",
                "query": {
                  "action": "opensearch",
                  "search": "${searchTerm}"
                }
              },
              "result": "wikiResult"
            }
          },
          {
            "returnOutput": {
              "return": "${wikiResult.body[1]}"
            }
          }
        ]
      }
    }
    

    除非您自行輸入搜尋字詞,否則工作流程會使用您的Google Cloud 位置建構搜尋字詞,並將搜尋字詞傳送至 Wikipedia API。系統會傳回相關的維基百科文章清單。

  8. 選取 [Deploy] (部署)。

執行工作流程

成功部署工作流程後,即可首次執行。部署工作流程之後,系統會將您帶往「Workflow details」(工作流程詳細資料) 頁面。

  1. 在「工作流程詳細資料」頁面中,按一下 「執行」

  2. 在「Execute workflow」(執行工作流程) 頁面中,按一下「Execute」(執行)

  3. 「Output」(輸出) 窗格會顯示工作流程結果。

您已部署並執行第一項工作流程!

清除所用資源

為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。

  1. 前往 Google Cloud 控制台的「Workflows」頁面:

    前往「Workflows」頁面

  2. 在工作流程清單中,按一下工作流程即可前往「Workflow details」(工作流程詳細資料) 頁面。

  3. 按一下 「Delete」(刪除)

  4. 輸入工作流程名稱,然後按一下「確認」

後續步驟