Skip to Content
ProvidersBuilt-in providers

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.

/trigger/github.ts
import { ,  } from "@trigger.dev/sdk";

export const github = ..define({
  : "github",
  : .github(),
});

export const githubEvents = ({
  id: "github-events",
  : github,
  : "header.x-github-event == 'issues'",
  : async ({  }) => {
    .();
  },
});
SettingValue
Sourcewebhooks.github()
VerificationHMAC-SHA256 in x-hub-signature-256
CredentialSecret you generate on Trigger.dev
Event typeheader.x-github-event
GitHub docsGitHub 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:

/trigger/github.ts
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:

/trigger/wait-for-pull-request.ts
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

  1. Start trigger dev or deploy, then copy the endpoint's Webhook URL from the Webhooks page.

  2. On the endpoint's Settings tab, choose Generate secret and copy the value. GitHub accepts a secret you choose.

  3. 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 a ping, which shows as Filtered for subscribers that don't match it. Or create a repository webhook with the gh CLI:

    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=true

    Use orgs/ORG/hooks for 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, set content_type to json.

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

On this page