github的webhook是个有用的功能,允许开发人员指定一个服务器的url。当开发者对github仓库施加操作,比如提交代码,创建issue时,github网站会自动向该url指定的服务器推送事件。借助webhook,我们可以实现很多自动化流程。比如部署一个应用在AWS上,本地提交代码后,github网站自动触发webhook,调用AWS上应用的逻辑,在AWS上将本地提交的最新代码用git pull抓取到AWS上并重新部署。

下面我们通过一个具体的例子来学习github webhook的用法。

新建一个github仓库,点击Settings链接:

在Payload url里指定一个应用的url,该url对应的应用监听github网站推送的事件。

Content Type指定成application/json便于我们在nodejs应用里解析payload。

创建后点Add webhook保存,github会发送一个json paload到这个url指定的应用上。

在Recent Deliveries里查看负载明细:

负载明细如下:

我们现在来做个实验,把webhook指定的url对应的应用设置一个断点,然后在github仓库里新建一个issue:

断点立即触发了。

从调试器里能观察到这个create issue事件的所有负载。

我部署在AWS上监听github webhook框架推送github repository发生变化的事件的应用源代码,可以从我的github上获取:

https://github.com/i042416/webhookstudy

代码很短,我的同事Haytham写的:


var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler({ path: '/push', secret: 'dis-koi' }) function run_cmd(cmd, args, callback) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = ""; child.stdout.on('data', function(buffer) { resp += buffer.toString(); });
child.stdout.on('end', function() { callback (resp) });
} http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(8083) handler.on('error', function (err) {
console.error('Error:', err.message);
}) handler.on('push', function (event) {
switch(event.payload.repository.name)
{
case 'githubHook':
//this push event is from my persional github account, as SAP github.tool's github hook do not work, so I use this one to test push event
console.log("reveive a push event from githubHook");
run_cmd('sh', ['./webshop.sh'], function(text){ console.log(text) });
break;
case 'frontend-web':
//push event from frontend-web
console.log("reveive a push event from frontend-web");
run_cmd('sh', ['./webshop.sh'], function(text){ console.log(text) });
break;
case 'backend-ms':
//push event from backenf-ms
console.log("reveive a push event from backend-ms");
run_cmd('sh', ['./backend_ms.sh'], function(text){ console.log(text) });
break;
}
}) handler.on('issues', function (event) {
console.log('Received an issue event for %s action=%s: #%d %s',
event.payload.repository.name,
event.payload.action,
event.payload.issue.number,
event.payload.issue.title);
})

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

最新文章

  1. 虚拟机下Ubuntu没有GUI图形界面,解决方法
  2. Subgraph Search Over Large Graph Database
  3. LU分解和求解线性方程组
  4. Gradle实战:不同编译类型的包同设备共存
  5. BZOJ 1560 火星藏宝图(DP)
  6. html表格单元格设置背景颜色
  7. 计算机网络 NAT
  8. wordpress建站过程1
  9. Linux 系统分区
  10. 2017Unity开发者大会备受关注的原因有哪些?
  11. trunk端口配置错误导致环路
  12. 清除UIWebView缓存
  13. Array数组内函数
  14. SpringBatch Sample (四)(固定长格式文件读写)
  15. B/S和C/S结构的区别
  16. 在postgresqlz中查看与删除索引
  17. C# 计算传入的时间距离今天的时间差
  18. [UE4]蓝图重构
  19. AngularJs 指令实现选项卡
  20. DELL服务器PXE前期处理

热门文章

  1. 用Keras搭建神经网络 简单模版(四)—— RNN Classifier 循环神经网络(手写数字图片识别)
  2. PowerDesigner设置code和name不联动的方法
  3. [C++]数据结构:栈之顺序栈
  4. iOS Xib布局某些控件显示或隐藏<约束的修改>
  5. nginx反向代理本地 单台wed -使用域名代理
  6. Data - 大数据生态圈
  7. Nginx-windows
  8. Xshell终端连接CentOS7.0下Docker容器中的MySql镜像后无法键入中文问题
  9. 蓝牙AT模式
  10. redis-5.0.3 redis.conf详解