<@ULVA73B9P> How do deploy an automation programma...
# marvin-ai
a
@Marvin How do deploy an automation programmatically using python? And then how to update it if it already exists on when i deploy a new version of my app? Currently I am doing this and the automatons stop working when I deploy new versions.
Copy code
shared_job_variables = {
    # stuff here
}

deploy11 = my_flow.to_deployment(
        name="deploy1",
        cron="* 10 * * *",
        job_variables=shared_job_variables,
        work_queue_name="slow-queue",
        tags=["awesome-things"],
        triggers=[
            DeploymentEventTrigger(
                name="Trigger deploy1 after deploy2 completion",
                expect={"prefect.flow-run.Completed"},
                match_related={
                    "prefect.resource.name": "deploy2"
                },
            )
        ],
    )

deploy2 = my_other_flow.to_deployment(
    # similar setup
)

deployables = (deploy1, deploy2)

deploy(
    *deployables,
    work_pool_name=CONSTANTS.DEFAULT_WORKPOOL,
    build=False,
    push=False,
    image=f"{docker_image}:{docker_tag}",
)
m
thought for 1484.2 seconds
Great question — and you’re not alone here. In Prefect 3.x there are two ways to manage “automations” that chain deployments: 1) Deployment-linked triggers (what you’re doing now with
triggers=[DeploymentEventTrigger(...)]
on a deployment) 2) Standalone Automations (created/managed directly via the Automations API) If your “automations stop working when I deploy new versions,” it’s usually because deployment-linked triggers are re-created on each deploy. If the new deploy omits the triggers or the trigger’s match no longer matches (e.g., resource labels changed, flow/deployment name changed), they’ll stop firing. Below are working patterns for both approaches and how to update them idempotently. Option A — Keep using deployment-linked triggers (recreated on each deploy) - Make sure you pass
triggers=[...]
on every deploy; they are regenerated and owned by that deployment. - Use a stable matcher for the other deployment. Prefer matching by the other deployment’s id, or use the full resource name (“flow-name/deployment-name”), not just the deployment name. Example (id-based match):
Copy code
from prefect import flow
from prefect.deploy import deploy
from prefect.deployment import get_deployment
from prefect.events import DeploymentEventTrigger

@flow
def my_flow(): ...

@flow
def my_other_flow(): ...

# Resolve the source deployment id you want to listen to
src = get_deployment(name="my-other-flow/deploy2")  # full "flow-name/deployment-name"

deploy1 = my_flow.to_deployment(
    name="deploy1",
    cron="* 10 * * *",
    work_queue_name="slow-queue",
    tags=["awesome-things"],
    job_variables=shared_job_variables,
    triggers=[
        DeploymentEventTrigger(
            expect={"prefect.flow-run.Completed"},
            match_related={"prefect.resource.id": str(src.id)},  # stable match!
            posture="Reactive",
        )
    ],
)

deploy2 = my_other_flow.to_deployment(
    name="deploy2",
    cron="0 12 * * *",
    work_queue_name="slow-queue",
    tags=["awesome-things"],
    job_variables=shared_job_variables,
)

deploy(
    deploy1,
    deploy2,
    work_pool_name=CONSTANTS.DEFAULT_WORKPOOL,
    build=False,
    push=False,
    image=f"{docker_image}:{docker_tag}",
)
Tips: - If you prefer name matching, use the full resource name:
match_related={"prefect.resource.name": "my-other-flow/deploy2"}
. The “resource name” for a deployment is typically “flow-name/deployment-name”. - These triggers are recreated at deploy time; if they “disappear,” it’s because your redeploy did not include them or the matcher no longer matches the new resource labels. Docs: - Creating deployment triggers - Event trigger grammar and matching Option B — Create a standalone Automation (persists across redeploys) and upsert it If you want an automation that survives redeploys (and you update it programmatically), create it with the Automations API and “upsert” by name. Example: when deployment B completes, run deployment A ``` from prefect.deployment import get_deployment from prefect.events import Automation, EventTrigger from prefect.events.actions import RunDeployment def upsert_chain_automation( name: str, source_full_name: str, # e.g. "my-other-flow/deploy2" target_full_name: str, # e.g. "my-flow/deploy1" ): # Resolve deployment ids to make matching robust src = get_deployment(name=source_full_name) tgt = get_deployment(name=target_full_name) trigger = EventTrigger( expect={"prefect.flow-run.Completed"}, match_related={"prefect.resource.id": str(src.id)}, # or use prefect.resource.name with the full name posture="Reactive", ) # Action: run the target deployment (you can also pass parameters, tags, flow_run_name, etc.) action = RunDeployment(deployment_id=str(tgt.id)) try:
a = Automation.read(name=name) # update-if-exists a.trigger = trigger a.actions = [action] a.enabled = True a.update() except ValueError: a = Automation( name=name, trigger=trigger, actions=[action], enabled=True, ) a.create() ``` Why this helps: - Standalone Automations are not owned by a deployment. They do not get deleted/recreated during deploys, so they continue to work across new versions. - You can idempotently “upsert” them by reading by name and updating if found. Docs: - Creating automations programmatically - Event trigger grammar - Chaining deployments with events Why your current triggers may stop working - The deployment-linked triggers are replaced on each deploy. If the redeploy does not include
triggers=[...]
, they won’t be recreated. - Matching by an imprecise label (e.g., using just “deploy2” instead of the full “flow-name/deploy2”, or using name when it changed) will stop the automation from firing. - Matching by id is the most robust, since ids are stable for that specific deployment record. Questions to confirm so I can tailor the fix: - Are you trying to keep the triggers attached to the deployments, or would you prefer standalone Automations that survive redeploys? - Can you share the exact “flow-name/deployment-name” for both deployments (or confirm if the names change between deploys)? - Which Prefect version are you on (assuming 3.x)? This helps ensure we use the right API surface.