Skip to main content

This version of GitHub Enterprise Server was discontinued on 2023-09-25. No patch releases will be made, even for critical security issues. For better performance, improved security, and new features, upgrade to the latest version of GitHub Enterprise Server. For help with the upgrade, contact GitHub Enterprise support.

Automatically redelivering failed deliveries for an organization webhook

You can write a script to handle failed deliveries of an organization webhook.

About automatically redelivering failed deliveries

This article describes how to write a script to find and redeliver failed deliveries for an organization webhook. For more information about failed deliveries, see "Handling failed webhook deliveries."

This example shows you:

  • A script that will find and redeliver failed deliveries for an organization webhook
  • What credentials your script will need, and how to store the credentials securely as GitHub Actions secrets
  • A GitHub Actions workflow that can securely access your credentials and run the script periodically

This example uses GitHub Actions, but you can also run this script on your server that handles webhook deliveries. For more information, see "Alternative methods."

Storing credentials for the script

The built in GITHUB_TOKEN does not have sufficient permissions to redeliver webhooks. Instead of using GITHUB_TOKEN, this example uses a personal access token. Alternatively, instead of creating a personal access token, you can create a GitHub App and use the app's credentials to create an installation access token during the GitHub Actions workflow. For more information, see "Making authenticated API requests with a GitHub App in a GitHub Actions workflow."

  1. Create a personal access token with the admin:org_hook and repo scope. For more information, see "Managing your personal access tokens."

  2. Store your personal access token as a GitHub Actions secret in the repository where you want the workflow to run. For more information, see "Using secrets in GitHub Actions."

Adding a workflow that will run the script

This section demonstrates how you can use a GitHub Actions workflow to securely access the credentials that you stored in the previous section, set environment variables, and periodically run a script to find and redeliver failed deliveries.

Copy this GitHub Actions workflow into a YAML file in the .github/workflows directory in the repository where you want the workflow to run. Replace the placeholders in the Run script step as described below.

YAML
name: Redeliver failed webhook deliveries
on:
  schedule:
    - cron: '15 */6 * * *'
  workflow_dispatch:

This workflow runs every 6 hours or when manually triggered.

permissions:
  contents: read

This workflow will use the built in GITHUB_TOKEN to check out the repository contents. This grants GITHUB_TOKEN permission to do that.

jobs:
  redeliver-failed-deliveries:
    name: Redeliver failed deliveries
    runs-on: ubuntu-latest
    steps:
      - name: Check out repo content
        uses: actions/checkout@v4

This workflow will run a script that is stored in the repository. This step checks out the repository contents so that the workflow can access the script.

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18.x'

This step sets up Node.js. The script that this workflow will run uses Node.js.

      - name: Install dependencies
        run: npm install octokit

This step installs the octokit library. The script that this workflow will run uses the octokit library.

      - name: Run script
        env:
          TOKEN: ${{ secrets.YOUR_SECRET_NAME }}
          ORGANIZATION_NAME: 'YOUR_ORGANIZATION_NAME'
          HOOK_ID: 'YOUR_HOOK_ID'
          LAST_REDELIVERY_VARIABLE_NAME: 'YOUR_LAST_REDELIVERY_VARIABLE_NAME'
          HOSTNAME: 'YOUR_HOSTNAME'
          WORKFLOW_REPO_NAME: ${{ github.event.repository.name }}
          WORKFLOW_REPO_OWNER: ${{ github.repository_owner }}
        run: |
          node .github/workflows/scripts/redeliver-failed-deliveries.js

This step sets some environment variables, then runs a script to find and redeliver failed webhook deliveries.

  • Replace YOUR_SECRET_NAME with the name of the secret where you stored your personal access token.
  • Replace YOUR_ORGANIZATION_NAME with the name of the organization where the webhook was created.
  • Replace YOUR_HOOK_ID with the ID of the webhook.
  • Replace YOUR_LAST_REDELIVERY_VARIABLE_NAME with the name that you want to use for a configuration variable that will be stored in the repository where this workflow is stored. The name can be any string that contains only alphanumeric characters and _, and does not start with GITHUB_ or a number. For more information, see "Variables."
  • Replace YOUR_HOSTNAME with the name of your GitHub Enterprise Server instance.
#
name: Redeliver failed webhook deliveries

# This workflow runs every 6 hours or when manually triggered.
on:
  schedule:
    - cron: '15 */6 * * *'
  workflow_dispatch:

# This workflow will use the built in `GITHUB_TOKEN` to check out the repository contents. This grants `GITHUB_TOKEN` permission to do that.
permissions:
  contents: read

#
jobs:
  redeliver-failed-deliveries:
    name: Redeliver failed deliveries
    runs-on: ubuntu-latest
    steps:
      # This workflow will run a script that is stored in the repository. This step checks out the repository contents so that the workflow can access the script.
      - name: Check out repo