var express = require('express'); var router = express.Router(); var moment = require('moment') const {spawn} = require('child_process'); const _info='info' const _error='error' function log(param,message){ console[param](`${moment().format('yyyy-MM-DD HH:mm:ss')}:${message}`) } function info(message){ log(_info,message) } function error(message){ log(_error,message) } //web钩子事件 //git@gogs.kirito.cool:panqihua/RiskCloudMock.git push事件 router.post('/deploy', function(req, res, next) { const updated_at=req.body.repository.updated_at const name=req.body.repository.name info(`开始自动部署${name}`) const promise = new Promise((resolve, reject) => { const spawnObj = spawn('updateRisk', [name], {encoding: 'utf-8'}); spawnObj.stdout.on('data', function(chunk) { info(chunk.toString()); }); spawnObj.stderr.on('data', (data) => { error(data); }); spawnObj.on('close', function(code) { info('close code : ' + code); }) spawnObj.on('exit', (code) => { info('exit code : ' + code); resolve(true) }) }) res.json({message:`仓库代码${req.body.repository.ssh_url}部署中`}) }); //部署完毕事件 router.get('/finsh', function(req, res, next) { info(`仓库代码${req.query.name}部署成功`) res.json({message:`仓库代码${req.query.name}部署成功`}) }); module.exports = router;