Home

Awesome

webhook

Image Size Last Commit Build Status Become a GitHub Sponsor

A lightweight, minimal webhook container

Usage

Docker images are available from both GitHub Container Registry (GHCR) and Docker Hub.

If you would prefer to pull from GHCR, simply replace thecatlady/webhook with ghcr.io/thecatlady/webhook in the examples below.

Docker Compose (recommended)

Add the following volume and service definitions to a docker-compose.yml file:

services:
  webhook:
    image: thecatlady/webhook
    container_name: webhook
    command: -verbose -hooks=hooks.yml -hotreload
    environment:
      - TZ=America/New_York #optional
    volumes:
      - /path/to/appdata/config:/config:ro
    ports:
      - 9000:9000
    restart: always

Then, run the following command from the directory containing your docker-compose.yml file:

docker-compose up -d

Docker CLI

Run the following command to create the container:

docker run -d \
  --name=webhook \
  -e TZ=America/New_York `#optional` \
  -v /path/to/appdata/config:/config:ro \
  -p 9000:9000 \
  --restart always \
  thecatlady/webhook \
  -verbose -hooks=hooks.yml -hotreload

Updating

The process to update the container when a new image is available is dependent on how you set it up initially.

Docker Compose

Run the following commands from the directory containing your docker-compose.yml file:

docker-compose pull webhook
docker-compose up -d
docker image prune

Docker CLI

Run the commands below, followed by your original docker run command:

docker stop webhook
docker rm webhook
docker pull thecatlady/webhook
docker image prune

Parameters

The container image is configured using the following parameters passed at runtime:

ParameterFunction
-e TZ=TZ database name of system time zone; e.g., America/New_York
-v /path/to/appdata/config:/config:roContainer data directory (mounted as read-only); your JSON/YAML hook definition file should be placed in this folder<br/>(Replace /path/to/appdata/config with the desired path on your host)
-p 9000:9000Expose port 9000<br/>(Necessary unless only accessing webhook via other containers in the same Docker network)
--restartContainer restart policy<br/>(always or unless-stopped recommended)
-verbose -hooks=/config/hooks.yml -hotreloadwebhook parameters; replace hooks.yml with the name of your JSON/YAML hook definition file, and add/modify/remove arguments to suit your needs<br/>(Can omit if using this exact configuration; otherwise, all parameters must be specified, not just those modified)

Configuring Hooks

See adnanh/webhook for documentation on how to define hooks.

Considerations for running inside the Docker container

You can set your execute-command to be a shell script that checks if any of the commands required exist, and if not installs them. Check and then install is useful because it will check and install when the first webhook request is received after creating the container, but not reinstall every time a webhook is received.

Here is an example of using webhook with git to retrieve the latest commit changes for a repository:


Example docker-compose.yml

services:
  webhook:
    image: thecatlady/webhook
    container_name: webhook
    command: -verbose -hooks=hooks.yml -hotreload
    environment:
      - TZ=America/New_York #optional
    volumes:
      - /path/to/appdata/config:/config:ro
      - /path/to/parent/git_folder:/opt/git
      - /path/to/.ssh:/root/.ssh:ro
    ports:
      - 9000:9000
    restart: always

In the above:


Example hooks.json (placed at /path/to/appdata/config/hooks.json)

[
  {
    "id": "my-hook-name",
    "execute-command": "/config/run/git-checkout-force.sh",
    "command-working-directory": "/opt/git/myrepo",
    "include-command-output-in-response": true,
    "include-command-output-in-response-on-error": true,
    "pass-arguments-to-command": [
      { "source": "payload", "name": "head_commit.id", "comment": "GIT_REF" },
      {
        "source": "string",
        "name": "/opt/git/myrepo",
        "comment": "GIT_DIR"
      },
      { "source": "string", "name": "1000", "comment": "PUID" },
      { "source": "string", "name": "1000", "comment": "PGID" }
    ],
    "trigger-rule": {
      "and": [
        {
          "match": {
            "type": "payload-hmac-sha1",
            "secret": "<YOUR_GITHUB_WEBHOOK_SECRET>",
            "parameter": { "source": "header", "name": "X-Hub-Signature" }
          }
        },
        {
          "match": {
            "type": "value",
            "value": "refs/heads/main",
            "parameter": { "source": "payload", "name": "ref" }
          }
        }
      ]
    }
  }
]

In the above:


Example git-checkout-force.sh (placed at /path/to/appdata/config/run/git-checkout-force.sh)

#!/usr/bin/env sh

# variables
GIT_REF=${1}
GIT_DIR=${2}
PUID=${3}
PGID=${4}

# log date
date

# install git
if ! command -v git > /dev/null 2>&1; then
    apk add --no-cache \
        git
fi

# install openssh
if ! command -v ssh > /dev/null 2>&1; then
    apk add --no-cache \
        openssh
fi

# allow git with different ownership
git config --global --add safe.directory ${GIT_DIR}

# fetch from git
git fetch --all

# checkout git reference
git checkout --force ${GIT_REF}

# set ownership
chown -R ${PUID}:${PGID} ${GIT_DIR}

In the above:


Important takeaways:

How to Contribute

Show your support by starring this project! 🌟 Pull requests, bug reports, and feature requests are also welcome!

You can also support me by becoming a GitHub sponsor or making a one-time donation 💖