Git Product home page Git Product logo

caddy-webhook's Introduction

caddy-webhook

Build License: MIT

Caddy v2 module for serving a webhook.

English | 中文

Installation

Build with xcaddy

xcaddy build \
  --with github.com/WingLim/caddy-webhook

Run in Docker

See caddy-docker for Dockerfile.

DockerHub: winglim/caddy

GitHub Package: winglim/caddy

Usage

Now supported webhook type:

  • github
  • gitlab
  • gitee
  • bitbucket
  • gogs

Caddyfile Format

Notice: webhook block should be the last handler of route. After receive request and handle it, we return nil instead of the next middleware. So, the next handler after webhook will not work.

webhook [<repo> <path>] {
    repo       <text>
    path       <text>
    branch     <text>
    depth      <int>
    type       <text>
    secret     <text>
    command    <text>...
    key	       <text>
    username   <text>
    password   <text>
    token      <text>
    submodule
}
  • repo - git repository url, supported http, https and ssh.
  • path - path to clone and update repository.
  • branch - branch to pull. Default is main.
  • depth - depth for pull. Default is 0.
  • type - webhook type. Default is github.
  • secret - secret to verify webhook request.
  • submodule - enable recurse submodules.
  • command - the command run when repo initializes or get the correct webhook request.
  • key - path of private key, using to access git with ssh.
  • username - username for http auth.
  • password - password for http auth.
  • token - GitHub personal access token.

Example

The full example to run a hugo blog:

Caddyfile:

example.com

root www
file_server

route /webhook {
    webhook {
        repo https://github.com/WingLim/winglim.github.io.git
        path blog
        branch hugo
        command hugo --destination ../www
        submodule   
    }
}

With the config above, webhook module will do things:

  1. Clone https://github.com/WingLim/winglim.github.io.git to blog when initializes.

    1. If the repo is already exist, will fetch and checkout to the branch you set.
  2. Run the command hugo --destination ../www inside the blog directory.

  3. Listen and serve at /webhook and handle the webhook request.

    1. When receive correct webhook request, will update repo and do step 2 again.

Thanks to

caddy-webhook's People

Contributors

git001 avatar winglim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

caddy-webhook's Issues

Caddy2 使用 Cloudflare DNS 插件后,webhook 未能生效

按照你的博客给的步骤,改动是使用了 Cloudflare DNS 插件,折腾了下,github 触发 webhook 无法访问 ,提示403。

Cloudflare DNS 用于其他的网站是正常工作的。

配置如下:

xxx.xyz {
  tls {
      dns cloudflare 123456
  }  
  encode gzip
  root * /data/www/blog
  file_server
  
  handle_errors {
  	@404 {
  		expression {http.error.status_code} == 404
  	}
  	handle @404 {
  		rewrite * /404.html
  		file_server
  	}
  }
  route /webhook {
    webhook {
      repo https://github.com/allanpk716/allanpk716.github.io.git
      branch main
      path /data/www/blog
      secret xxxxx
    }
  }
}

Webhook handler doesn't work when defined in a json config file

I have a caddy server running with my config defined in a JSON file instead of a caddy file. Whenever I hit my webhook endpoint I get the following error:
{"level":"debug","ts":1665975832.1216764,"logger":"http.log.error","msg":"page not found","request":{"remote_ip":"***.***.***.***","remote_port":"29294","proto":"HTTP/2.0","method":"GET","host":"valid.hostname","uri":"/webhook","headers":{"Te":["trailers"],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"],"Accept-Language":["en-US,en;q=0.5"],"Accept-Encoding":["gzip, deflate, br"],"Upgrade-Insecure-Requests":["1"],"Sec-Fetch-Dest":["document"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-Site":["none"],"Sec-Fetch-User":["?1"]},"tls":{"resumed":true,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"server.name"}},"duration":0.000027586,"status":404,"err_id":"gyp8k3bjm","err_trace":"caddy-webhook.(*WebHook).ServeHTTP (webhook.go:221)"}
Here is the config i'm using for the webhook endpoint:
{ "match": [ { "host": ["*hostname"], "path": ["/webhook"] } ], "handle": [ { "handler": "webhook", "repo": "[email protected]:user/repo", "path": "/home/user/caddy_webhooks", "branch": "master", "type": "gitlab", "secret": "{env.WEBHOOK_SECRET}", "command": ["ls"] } ], "terminal": true }

Looking in the source code, it looks like Webhook.setup is never set to true, and I looked around a bit and it seemed like it may because it's only setup to read from a caddy file not a json file.

按照介绍的配置 route /webhook 一直报错404,日志中输出 ERROR http.handlers.webhook repository not setup

Caddyfile

:80 {
  encode zstd gzip

  log {
    output file /var/log/access.log
  }

  root * /data/blog
  file_server

  handle_errors {
    @404 {
        expression {http.error.status_code} == 404
    }
    handle @404 {
        rewrite * /404.html
        file_server
    }
  }

  route /webhook {
    webhook {
      repo "https://github.com/c588/hugoblog.git"
      branch main
      path /data/blog
      secret ******
    }
  }
}

但是使用 GitHub webhook 都是404
image
最后想着看一下 caddy2 的 route 是怎么使用的,就多加了一段

route /route_test {
  redir https://baidu.com
}

结果发现可以正常使用了
image
但是我看 caddy-webhook 的介绍和另外一位朋友提的 issue 中的配置都是直接写的 route /webhook 并且可以正常使用,所以来问一下,希望作者大大可以解答一下我的迷惑,感谢 :D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.