Pentium10
Pentium10

Reputation: 207912

pass arguments to Cloud Workflows using Python

I am developing a python script allowing me to trigger a cloud workflow service.

In the script, I pass some arguments when I call the
projects/{project}/locations/{location}/workflows/{workflow}/executions endpoint.

But I do not find how to use these args in the YAML.

Upvotes: 3

Views: 2504

Answers (1)

Pentium10
Pentium10

Reputation: 207912

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

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).

Once you deploy and run the function above, the arguments can be accessed in the following way in a workflow (example also available Docs):

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

Upvotes: 9

Related Questions