使用 gcloud CLI 建立工作流程
本快速入門導覽課程說明如何使用 Google Cloud CLI 建立、部署及執行您的第一項工作流程。範例工作流程會向公用 API 傳送要求,然後傳回 API 的回應。
如要查看所有 Workflows gcloud CLI 指令的清單,請參閱 Workflows gcloud CLI 參考頁面。
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。
- 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.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
Verify that billing is enabled for your Google Cloud project.
-
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 theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable workflows.googleapis.com
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
Verify that billing is enabled for your Google Cloud project.
-
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 theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAMEwith a name for the service account. -
Grant the
roles/logging.logWriterIAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/logging.logWriter
Replace the following:
SERVICE_ACCOUNT_NAME: the name of the service accountPROJECT_ID: the project ID where you created the service account
-
Ensure that you have the Create Service Accounts IAM role
(
-
服務帳戶使用者 (
roles/iam.serviceAccountUser) -
Workflows 管理員 (
roles/workflows.admin) 在主目錄中,建立名為
myFirstWorkflow.yaml或myFirstWorkflow.json的新檔案。複製以下工作流程並貼到新檔案中,然後儲存檔案:
YAML
JSON
除非您自行輸入搜尋字詞,否則工作流程會使用您的Google Cloud 位置建構搜尋字詞,並將搜尋字詞傳送至 Wikipedia API。系統會傳回相關的維基百科文章清單。
部署工作流程,並與指定的服務帳戶建立關聯:
gcloud workflows deploy myFirstWorkflow --source=myFirstWorkflow.EXTENSION \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com更改下列內容:
EXTENSION:工作流程的副檔名;YAML 版本請使用yaml,JSON 版本請使用jsonPROJECT_ID:專案 ID
執行工作流程:
gcloud workflows run myFirstWorkflow \ --data='SEARCH_TERM'將
SEARCH_TERM替換為搜尋字詞,例如{"searchTerm":"North"}。如果您輸入{},系統會使用您的 Google Cloud位置建構搜尋字詞。這會傳回執行嘗試的結果。輸出結果會與下列內容相似:
argument: '{"searchTerm":"North"}' duration: 0.210616856s endTime: '2023-05-10T21:56:39.465899376Z' name: projects/734581694262/locations/us-central1/workflows/workflow-1/executions/eae31f11-a5c3-47e2-8014-05b400820a79 result: '["North","North America","Northern Ireland","North Korea","North Macedonia","North Carolina","Northrop Grumman B-2 Spirit","Northrop F-5","Northern Cyprus","North Dakota"]' startTime: '2023-05-10T21:56:39.255282520Z' state: SUCCEEDED status: currentSteps: - routine: main step: returnOutput workflowRevisionId: 000001-ac2刪除您建立的工作流程:
gcloud workflows delete myFirstWorkflow系統詢問您是否要繼續時,請輸入
y。
如要進一步瞭解服務帳戶角色和權限,請參閱授予工作流程存取Google Cloud 資源的權限。
必要的角色
如要取得完成本快速入門導覽課程所需的權限,請要求管理員在專案中授予您下列 IAM 角色:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
建立、部署及執行工作流程
您已部署並執行第一項工作流程!
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。
工作流程已刪除。