使用 Cloud 用戶端程式庫執行工作流程
本快速入門導覽課程說明如何使用 Cloud 用戶端程式庫執行工作流程,以及查看執行結果。
如要進一步瞭解如何安裝 Cloud 用戶端程式庫及設定開發環境,請參閱 Workflows 用戶端程式庫總覽。
您可以在終端機或 Cloud Shell 中使用 Google Cloud 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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with 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_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/owner
IAM 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/owner
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with 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_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/owner
IAM 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/owner
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
- (選用) 如要將記錄傳送至 Cloud Logging,請將
roles/logging.logWriter
角色授予服務帳戶。gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/logging.logWriter"
如要進一步瞭解服務帳戶角色和權限,請參閱「授予工作流程存取Google Cloud 資源的權限」。
- 視需要下載並安裝 Git 原始碼管理工具。
部署工作流程範例
定義工作流程後,您就可以部署工作流程,讓系統執行。部署步驟也會驗證來源檔案是否可執行。
以下工作流程會傳送要求至公用 API,然後傳回 API 的回應。
建立檔案名稱為
myFirstWorkflow.yaml
的文字檔案,並在其中加入下列內容:建立工作流程後,您可以部署工作流程,但請勿執行工作流程:
gcloud workflows deploy myFirstWorkflow \ --source=myFirstWorkflow.yaml \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --location=CLOUD_REGION
將
CLOUD_REGION
替換為工作流程支援的位置。程式碼範例中使用的預設區域為us-central1
。
取得程式碼範例
您可以從 GitHub 複製程式碼範例。
將應用程式存放區範例複製到本機電腦中:
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
變更為包含 Workflow 範例程式碼的目錄:
C#
cd dotnet-docs-samples/workflows/api/Workflow.Samples/
Go
cd golang-samples/workflows/executions/
Java
cd java-docs-samples/workflows/cloud-client/
Node.js
cd nodejs-docs-samples/workflows/quickstart/
Python
cd python-docs-samples/workflows/cloud-client/
請查看程式碼範例。每個範例應用程式都會執行下列作業:
- 設定工作流程的 Cloud 用戶端程式庫。
- 執行工作流程。
- 輪詢工作流程的執行作業 (使用指數輪詢),直到執行作業結束為止。
- 列印執行結果。
C#
Go
Java
Node.js
Python
執行程式碼範例
您可以執行範例程式碼,並執行工作流程。執行工作流程會執行與工作流程相關聯的已部署工作流程定義。
如要執行範例,請先安裝依附元件:
C#
dotnet restore
Go
go mod download
Java
mvn compile
Node.js
npm install -D tsx
Python
pip3 install -r requirements.txt
執行指令碼:
C#
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME dotnet run
Go
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME go run .
Java
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME mvn compile exec:java -Dexec.mainClass=com.example.workflows.WorkflowsQuickstart
Node.js
npx tsx index.js
Python
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME python3 main.py
更改下列內容:
PROJECT_ID
:您的 Google Cloud 專案名稱CLOUD_REGION
:工作流程的位置 (預設值:us-central1
)WORKFLOW_NAME
:工作流程名稱 (預設:myFirstWorkflow
)
輸出結果會與下列內容相似:
Execution finished with state: SUCCEEDED Execution results: ["Thursday","Thursday Night Football","Thursday (band)","Thursday Island","Thursday (album)","Thursday Next","Thursday at the Square","Thursday's Child (David Bowie song)","Thursday Afternoon","Thursday (film)"]
在執行要求中傳遞資料
視用戶端程式庫語言而定,您也可以在執行要求中傳遞執行階段引數。例如:
C#
Go
Java
// Creates the execution object
CreateExecutionRequest request =
CreateExecutionRequest.newBuilder()
.setParent(parent.toString())
.setExecution(Execution.newBuilder().setArgument("{\"searchTerm\":\"Friday\"}").build())
.build();
Node.js
// Execute workflow
try {
const createExecutionRes = await client.createExecution({
parent: client.workflowPath(projectId, location, workflow),
execution: {
argument: JSON.stringify({"searchTerm": "Friday"})
}
});
const executionName = createExecutionRes[0].name;
Python
如要進一步瞭解如何傳遞執行階段引數,請參閱「在執行要求中傳遞執行階段引數」。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取您在本頁面使用資源的費用,請刪除含有這些資源的 Google Cloud 專案。
刪除您建立的工作流程:
gcloud workflows delete myFirstWorkflow
系統詢問您是否要繼續時,請輸入
y
。
工作流程已刪除。