<@ULVA73B9P> In prefect 3, on a self hosted server...
# ask-marvin
n
@Marvin In prefect 3, on a self hosted server, is it possible to trigger an automation only if a flow run has a specific job variable?
m
thought for 123.0 seconds
Short answer: not directly. In Prefect 3.x (self‑hosted or Cloud), automations match on event labels from flow-run events. While flow runs do have a
job_variables
field, those values are not included in the event labels — so you can’t filter an automation directly by a specific job variable. What you can do instead: - Use tags - Tags are exposed on flow-run events (as related resources), so you can tag the deployment or add a tag at run time that represents the job variable (e.g.,
env:prod
) and have the automation match on that tag. - Emit a custom event that includes the job variable(s) - From your flow, read
job_variables
and emit a custom event with labels you can match in an automation. Example custom event approach:
Copy code
from prefect import flow, get_run_context
from prefect.events import emit_event

@flow
def my_flow():
    ctx = get_run_context()
    job_vars = (ctx.flow_run.job_variables or {})
    if job_vars.get("environment") == "production":
        emit_event(
            event="my-app.flow-run.check",
            resource={
                "prefect.resource.id": f"prefect.flow-run.{ctx.flow_run.id}",
                "environment": job_vars["environment"],
            },
        )
Then configure an automation that expects
my-app.flow-run.check
and matches
environment=production
. Helpful docs: - Automations and event matching: Automations - Events and matching: Events - Custom events (recommended workaround): Using custom event grammar If you can share the specific job variable you want to filter on, I can suggest the cleanest tagging or custom-event pattern for your setup.
n
@Marvin Can I match on all tags starting with a prefix?
m
thought for 171.8 seconds
Yes. In Prefect 3.x automations you can match tags by prefix using wildcard patterns in the resource matcher. Tags are emitted as related resources, so use match_related and a trailing wildcard on the tag resource id. Example: match any flow-run event with a tag starting with prod-
Copy code
{
  "type": "event",
  "expect": [
    "prefect.flow-run.Completed"
  ],
  "match": {
    "prefect.resource.id": "prefect.flow-run.*"
  },
  "match_related": {
    "prefect.resource.id": "prefect.tag.prod-*",
    "prefect.resource.role": "tag"
  }
}
Notes about pattern semantics: - Trailing wildcard only: "prefect.tag.prod-*" works; "*prod" or "prod-*-west" do not. - Negation is supported: "!prefect.tag.dev-" excludes any dev- tag. - Lists are OR’d within a label: ["prefect.tag.prod-*", "prefect.tag.staging-*"] matches either. - Multiple labels are AND’d together. - Matching is case-sensitive. Docs and source: - Event triggers and matching - Source implementation: matches/ResourceSpecification