Authored by 周奇琪

init project~

node_modules/
... ...
# fe-git-ci
一个用于git代码更新,执行持续集成命令的项目
\ No newline at end of file
... ...
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use(function(req, res, next) {
res.status(404);
res.json({
message: 'not found!',
code : 404
});
});
module.exports = app;
... ...
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}
... ...
{
"name": "fe-git-ci",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.13.2",
"express": "~4.13.1",
"morgan": "~1.6.1",
"serve-favicon": "~2.3.0"
}
}
\ No newline at end of file
... ...
var express = require('express');
var router = express.Router();
var exec = require('child_process').exec;
var projects = {};
router.post('/:name', function(req, res, next) {
proc(req,res);
});
/**
* 处理刷新和重启服务
* @param {String} name 服务目录
* @param {Object} req 请求
* @param {Object} res 响应
*/
function proc(req,res) {
var cliArr = [
"cd /Data/code/test/"+req.params.name+"/client/",
"git pull",
"gulp",
"cd /Data/code/test/"+req.params.name+"/",
"make start"
];
console.log(req.body);
if(req.body.ref.indexOf('release')<0) {
res.json({"code":200,"message":"not release,do nothing!"});
return;
}
if(projects[req.params.name].hash === req.body.after) {
res.json({"code":200,"message":"same commits , do nothing!"});
return;
}
//设置当前项目的commits的hash
projects[req.params.name].hash = req.body.after;
//执行命令
exec(cliArr.join(' && '),function(err,out,outerr) {
if(err) {
console.log(err);
console.log(outerr);
res.json({"code":500,"message": err.message});
} else {
console.log(out);
res.json({"code":200,"message":"done!"});
}
});
}
module.exports = router;
... ...