GitHub webhooks
Receive and verify GitHub webhooks with Trigger.dev. Check the HMAC-SHA256 signature in x-hub-signature-256 and run a task for each GitHub event, filtered on header.x-github-event.
webhooks.github() verifies the HMAC-SHA256 signature GitHub sends in x-hub-signature-256 on every request.
import { , } from "@trigger.dev/sdk";
export const github = ..define({
: "github",
: .github(),
});
export const githubEvents = ({
id: "github-events",
: github,
: "header.x-github-event == 'issues'",
: async ({ }) => {
.();
},
});| Setting | Value |
|---|---|
| Source | webhooks.github() |
| Verification | HMAC-SHA256 in x-hub-signature-256 |
| Credential | Secret you generate on Trigger.dev |
| Event type | header.x-github-event |
| GitHub docs | GitHub webhook docs |
Type GitHub events
webhooks.github() types event as an open object. Pass a type from @octokit/openapi-webhooks-types, which must be installed in your project, for typed fields and autocomplete:
import type { } from "@octokit/openapi-webhooks-types";
import { , } from "@trigger.dev/sdk";
type PullRequestEvent =
| ["schemas"]["webhook-pull-request-opened"]
| ["schemas"]["webhook-pull-request-closed"];
export const github = ..define({
: "github",
: .github<PullRequestEvent>(),
});
export const pullRequestClosed = ({
id: "pull-request-closed",
: github,
filter: "header.x-github-event == 'pull_request' && event.action == 'closed'",
: async ({ event }) => {
.("Pull request closed", event.pull_request.number, event.pull_request.merged);
},
});Wait for a pull request event
A waiter pauses a run until a matching delivery arrives. This task uses the typed github endpoint above and waits until a pull request closes:
import { , , } from "@trigger.dev/sdk";
import { } from "./github";
export const waitForPullRequest = ({
: "wait-for-pull-request",
: async ({ pullRequestNumber }: { pullRequestNumber: number }) => {
const waiter = await ..create(, {
: {
"header.x-github-event": "pull_request",
"event.pull_request.number": pullRequestNumber,
"event.action": "closed",
},
: "7d",
// A retried attempt gets this waiter (and its event) back instead of making a new one
: `pr-${pullRequestNumber}-closed`,
});
const result = await ..wait(waiter);
if (!result.ok) {
// A cancelled waiter says nothing about the pull request. Fail without retrying,
// since a retry gets the same cancelled waiter back
if (result.error.name !== "WaitpointTimeoutError") throw new (result.error.message);
// Timed out: the pull request didn't close within 7 days
return { closed: false };
}
return { closed: true, merged: result.output.event.pull_request.merged };
},
});A waiter only matches deliveries that arrive after it is created. Create it before the step that opens or closes the pull request, or check the pull request's state after creating it, so a close that happens first isn't missed. This assumes a repository webhook. With an organization webhook or a GitHub App, pull request numbers repeat across repositories, so also match event.repository.full_name. To wait on a branch instead, match event.pull_request.head.ref. Branch names aren't unique across forks, so the first pull request from that branch name to close resumes the run.
Set up GitHub
-
Start
trigger devor deploy, then copy the endpoint's Webhook URL from the Webhooks page. -
On the endpoint's Settings tab, choose Generate secret and copy the value. GitHub accepts a secret you choose.
-
Create the webhook. In the repository, go to Settings > Webhooks > Add webhook. Paste the webhook URL as the Payload URL, set Content type to
application/json, and paste the secret. Under the events, choose Let me select individual events and select the events your subscribers need, such as Pull requests. GitHub selects Pushes by default, so untick it unless a subscriber needs push events. GitHub's first delivery is aping, which shows as Filtered for subscribers that don't match it. Or create a repository webhook with theghCLI:gh api repos/OWNER/REPO/hooks -f name=web -f "config[url]=<webhook URL>" -f "config[content_type]=json" -f "config[secret]=<secret>" -f "events[]=issues" -f "events[]=pull_request" -F active=trueUse
orgs/ORG/hooksfor an organization webhook. For a GitHub App, set the webhook URL and secret in the app's settings. If you configure an App's webhook through the API, setcontent_typetojson. -
Trigger an event in the repository, or use the endpoint's Test tab, and check the endpoint's Deliveries tab. See Verify the connection if nothing arrives.
Example project
The API change notifier example starts an AI agent when a merged pull request changes an OpenAPI spec, then uses a waiter to pause the run until a second GitHub event, the docs PR being closed, arrives.
Was this page helpful?
Last updated
Edit on GitHub