# How to handle GitHub webhook redeliveries without running the same job twice

Gregale · Updated 2026-12-01

Verify GitHub webhook signatures, save receipts before responding, and prevent redeliveries from creating duplicate background jobs.

**Verify a GitHub webhook, save the receipt and job in one transaction,
then return `202`. Run the job separately.** That way GitHub gets a
quick response, and redelivering the same webhook doesn't create a
second job. The durable database write, not a call to an in-memory
background task, is what makes it safe to respond.

Gregale publishes this guide. The example handles a pull request opening
in one configured repository. It records a synthetic review result and
never posts to GitHub or changes a repository.

## Verify GitHub's exact request

Read the raw request body before parsing JSON, then check
`X-Hub-Signature-256` with the endpoint's secret using a constant-time
comparison. Reject a missing secret or invalid signature. Use
[GitHub's signature documentation](https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries)
and test the request through the actual HTTPS route, not just the handler.
The sample then checks the event type and the signed payload's
repository ID, action, and pull request ID.

GitHub's signature covers the body, not the delivery or event headers.
Use a dedicated secret for the endpoint, subscribe only to the events
you need, and use signed fields when choosing what work to do. GitHub
doesn't send Gregale's generic webhook signature headers. This example
uses a normal FastAPI HTTP receiver, not the
[Gregale generic webhook endpoint](https://gregale.dev/docs/inbound-webhooks.md).

## Deduplicate the delivery and the work

These keys answer two different questions:

| Key                                         | What it prevents                                                                    |
| ------------------------------------------- | ----------------------------------------------------------------------------------- |
| `X-GitHub-Delivery`                         | The same delivery creating a second receipt. GitHub preserves the ID on redelivery. |
| Repository ID + pull request ID + operation | Two notifications about the same PR opening creating two review jobs.               |

Give each key a unique database constraint. Insert the receipt and
any new job in the same PostgreSQL transaction. When a repeat arrives,
return success if it matches the receipt you already have. The sample
returns 409 if the same delivery ID arrives with different signed content.

[GitHub expects a 2xx within 10 seconds](https://docs.github.com/en/webhooks/using-webhooks/best-practices-for-using-webhooks).
If you cannot commit the receipt, don't return 202. GitHub
[doesn't automatically redeliver failed deliveries](https://docs.github.com/en/webhooks/using-webhooks/handling-failed-webhook-deliveries).
Arrange manual or scheduled redelivery for missed events and alert when
failures pile up.

## Run the job separately

[Download the FastAPI and PostgreSQL example](https://gregale.dev/examples/github-webhook-redelivery-drill.zip). The same files are on GitHub as [gregale-dev/github-webhook-redelivery](https://github.com/gregale-dev/github-webhook-redelivery).
The demo worker takes one queued row with `FOR UPDATE SKIP LOCKED`
and writes the synthetic result in the same transaction as job
completion. It does not call an external provider. If your real job
posts a comment, sends an email, or calls another API, use that service's
idempotency mechanism where available. A unique database row cannot
undo an external call made before a worker crashes.

The demo keeps its worker endpoint private. For a real webhook, use a
dedicated HTTPS receiver that GitHub can reach without a Gregale bearer
token. Keep signature verification enabled and leave the worker off
the public receiver. Give independent retry and retention rules to
accepted jobs and to failed GitHub deliveries.

## What we verified

The drill passed against local PostgreSQL and the stable HTTPS URL
of a private Gregale Scale app with managed PostgreSQL. An altered
request body failed signature verification and created no job. A
valid redelivery returned the original receipt; a second delivery ID
for the same PR opening created no second job. The database showed
two receipts, one job, and zero effects before the worker ran. Two
worker calls left exactly one effect.

The hosted proof used locally generated signatures and synthetic data.
It did not register a real webhook, test GitHub-to-app connectivity,
or post anything to a repository.

## Questions

### Does GitHub automatically retry failed webhooks?

No. Redeliver failed webhooks from the delivery history or through the API. Return success only after recording the verified delivery durably.

### How do I avoid running a job twice when a GitHub webhook is redelivered?

Store the delivery ID under a unique constraint and use a separate unique key for the business operation. Commit the receipt and job together, and make the worker's effect idempotent too.

Canonical URL: https://gregale.dev/articles/github-webhook-redelivery-idempotency

