Skip to content

Instantly share code, notes, and snippets.

@Bardin08
Created May 10, 2024 10:43
Show Gist options
  • Save Bardin08/6d0b23a8966269156d126681a84e53d6 to your computer and use it in GitHub Desktop.
Save Bardin08/6d0b23a8966269156d126681a84e53d6 to your computer and use it in GitHub Desktop.

Prerequisites:

  • A GitHub account
  • A repository on GitHub where you want to receive webhook events
  • A server with a public endpoint (or the ability to use tools like ngrok to expose your local server) to receive webhook requests

Steps:

  1. Access Webhook Settings:

    • Navigate to your target GitHub repository.
    • Go to the Settings tab.
    • In the left sidebar, click on Webhooks.
  2. Add a New Webhook:

    • Click the Add webhook button.
  3. Configure Webhook Details:

    • Payload URL: Enter the public URL of your server endpoint that will handle incoming webhook requests. If you're testing on your local machine, you can utilize a tool like ngrok (see the previous guide for instructions).
    • Content type: Select "application/json" as this is the standard format for GitHub webhooks.
    • Secret: (Optional but recommended) Enter a secret key for validating the authenticity of webhook requests.
    • SSL verification: It's strongly advised to enable SSL verification for security.
    • Which events would you like to trigger this webhook? Select "Let me select individual events."
    • Individual Events:
      • Expand the "Repository events" section.
      • Select the specific repository events you want to trigger the webhook for (e.g., "Issues," "Pull Requests," "Pushes," etc.).
    • Active: Ensure the checkbox is selected to activate your webhook.
  4. Save and Review:

    • Click Add webhook to save your configuration.
    • GitHub will send a test request ("ping") to your endpoint to verify its availability. You should see the result of this under the webhook's delivery history.

Testing Your Webhook

  1. Trigger an Event: Perform an action within your GitHub repository that corresponds to one of the events you selected. For example, create a new issue, open a pull request, or push a commit.

  2. Verify Delivery: Check your server logs or the endpoint you've configured to receive the webhook. You should see the incoming webhook payload with data about the event that was triggered in your repository.

Important Notes:

  • Security: If you've set up a secret key, make sure your server is configured to validate the incoming requests using that secret to ensure they are indeed from GitHub.
  • Error Handling: Implement robust error handling on your server to log and monitor any issues with receiving or processing webhooks.
  • Payload Details: To explore what data is included in the webhook payload for different events, refer to GitHub's documentation: https://docs.github.com/en/webhooks/webhook-events-and-payloads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment