Any Git push to a Repository, including editing tags or branches. Commits via API actions that update references are also counted. This is the default event.
| Key | Value |
| --- | --- |
| zen | Random string of GitHub zen |
| hook_id | The ID of the webhook that triggered the ping |
| hook | The [webhook configuration](https://developer.github.com/v3/repos/hooks/#get-single-hook) |
服务器:
Jenkins Server
Git Server
App Server
关键词:nodejs
ngrok
github webhook
在配置Jenkins实现前端自动化构建的过程中,对于自动化的过程理解很模糊,只是知道Jenkins服务器,Git仓库和后端应用服务器这三个概念。
答案就是:Git webhook机制发出请求,告知Jenkins服务器你要自动构建了。
webhook与异步编程中"订阅-发布模型"非常类似,一端触发事件,一端监听执行。
如果仅仅想学习webhook,可以直接阅读。但是一定要注意,知识点是"异步编程模型",webhook仅仅是它的一个实现。
开启正文!
维基百科:
一、基础部分
Webhook 允许我们通过在Github.com订阅事件后构建后或者安装Github应用。当其中之一的事件被触发时,我们可以发送HTTP POST请求到webhook的配置URL。Webhook可以用作升级一个issue追踪,触发CI构建,升级一个后端镜像,或者甚至是部署你的生产服务器。只有想不到,没有做不到。
事件
在配置webhook的时候,你可以选择自己想要接收的事件。你甚至可以选择参加触发所有事件。只有订阅特殊的需要的事件,可以有效限制服务器HTTP请求数。可以通过API或者UR随时订阅事件。默认情况下,webhook只订阅push事件。
每个事件与一个动作集合联系,这些动作可以在你的组织或者repo中发生。例如,如果你订阅了issues事件,你将在issue open,close以及labeled时接收到detailed payload。
*
check_run
check_suite
commit_comment
push
......
载荷
每一个事件类型都有一个指定的与相关事件信息有关的payload格式。所有的事件载荷都是事件类型的载荷镜像,push除外,因为他有更加详细的webhook负载。
除了每个事件的documented字段,webhook负载包含了执行事件的用户以及组织和或repo,对于一个Github App的webhook来说,它包含installation。在PullRequestEvent payload中有示例。
发送报文头
X-GitHub-Event
X-GitHub-Delivery
X-Hub-Signature
User-Agent也将会加上前缀GitHub-Hookshot/. 示例:
Ping 事件 ping事件负载
GitHub App 当你注册一个新的GitHub App时,GitHub发送一个ping事件到webhook URL。事件的包含app_id。
二、实验开始
1. 配置并创建Webhook
2. 配置接收和管理负载的服务器
将生成的只有8小时有效时间的url复制到Payload URL。
var app = new Koa(); var router = new Router();
router .post('/payload', (ctx, next) => { console.log(ctx); })
app .use(router.routes()) .use(router.allowedMethods());
app.listen(4567);
node server.js
{ request: { method: 'POST', url: '/payload', header: { host: '785902b9.ngrok.io', accept: '/', 'user-agent': 'GitHub-Hookshot/22e0d92', 'x-github-event': 'issues', 'x-github-delivery': 'cde0a020-7c48-11e8-9c35-c1d90e4891c8', 'content-type': 'application/x-www-form-urlencoded', 'x-hub-signature': 'sha1=9f4873803f9615615e02f7dec856778ebfc201be', 'content-length': '10929', 'x-forwarded-for': '192.30.252.44' } }, response: { status: 404, message: 'Not Found', header: {} }, app: { subdomainOffset: 2, proxy: false, env: 'development' }, originalUrl: '/payload', req: '',
res: '',
socket: '' }