git使用WebHook实现自动构建
2024-10-21 22:57:59
说明
我们使用git进行版本管理常常会遇到这样的一个需求,希望git push的时候服务器上代码的代码也能自动更新,这次我使用了coding进行示范
一、编写git pull 更新脚本 auth_pull.sh
vim auth_pull.sh
#!/bin/sh
unset GIT_DIR
Path="{自己的项目跟目录}"
cd $Path
git pull origin master
exit 0
对auth_pull.sh进行授权
chmod a+x auth_pull.sh
二、编写auto_pull.js文件
这里为了方便只使用了nodejs,你也可以使用php python,原理都是一样,githua、coding上都会有一个webhook的功能,如果git有push 或者其它事件发生就会向你设置webhook发起一个请求,这里我简单的拿了个coding的请求代码
请求头:
Request URL: http://hook.chainhots.com/pushRequest
method: POST
User-Agent: Coding.net Hook
X-Coding-Event: push
X-Coding-Delivery: 7bf60799-a9ac-4ae1-b496-dd47b1bb5537
X-Coding-WebHook-Version: v2
X-Coding-Signature: sha1=ca375797efab8acea2f59db51ade3e99ef97c45f
请求内容
...
这里就不多写了,想知道可以自己去看看,吧请求的数据写到日志中就能看到了。
auto_pull.js :
var http = require('http');
var crypto = require('crypto')
var exec = require('child_process').exec;
// 在Webhook中设定的secret
var secret = ''
// 在Webhook中设定的Payload URL
var url = ''
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type':'application/json'});
response.end();
if (request.headers['x-coding-event'] && request.headers['x-coding-event'] === 'push') {
console.log('push');
request.on('data', function(chunk) {
//这里可以写自己验证逻辑
//var Signature = request.headers['x-coding-signature'];
//console.log(chunk.toString()); chunk中存储了payload的数据,如果需要可以拿出来做更精确的处理.比如部署触发该次push的commit的代码
//if (verifySecret(Signature, sign(secret, chunk.toString())) && verifyUrl(url, request.url)) {
console.log('verify');
runCommand();
//} else {
// console.log('verify faild');
//}
});
}
}).listen(8083, '127.0.0.1'); // 对,服务监听的是内网地址.用Nginx反代一下就好.(当然直接丢到外网也没问题)
function sign(secret, data) {
return 'sha1=' + crypto.createHmac('sha1', secret).update(data).digest('hex');
}
function verifySecret(data0, data1) {
return (data0 == data1);
}
function verifyUrl(data0, data1) {
return (data0 == data1);
}
function runCommand() {
exec("./auto_pull.sh", function(err,stdout,stderr){
if(err) {
console.log('error:'+stderr);
} else {
console.log("stdout:"+stdout);
}
});
}
运行node auto_pull.js
三、配置守护进程
这里守护进程使用 Supervisor 来实现
centos 7 直接运行命令
yum install supervisor
一路回车下来就行,安装完成之后启动supervisor
systemctl start supervisord
设置开机自动启动
systemctl enable supervisord
接写来编写auto_pull 的守护进程配置 ,切换到 cd /etc/supervisord.d目录下,创建auto_pull.ini文件
[program:auto_pull] #应用程序名称
command=nohup node /home/githook/auto_pull.js > /home/githook/consloe.log 2>&1 & #配置后台运行并讲输出日志写入到文件中
directory=
user=root #运行用户
stopsignal=INT
autostart=true #是否自动启动
autorestart=true #是否自动重启
startsecs=3 #重启间隔时间
stderr_logfile=/var/log/autopull.err.log #运行错误日志
stdout_logfile=/var/log/autopull.out.log #运行日志
配置完成,运行
systemctl restart supervisord
重启就OKle
最新文章
- 关于SubSonic3.0插件使用实体进行更新操作时(执行T.Update()或T.Save()),某些列无法进行修改操作的问题处理
- 让Redis在你的系统中发挥更大作用的几点建议
- 字符串-Alphabet
- 让CKEditor支持FLV视频播放
- 无法将Win7安装到GPT分区下解决办法
- 内嵌页js与ios和安卓的交互
- CodeIgniter 定义“全局变量-global variable”,可以在所有controller,model和view中使用
- js中获取项目路径的小插件
- 链接服务器";(null)";的 OLE DB 访问接口 ";Microsoft.Jet.OLEDB.4.0"; 返回了消息 ";未指定的错误";。[手稿]
- android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
- nuc970连接jlink进行单步调试的设置
- tomcat 启动超时
- Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer
- HDU-1996-汉诺塔VI
- 初识vw和vh
- JS取出两个数组中的不同或相同元素
- .Net Core(二)EFCore
- B+树和LSM比较(转)
- Java I/O 应用程序设计
- Laravel基本使用
热门文章
- android_MultiAutoCompleteTextView
- IQueryable.Where中动态生成多个并或筛选Expression<;Func<;T, bool>;>;
- jmeter_遍历转换浮点时间戳
- 【Android UI】侧滑栏的使用(HorizontalScrollView控件的使用)
- Java--ASCII码
- CentOS虚拟机查询jdk路径
- linux初学者-firewall篇
- linux初学者-sshd服务
- Python 之父撰文回忆:为什么要创造 pgen 解析器?
- Python(简单图形和文件处理)编程