使用 Python 将参数传递给 Cloud Workflows

pass arguments to Cloud Workflows using Python

我正在开发一个 python 脚本,允许我触发云工作流服务。

在脚本中,我在调用
时传递了一些参数 projects/{project}/locations/{location}/workflows/{workflow}/executions 端点。

但我没有找到如何在 YAML 中使用这些参数。

requirements.txt

google-cloud-workflows>=0.1.0

main.py

from google.cloud.workflows.executions_v1beta.services.executions import ExecutionsClient
from google.cloud.workflows.executions_v1beta.types import CreateExecutionRequest, Execution
import json

def callWorkflow(request):
    project = "projectid"
    location = "us-central1"
    workflow = "workflowname"
    arguments = {"first": "Hello", "second":"world"}

    parent = "projects/{}/locations/{}/workflows/{}".format(project, location, workflow)
    execution = Execution(argument = json.dumps(arguments))

    client = ExecutionsClient()
    response = None
    # Try running a workflow:
    try:
        response = client.create_execution( parent=parent, execution=execution)
    except:
        return "Error occurred when triggering workflow execution", 500

    return "OK", 200

将其部署为 Cloud Function 时,确保 Function 的服务帐户在 IAM 中设置了 Workflows.Invoker 角色(需要触发工作流执行)。

一旦部署并 运行 上面的函数,就可以在工作流中通过以下方式访问参数(示例也可用 Docs):

main:
    params: [args]
    steps:
      - firstStep:
          return: ${args.first + " " + args.second}