Automatic Deployment of Static Sites Using Caddy + GitHub Webhook

| Categories Caddy  | Tags Caddy 

Automatic Deployment of Static Sites Using Caddy + GitHub Webhook

When developing static websites (e.g., Jekyll, Hugo, Hexo), automated deployment can greatly improve workflow efficiency. In this guide, we’ll use GitHub Webhook and Caddy Server to build a lightweight CI system for seamless auto-deployment.

You’ll learn how to:

  • Add the Git plugin to Caddy
  • Configure automatic build logic
  • Set up GitHub Webhook to trigger pull and deployment

1. Prerequisites

Install Caddy with Git Plugin

Use the Caddy download page to build a custom version:

  • Choose your platform: Linux/macOS
  • Select plugin: ✅ http.git

Alternatively, build manually using the official tool (example for Linux):

xcaddy build --with github.com/caddyserver/git
sudo mv caddy /usr/local/bin/

xcaddy is the recommended way to build Caddy with plugins: https://github.com/caddyserver/xcaddy


2. Configure Caddyfile for Auto Deployment

Suppose you have a Jekyll site hosted on GitHub:

  • GitHub repo: https://github.com/yourname/yourblog
  • Build output directory: /var/www/yourblog
  • Build command: jekyll build

Example Caddyfile configuration:

git /github/yourname/yourblog {
    path /var/www/yourblog
    then jekyll build /var/www/yourblog
    hook /webhooks github_webhook_secret_key
    hook_type github
    clone_args --recursive
    pull_args --recurse-submodules
}

Explanation of config:

Directive Meaning
git Mount a Git repo using the Git plugin
path Local path to clone into
then Command to run after each update (e.g., build command)
hook Webhook endpoint and secret
hook_type Specifies webhook origin (e.g., github, gitea)
clone_args Extra flags during initial clone (e.g., recursive submodules)
pull_args Arguments for each git pull

3. Add a GitHub Webhook

  1. Go to your GitHub repo → Settings -> Webhooks
  2. Click Add webhook
  3. Fill in the following:
Field Value
Payload URL http://your-server-ip:port/webhooks
Content type application/json
Secret github_webhook_secret_key (must match Caddy config)
Events Choose Just the push event.

GitHub will now notify your server on each push.


4. Test the Workflow

  1. Make a local change, commit, and push:
git commit -am "update posts"
git push origin main
  1. On your server, Caddy will:
  • Receive the webhook
  • Pull the latest code
  • Run the build command (e.g., Jekyll)
  • Deploy the output directory

You can monitor the log with:

journalctl -u caddy -f
# or if running manually:
caddy run

5. Notes & Tips

  • Absolute Paths: Use full paths for build commands (e.g., /usr/bin/jekyll) to ensure Caddy can locate them.
  • Permissions: The Caddy user must have read/write access to the deployment path.
  • Security: Always set a non-empty Webhook Secret to prevent unauthorized triggers.

6. References