Authored by 周奇琪

init project~

  1 +node_modules/
  1 +# fe-git-ci
  2 +
  3 +一个用于git代码更新,执行持续集成命令的项目
  1 +var express = require('express');
  2 +var path = require('path');
  3 +var favicon = require('serve-favicon');
  4 +var logger = require('morgan');
  5 +var bodyParser = require('body-parser');
  6 +
  7 +var routes = require('./routes/index');
  8 +
  9 +var app = express();
  10 +
  11 +app.use(logger('dev'));
  12 +app.use(bodyParser.json());
  13 +app.use(bodyParser.urlencoded({ extended: false }));
  14 +app.use(express.static(path.join(__dirname, 'public')));
  15 +
  16 +app.use('/', routes);
  17 +
  18 +app.use(function(req, res, next) {
  19 + res.status(404);
  20 + res.json({
  21 + message: 'not found!',
  22 + code : 404
  23 + });
  24 +});
  25 +
  26 +module.exports = app;
  1 +#!/usr/bin/env node
  2 +
  3 +/**
  4 + * Module dependencies.
  5 + */
  6 +
  7 +var app = require('../app');
  8 +var http = require('http');
  9 +
  10 +/**
  11 + * Get port from environment and store in Express.
  12 + */
  13 +
  14 +var port = normalizePort(process.env.PORT || '3000');
  15 +app.set('port', port);
  16 +
  17 +/**
  18 + * Create HTTP server.
  19 + */
  20 +
  21 +var server = http.createServer(app);
  22 +
  23 +/**
  24 + * Listen on provided port, on all network interfaces.
  25 + */
  26 +
  27 +server.listen(port);
  28 +server.on('error', onError);
  29 +server.on('listening', onListening);
  30 +
  31 +/**
  32 + * Normalize a port into a number, string, or false.
  33 + */
  34 +
  35 +function normalizePort(val) {
  36 + var port = parseInt(val, 10);
  37 +
  38 + if (isNaN(port)) {
  39 + // named pipe
  40 + return val;
  41 + }
  42 +
  43 + if (port >= 0) {
  44 + // port number
  45 + return port;
  46 + }
  47 +
  48 + return false;
  49 +}
  50 +
  51 +/**
  52 + * Event listener for HTTP server "error" event.
  53 + */
  54 +
  55 +function onError(error) {
  56 + if (error.syscall !== 'listen') {
  57 + throw error;
  58 + }
  59 +
  60 + var bind = typeof port === 'string'
  61 + ? 'Pipe ' + port
  62 + : 'Port ' + port;
  63 +
  64 + // handle specific listen errors with friendly messages
  65 + switch (error.code) {
  66 + case 'EACCES':
  67 + console.error(bind + ' requires elevated privileges');
  68 + process.exit(1);
  69 + break;
  70 + case 'EADDRINUSE':
  71 + console.error(bind + ' is already in use');
  72 + process.exit(1);
  73 + break;
  74 + default:
  75 + throw error;
  76 + }
  77 +}
  78 +
  79 +/**
  80 + * Event listener for HTTP server "listening" event.
  81 + */
  82 +
  83 +function onListening() {
  84 + var addr = server.address();
  85 + var bind = typeof addr === 'string'
  86 + ? 'pipe ' + addr
  87 + : 'port ' + addr.port;
  88 + console.log('Listening on ' + bind);
  89 +}
  1 +{
  2 + "name": "fe-git-ci",
  3 + "version": "0.0.0",
  4 + "private": true,
  5 + "scripts": {
  6 + "start": "node ./bin/www"
  7 + },
  8 + "dependencies": {
  9 + "body-parser": "~1.13.2",
  10 + "express": "~4.13.1",
  11 + "morgan": "~1.6.1",
  12 + "serve-favicon": "~2.3.0"
  13 + }
  14 +}
  1 +var express = require('express');
  2 +var router = express.Router();
  3 +var exec = require('child_process').exec;
  4 +
  5 +var projects = {};
  6 +
  7 +router.post('/:name', function(req, res, next) {
  8 + proc(req,res);
  9 +});
  10 +
  11 +
  12 +/**
  13 + * 处理刷新和重启服务
  14 + * @param {String} name 服务目录
  15 + * @param {Object} req 请求
  16 + * @param {Object} res 响应
  17 + */
  18 +function proc(req,res) {
  19 + var cliArr = [
  20 + "cd /Data/code/test/"+req.params.name+"/client/",
  21 + "git pull",
  22 + "gulp",
  23 + "cd /Data/code/test/"+req.params.name+"/",
  24 + "make start"
  25 + ];
  26 +
  27 + console.log(req.body);
  28 +
  29 + if(req.body.ref.indexOf('release')<0) {
  30 + res.json({"code":200,"message":"not release,do nothing!"});
  31 + return;
  32 + }
  33 + if(projects[req.params.name].hash === req.body.after) {
  34 + res.json({"code":200,"message":"same commits , do nothing!"});
  35 + return;
  36 + }
  37 +
  38 + //设置当前项目的commits的hash
  39 + projects[req.params.name].hash = req.body.after;
  40 +
  41 + //执行命令
  42 + exec(cliArr.join(' && '),function(err,out,outerr) {
  43 + if(err) {
  44 + console.log(err);
  45 + console.log(outerr);
  46 + res.json({"code":500,"message": err.message});
  47 + } else {
  48 + console.log(out);
  49 + res.json({"code":200,"message":"done!"});
  50 + }
  51 + });
  52 +}
  53 +
  54 +module.exports = router;