专业编程基础技术教程

网站首页 > 基础教程 正文

github webhook自动化部署

ccvgpt 2024-11-25 10:14:27 基础教程 1 ℃

服务端nodejs监控

secret在github上设置的,path为访问路径:如在github上设置Payload Url :xxxx/webhook

监控

首先下载cnpm i github-webhook-handler

github webhook自动化部署

var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler({ path: '/webhook', secret: '123456' })

function RunCmd (cmd, args, cb) {
  var spawn = require('child_process').spawn;
  var child = spawn(cmd, args);
  var result = '';
  child.stdout.on('data', function (data) {
    result += data.toString();
  });
  child.stdout.on('end', function () {
    cb(result)
  });
}

http.createServer(function (req, res) {
  handler(req, res, function (err) {
    res.statusCode = 404;
    res.end('no such location');
  })
}).listen(8098)

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);
//监听来自deploy分支,然后部署
  if (event.payload.ref.includes('deploy')) {
    var shpath = './deploy.sh';//下方代码
    RunCmd('sh', [shpath], function (result) {
      console.log(result);
    })
  }
})

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);
})

拉取代码deploy.sh

拉取之前,在github=>settings=>Deploy key设置好秘钥

提示:最好使用ssh拉取代码,否则会出现其他问题

#! /bin/bash
echo '开始执行'
cd Airport-Management-System
# git init
# git remote add origin 仓库地址
git fetch
echo '切换分支'
git checkout deploy
## 拉取最新代码
echo '开始拉去最新代码'
git pull origin deploy
cp -rf /usr/local/auto-build/Airport-Management-System/dist/* /usr/local/nginx/airport
echo '拷贝完成'



Tags:

最近发表
标签列表