Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:PrefectHQ Prefect Resume Flow Run On PR Merge

From Leeroopedia
Revision as of 16:22, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/PrefectHQ_Prefect_Resume_Flow_Run_On_PR_Merge.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

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-...

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment