使用 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.
-
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.
-
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 克隆示例代码。
将示例应用代码库克隆到本地机器:
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 文件并将其解压缩。
切换到包含工作流示例代码的目录:
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 客户端库。
- 执行工作流。
- 轮询工作流的执行(使用指数退避算法),直到执行终止为止。
- 打印执行结果。
Java
Node.js
Python
运行示例代码
您可以运行示例代码并执行工作流。执行某个工作流会运行与该工作流关联的已部署工作流定义。
要运行示例,请先安装依赖项:
Java
mvn compile
Node.js
npm install -D tsx
Python
pip3 install -r requirements.txt
运行脚本:
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)"]
在执行请求中传递数据
根据客户端库语言,您还可以在执行请求中传递运行时参数。例如:
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;
如需详细了解如何传递运行时参数,请参阅在执行请求中传递运行时参数。
清理
为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。
删除您创建的工作流:
gcloud workflows delete myFirstWorkflow
当系统询问您是否要继续时,请输入
y
。
工作流已删除。