此文,只用于本人实际操作中遇到的问题,做一个大致统计,具体详情可以查看原文信息
查看原文信息1
查看原文信息2
标题如何配置
找到你在Github上的项目地址上的Setting的Webhooks
项目配置
- 编写执行shell命令
在项目根目录下新建deployed.sh文件,输入你想在服务器上执行的命令行,如:
cd /my/nginx-project/vant-study
git pull
npm install
npm run build
nginx -s reload
- 编写执行脚本
在项目根目录下新建deployed.js文件
var http = require('http')
var spawn = require('child_process').spawn
var createHandler = require('github-webhook-handler')
var handler = createHandler({
path: '/pushCode',
secret: '12345678'
})
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404;
res.end('no such location')
})
}).listen(3000)
handler.on('error', function (err) {
console.error('Error:', err.message)
})
handler.on('push', function (event) {
console.log('Received a push event for %s to %s',
event.payload.repository.name,
event.payload.ref)
rumCommand('sh', ['./deployed.sh'], function (txt) {
console.log(txt)
})
})
function rumCommand(cmd, args, callback) {
var child = spawn(cmd, args)
var response = ''
child.stdout.on('data', function (buffer) {
response += buffer.toString()
})
child.stdout.on('end', function () {
callback(response)
})
}
nginx配置
执行脚本跑在了3000端口,我们服务器对应启用到 3000 端口,如果地址不同,可能需要在服务器添加放行端口
upstream test {
server 127.0.0.1:3000;
}
server {
location /pushCode {
proxy_pass http://test;
proxy_redirect off;
}
}
添加webhook访问地址:
http://「你的域名,如上nginx的配置,此处可以不加端口」/pushCode
http://xxx.xxx.xxx/pushCode
启动项目
node deployed.js