docs/v3/examples/resume-flow-run-on-pr-merge.mdx
{/*
This page is automatically generated via the generate_example_pages.py script. Any changes to this page will be overwritten.
*/}
<a href="https://github.com/PrefectHQ/prefect/blob/main/examples/resume_flow_run_on_pr_merge.py" target="_blank">View on GitHub</a>
When a flow run fails due to a bug in your code, you typically need to:
This example shows how to automate step 3 by creating a webhook and automation that automatically resumes a failed flow run when a PR containing the flow run URL is merged.
Create a webhook in Prefect Cloud that transforms GitHub PR events into Prefect events. The webhook template extracts the flow run ID from the PR body when present.
Navigate to your workspace's Webhooks page and create a new webhook with this template:
{
"event": "github.{{ headers.get('x-github-event', 'unknown') }}.{{ body.action|default('no-action') }}",
"resource": {
"prefect.resource.id": "{% set frid = body.pull_request.body|flow_run_id %}{% if frid %}prefect.flow-run.{{ frid }}{% else %}github.pr.{{ body.pull_request.number|default(0) }}{% endif %}",
"pr.number": "{{ body.pull_request.number|default(0) }}",
"pr.merged": "{{ body.pull_request.merged|default(false) }}",
"pr.title": "{{ body.pull_request.title|default('')|truncate(100) }}"
}
}
This template uses the flow_run_id filter to extract a flow run UUID from any Prefect Cloud URL
in the PR body. If no flow run URL is found, it falls back to github.pr.<number>.
The pr.merged label enables filtering for merged PRs only.
Copy the webhook URL for the next step.
In your GitHub repository:
application/jsonCreate an automation that triggers when a PR is merged and the event contains a flow run ID.
Navigate to your workspace's Automations page and create a new automation:
Trigger configuration:
github.pull_request.closedprefect.resource.id starting with prefect.flow-run.pr.merged equals TrueAction configuration:
The automation extracts the flow run ID from the event's prefect.resource.id and changes
its state to resume execution.
Here's a simple flow that reads configuration and can fail based on its contents:
import json
from pathlib import Path
from prefect import flow
@flow(log_prints=True)
def my_flow():
config = json.loads(Path("config.json").read_text())
if error := config.get("error"):
raise ValueError(f"Flow failed: {error}")
print("Flow completed successfully!")
if __name__ == "__main__":
my_flow()
When this flow fails:
config.json)This PR fixes the data validation issue.
Fixes: https://app.prefect.cloud/account/.../workspace/.../runs/flow-run/abc123-...
prefect.resource.id set to prefect.flow-run.<uuid>github.pull_request.closed events where pr.merged is True and the resource ID matches a flow runScheduled, which resumes execution