webhook-github

此文,只用于本人实际操作中遇到的问题,做一个大致统计,具体详情可以查看原文信息
查看原文信息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

pm2,不做解释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值