简体   繁体   English

使用 Python 将参数传递给 Cloud Workflow

[英]pass arguments to Cloud Workflows using Python

I am developing a python script allowing me to trigger a cloud workflow service.我正在开发一个 python 脚本,允许我触发云工作流服务。

In the script, I pass some arguments when I call the在脚本中,当我调用
projects/{project}/locations/{location}/workflows/{workflow}/executions endpoint. projects/{project}/locations/{location}/workflows/{workflow}/executions端点。

But I do not find how to use these args in the YAML.但我没有找到如何在 YAML 中使用这些参数。

requirements.txt要求.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

When deploying this as a Cloud Function, ensure that the Service Account of the Function has Workflows.Invoker role set in IAM (required to trigger a workflow execution).将其部署为云函数时,请确保函数的服务帐户在 IAM 中设置了Workflows.Invoker角色(触发工作流执行所需)。

Once you deploy and run the function above, the arguments can be accessed in the following way in a workflow (example also available Docs ):部署并运行上面的函数后,可以在工作流中通过以下方式访问参数(示例也可用Docs ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM