Skip to content

Webhook Based On Caddy

Introduction

This article introducing a method on building webhook service with CaddyServer.

1. Install CaddyServer

The easiest way to install caddy is using package manager.

  • On Debian/Ubuntu

    sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
        | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
        | sudo tee /etc/apt/sources.list.d/caddy-stable.list
    sudo apt update
    sudo apt install caddy
    

  • On RHEL/CentOS/RockyLinux

    dnf install 'dnf-command(copr)'
    dnf copr enable @caddy/caddy
    dnf install caddy
    

2. Add Modules for Caddy

  • Add Packages Online

    sudo caddy add-package XXX
    
    XXX is the package name, which always start with github.com/xxxxxx.

    It's easy to find packages name from Download Page.

    Examples:

    install webhook module:

    sudo caddy add-package github.com/WingLim/caddy-webhook

    install exec module:

    sudo caddy add-package github.com/mholt/caddy-events-exec

  • Add Packages Manually

    Select and Download CaddyServer with modules from Download Page.

    Replace executable caddy file with downloaded:

    sudo mv caddy /usr/bin
    sudo chmod +x /usr/bin/caddy
    sudo restorecon /usr/bin/caddy
    sudo chown root:root /usr/bin/caddy
    

3. Modify CaddyServer's service file

Due to the configuration in default systemd file, the parameter ProtectSystem=full, caddy lose the permission on writing /usr, /boot and /etc.(Reference-2 for details).

  • Add ReadWrite permission to caddy web folder /usr/share/caddy Adding paramter to service file /usr/lib/systemd/system/caddy.service under [service] lable:

    ReadWritePaths=/usr/share/caddy

  • Reload systemd

    sudo systemctl daemon-reload

4. Config Caddyfile

  • Webhook Module Configure introduction is available: caddy-webhook

        route /webhook {
            webhook {
                repo https://github.com/WingLim/winglim.github.io.git
                path /usr/share/caddy
                branch master
                depth 1
                type github/gitlab/gitee/gogs/bitbucket
                command 
                username
                password
                key
                token
                submodule
            }
        }
    

  • Exec Module Configure introduction is available: caddy-exec

        route /webhook {
            exec {
                command git
                args pull
                directory /usr/share/caddy
                log file /var/log/caddy/git.log
            }
        }
    

REF

[1]. https://caddyserver.com/docs/install

[2]. https://www.redhat.com/sysadmin/mastering-systemd

[3]. https://github.com/abiosoft/caddy-exec

[4]. https://github.com/WingLim/caddy-webhook