Implementation:PrefectHQ Prefect Resume Flow Run On PR Merge
| Knowledge Sources | |
|---|---|
| Domains | Orchestration, CI_CD, Webhooks, Automations |
| Last Updated | 2026-02-09 22:00 GMT |
Overview
Example demonstrating automatic resumption of failed Prefect flow runs when a GitHub PR containing the flow run URL is merged, using webhooks and automations.
Description
The resume_flow_run_on_pr_merge.py example shows an advanced integration pattern between GitHub and Prefect Cloud. It documents a three-part setup: (1) a Prefect Cloud webhook that receives GitHub PR events and extracts flow run IDs from PR bodies using a Jinja template with the `flow_run_id` filter, (2) a GitHub webhook configured to send pull_request events to Prefect Cloud, and (3) a Prefect automation that triggers on `github.pull_request.closed` events where `pr.merged` is True and the resource ID matches a flow run, changing the flow run state back to Scheduled to resume execution.
Usage
Use this pattern when you want to automate the retry of failed flow runs after deploying a code fix. When a flow run fails, include its URL in the PR body that fixes the issue. Upon merge, the automation automatically resumes the flow run.
Code Reference
Source Location
- Repository: PrefectHQ_Prefect
- File: examples/resume_flow_run_on_pr_merge.py
- Lines: 1-123
Signature
@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!")
Import
import json
from pathlib import Path
from prefect import flow
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config.json | file | Yes | JSON config file read by the flow |
| GitHub PR event | webhook payload | Yes | Pull request event from GitHub (for automation) |
| Prefect Cloud webhook URL | string | Yes | Webhook endpoint that receives GitHub events |
Outputs
| Name | Type | Description |
|---|---|---|
| Flow run state change | Scheduled | Failed flow run is rescheduled upon PR merge |
| Prefect event | Event | Transformed GitHub event with flow run resource ID |
Usage Examples
Webhook Jinja 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) }}"
}
}Sample Flow
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()
PR Body Format
This PR fixes the data validation issue.
Fixes: https://app.prefect.cloud/account/.../workspace/.../runs/flow-run/abc123-...