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

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


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

逐步引導


事前準備

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

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  6. 啟用 Workflows API。

    啟用 Workflows API

  7. 前往 Google Cloud 控制台的「Service Accounts」頁面。

    前往「Service Accounts」(服務帳戶)

  8. 選取專案,然後按一下「建立服務帳戶」
  9. 在「Service account name」欄位中輸入名稱,例如 sa-name
  10. 按一下「建立並繼續」
  11. 如要將記錄檔傳送至 Cloud Logging,請按一下「請選擇角色」欄位,然後依序選取「Logging」>「Logs Writer」

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

  12. 按一下 [完成]

建立及部署工作流程

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

    前往「Workflows」頁面

  2. 在「Workflows」頁面中,按一下 「Create」

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

  4. 在「Region」(區域) 部分,選擇「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. 在「Workflow details」頁面中,按一下 「Execute」

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

  3. 「Output」窗格會顯示工作流程的結果。

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

清除所用資源

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

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

    前往「Workflows」頁面

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

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

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

後續步驟