使用 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. 
- 
          如果您使用的是外部身份提供方 (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
      (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). 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/ownerIAM 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 account
- PROJECT_ID: the project ID where you created the service account
 
 
- 
    Ensure that you have the Create Service Accounts IAM role
    (
- 
      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
      (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). 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/ownerIAM 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 account
- PROJECT_ID: the project ID where you created the service account
 
 
- 
    Ensure that you have the Create Service Accounts IAM role
    (
- (可选)如需将日志发送到 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 源代码管理工具。
- 创建一个文件名为 - 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。
- 将示例应用代码库克隆到本地机器: - 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 文件并将其解压缩。 
- 切换到包含工作流示例代码的目录: - 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)"]
- 删除您创建的工作流: - gcloud workflows delete myFirstWorkflow
- 当系统询问您是否要继续时,请输入 - y。
部署示例工作流
定义工作流后,可以进行部署,使其可以执行。部署步骤还会验证源文件是否可以执行。
以下工作流会向公共 API 发送请求,然后返回该 API 的响应。
获取示例代码
您可以从 GitHub 克隆示例代码。
运行示例代码
您可以运行示例代码并执行工作流。执行某个工作流会运行与该工作流关联的已部署工作流定义。
在执行请求中传递数据
根据客户端库语言,您还可以在执行请求中传递运行时实参。例如:
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 项目。
工作流已删除。