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."
-
Create a personal access token with the
admin:org_hookandreposcope. For more information, see "Managing your personal access tokens." -
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.
#
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 content
uses: actions/checkout@v4
# This step sets up Node.js. The script that this workflow will run uses Node.js.
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18.x'
# This step installs the octokit library. The script that this workflow will run uses the octokit library.
- name: Install dependencies
run: npm install octokit
# 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 "[AUTOTITLE](/actions/learn-github-actions/variables#defining-configuration-variables-for-multiple-workflows)."
# - Replace `YOUR_HOSTNAME` with the name of your GitHub Enterprise Server instance.
- 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
name: Redeliver failed webhook deliverieson:
schedule:
- cron: '15 */6 * * *'
workflow_dispatch:This workflow runs every 6 hours or when manually triggered.
permissions:
contents: readThis 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@v4This 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 octokitThis 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.jsThis step sets some environment variables, then runs a script to find and redeliver failed webhook deliveries.
- Replace
YOUR_SECRET_NAMEwith the name of the secret where you stored your personal access token. - Replace
YOUR_ORGANIZATION_NAMEwith the name of the organization where the webhook was created. - Replace
YOUR_HOOK_IDwith the ID of the webhook. - Replace
YOUR_LAST_REDELIVERY_VARIABLE_NAMEwith 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 withGITHUB_or a number. For more information, see "Variables." - Replace
YOUR_HOSTNAMEwith 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