Luke He
12/07/2023, 8:18 PMMarvin
12/07/2023, 8:19 PMapplication/json
, the body will be parsed as a JSON object and made available to the webhook templates. If it is application/x-www-form-urlencoded
, the body will be parsed into a flat dictionary-like object of key-value pairs.
Regarding triggering a deployment using a webhook in Prefect, there appears to be functionality related to Prefect Cloud Webhook CLI as mentioned in the release notes for Prefect 2.0a4, indicating a streamlined way to deploy flows. However, the provided snippets do not include specific examples of how a webhook triggers a deployment.
For a more detailed guide or examples on how to specifically trigger a deployment using a webhook, we would need to search Prefect's official documentation or GitHub for instances where this process is described in full. If you are looking for code examples or step-by-step instructions, please let me know so I can assist you further.Luke He
12/07/2023, 8:24 PMNate
12/07/2023, 8:29 PMMarvin
12/07/2023, 8:30 PMpython
triggers: [
{
"enabled": True,
"match": "prefect.resource.id: prefect.flow-run.*",
"expect": [
"prefect.flow-run.Completed"
],
"match_related": {
"prefect.resource.name": "prefect.flow.etl-flow",
"prefect.resource.role": "flow"
},
"parameters": {
"param_1": "{{ event }}"
}
}
]
In this example, the trigger is set up to start a flow run upon the completion of an upstream flow specified in the match_related
key. The event data is passed into the flow run as a parameter named param_1
.
In webhooks, you can also accept JSON payloads using a simple pass-through Jinja template. For instance, you could use the following template to convert the entire body of the webhook to JSON, which could then be passed to a Prefect flow as a parameter:
python
{{ body|tojson }}
Using this template, you could configure your webhook to parse the incoming HTTP request body as JSON and then pass that data to the flow run. You may tailor the template to extract specific information from the event if needed.
Here is a more straightforward example to designate a parameter within the webhook:
python
parameters: {
"my_param": "{{ body['key_from_body'] }}"
}
In this case, the value for my_param
will be extracted from the key_from_body
key in the incoming event's body. The name my_param
corresponds to a parameter within the Prefect flow that is being triggered.
To set up these configurations, you would typically define them in the YAML file for your deployment or via the Prefect UI when creating or managing automations and triggers. These triggers and parameters can be tailored to listen to a variety of internal or external events and then act accordingly, passing the event data to your flows seamlessly.Luis Diaz
12/07/2023, 8:38 PMMarvin
12/07/2023, 8:39 PM+
icon.
3. Configure the trigger condition that will cause the automation to execute.
4. After configuring the trigger, specify one or more actions that the automation should carry out.
When setting up an action to kick off a flow run, you will be given the option to define parameters for that run. The parameters can be set to static values, or you can use Jinja templating to dynamically assign values based on the event data. In the Actions configuration, there should be fields where you can specify the names and values of parameters to pass to the flow run.
Here's an outline of what it might look like in the Automations UI:
- Name the automation and provide a description if necessary.
- Set up a trigger based on a specific event, such as a flow run state change or a work pool status change.
- Define the action to run a flow, and within that action's configuration, you will find a section to specify parameters.
- Enter the parameter names and their corresponding values (static values or using Jinja templates to extract from the event).
Please note that precise UI elements may vary slightly based on updates and changes to the Prefect Cloud interface.
If you need the exact UI navigation steps or screenshots for clarity, I would recommend consulting the latest Prefect Cloud documentation or seeking assistance directly within the Prefect Cloud UI, as details may change over time and my access to the current UI and documentation is limited to what's available publicly as of my knowledge cutoff in April 2023.Luis Diaz
12/07/2023, 8:40 PMMarvin
12/07/2023, 8:40 PMjson
{
"event_type": "flow_run_completed",
"flow_run_id": "12345",
"status": "Success",
"triggering_event_data": {
"param1": "value1",
"param2": "value2"
}
}
You want to kick off a new flow run with parameters that are extracted from the triggering event. Here's an example Jinja template for defining parameters based on the event data:
jinja
{
"new_param1": "{{ event.triggering_event_data.param1 }}",
"new_param2": "{{ event.triggering_event_data.param2 }}"
}
When this template is rendered, {{ event.triggering_event_data.param1 }}
is replaced with the actual value from the event's data ("value1"
in this case), resulting in a JSON object that can be passed as parameters:
json
{
"new_param1": "value1",
"new_param2": "value2"
}
Prefect automations can use similar templating to extract values from webhook payloads or other event data to pass as parameters when triggering flow runs. Jinja's flexibility makes it very powerful for constructing dynamic parameter payloads based on the contents of triggers and events within Prefect Cloud.Luke He
12/07/2023, 9:56 PMMarvin
12/07/2023, 9:57 PMparameters
section. Parameters in the deployment.yaml
file should be defined as JSON key-value pairs.
Here’s an example of how to include a Jinja template inside the `deployment.yaml`:
yaml
# Example content in deployment.yaml
version: "1"
type: "deployment"
name: "my-deployment"
# ... other configurations ...
parameters:
param1: "{{ event.data.value1 }}"
param2: "{{ event.data.value2 }}"
In this case, param1
and param2
are parameters expected by the entrypoint flow function within your deployment, and they will be filled in with values from an event object when the flow run is triggered. The Jinja template specifies the source of the parameter values using event data.
When you apply this deployment YAML via the prefect deployment apply
command, this configuration would be set up in the Prefect Cloud API. You can then trigger flow runs based on this deployment, and the parameters will be dynamically resolved using the Jinja templates you’ve included in the YAML file.
The Prefect UI doesn't always show you raw Jinja templates for direct editing. Still, it allows you to edit default parameter values from the details page for a deployment by selecting Edit from the commands menu. If the parameter values are intended to be dynamic, you typically set up the Jinja templating within the deployment YAML itself or handle them programmatically, if the UI supports Jinja templating syntax directly.Luke He
12/07/2023, 10:08 PMMarvin
12/07/2023, 10:08 PM:
, which is used to denote key-value pairs, or the curly braces {}
used for objects). If the value contains special characters, using quotes is necessary to ensure the string is interpreted correctly.
For Jinja templates within YAML files, it's common practice to use quotes because the templates often contain characters that YAML could misinterpret. Here is how you might write the parameters with a Jinja template in a `deployment.yaml`:
yaml
# Example content in deployment.yaml with quoted Jinja templates
version: "1"
type: "deployment"
name: "my-deployment"
# ... other configurations ...
parameters:
param1: "{{ '{{ event.data.value1 }}' }}"
param2: "{{ '{{ event.data.value2 }}' }}"
In this example, double quotes are used around the Jinja template within the YAML to clearly indicate that the enclosed {{ ... }}
are part of a string rather than YAML syntax.
However, if the auto-generated deployment YAML from prefect deployment build
does not include quotes around Jinja template strings, it's possible that Prefect's parsing of YAML files correctly handles these templates without quotes. In that case, you can follow the generated format convention as output by the Prefect CLI tool.
Be sure to test your deployment YAML with the flow and the Prefect Cloud UI to ensure that the Jinja templates are being properly interpreted. If there is any ambiguity in the parsing, or you encounter issues, you may choose to add quotes to the Jinja templates for clarity.Nate
12/07/2023, 10:16 PMdeployment.yaml
file, itd be in a prefect.yaml
, specifically, itd be in your triggers
for a given deployment definition.
if its helpful, here's one of my webhook templates where I grab stuff off the event payload