<@ULVA73B9P> I'm trying to setup an automation. I ...
# ask-marvin
n
@Marvin I'm trying to setup an automation. I want to call a webhook once a flow run of a deployment finishes in state Completed. I have multiple deployments for a single flow. However, not all deployments should trigger the automation.
m
thought for 330.8 seconds
Yes — you can do this with a Prefect Cloud Automation that listens for flow runs entering Completed, filters to only the deployments you care about, then calls a webhook. Two common ways to target only some deployments: 1) Filter by specific deployment(s) (recommended if you know which ones) 2) Filter by a tag that only those deployments add to their runs (nice if you’ll change the set over time) UI steps (Prefect Cloud) - Go to Automations → Create automation - Trigger - Type: Flow run enters a state - State: Completed - Filters → Add “Related resource” → choose “Deployment” and select the deployment(s) you want to include - Action - Choose “Call a webhook” - Pick your Webhook block - (Optional) Add a templated JSON payload; Jinja variables like
{{ flow_run.id }}
and
{{ flow_run|ui_url }}
are supported - Save and enable Infrastructure-as-code example (YAML for prefect automation create) - This example targets two specific deployments by ID and calls a webhook with templated details.
Copy code
name: Notify on selected deployments completed
description: Call webhook when flow run completes for selected deployments
enabled: true
trigger:
  type: event
  posture: Reactive
  expect:
    - prefect.flow-run.Completed
  match:
    prefect.resource.id: "prefect.flow-run.*"
  match_related:
    - prefect.resource.id:
        - "prefect.deployment.11111111-2222-3333-4444-555555555555"
        - "prefect.deployment.aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
      prefect.resource.role: "deployment"
actions:
  - type: call-webhook
    block_document_id: "00000000-1111-2222-3333-444444444444"
    payload: |
      {
        "flow_run_id": "{{ flow_run.id }}",
        "flow_name": "{{ flow.name }}",
        "deployment": "{{ deployment.name }}",
        "state": "{{ flow_run.state.name }}",
        "ui_url": "{{ flow_run|ui_url }}"
      }
- Apply it:
Copy code
prefect automation create --from-file automation.yaml
Tag-based alternative (no need to list deployments) - Add a tag (e.g., notify-webhook) to the deployments you want included so their flow runs inherit it. - Use this trigger filter instead:
Copy code
trigger:
  type: event
  posture: Reactive
  expect:
    - prefect.flow-run.Completed
  match:
    prefect.tag: "notify-webhook"
Notes - The webhook action uses a Webhook block. If you haven’t created one yet, go to Blocks → Create block → Webhook and set the URL/method/headers. - Jinja context in the payload supports rich objects like
flow_run
,
deployment
, and filters like
{{ flow_run|ui_url }}
for a direct link. Helpful docs - Automations overview: Automations (v3) - Events and resource matching (filters): Event triggers and matching - Call a webhook action: Automation actions - Webhook block: Webhook block API Want me to draft this for your exact deployments? If you share the deployment IDs/names you want included (or a tag you prefer), and your webhook’s expected payload shape, I can tailor the YAML/UI steps for you.
n
@Marvin Does this also work with a self-hosted server?
m
thought for 302.7 seconds
Yes. In Prefect 3.x self-hosted Server, Automations and the Call a webhook action work the same as in Cloud. You can trigger on flow runs entering Completed and filter to only specific deployments, then invoke a webhook via a Webhook block. The YAML/CLI examples I shared apply unchanged to self-hosted. Notes - Create a Webhook block in your self-hosted UI (or via code) and reference its block ID in the automation action. - Make sure your self-hosted deployment includes the background services that process events/automations (the official docker-compose/Helm setups do). - Only the “inbound Webhooks” feature (receiving external HTTP calls at a Prefect-managed URL) is Cloud-only. Calling out to a webhook from an automation is fully supported in OSS. Docs - Automations: Automations (v3) - Event triggers and matching (filters for deployments, tags, etc.): Event triggers and matching - Webhook block API (used by “Call a webhook” action): Webhook block API - Self-hosted reference: Self-hosted guide - Cloud-only inbound webhooks (for awareness): Webhooks (Cloud-only) If you’d like, share which deployments to include (IDs or names) and I’ll tailor a ready-to-apply
automation.yaml
for your self-hosted instance.
n
@Marvin Can I also add flow parameters in the payload to the webhook?
m
Error: unhandled errors in a TaskGroup (1 sub-exception)