1 Star 15 Fork 6

GLUESTICK / auto-deployment

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
index.js 4.73 KB
一键复制 编辑 原始数据 按行查看 历史
GLUESTICK 提交于 2020-08-12 15:09 . #I1QNWV
'use strict';
const http = require('http');
const exec = require('child_process').exec;
const execFile = require('child_process').execFile;
const os = require('os');
const iconv = require('iconv-lite');
const buffer = require('buffer');
const system = os.platform();
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
let binaryEncoding = 'binary';
let encoding = 'cp936';
let options = {
encoding: binaryEncoding,
windowsHide:false,
};
function func(){}
func.prototype = {
execute:function(cmd,callback){
exec(cmd,options, function(error, stdout, stderr) {
if(error){
console.log(cmd+ ' fail');
if(callback && typeof callback === 'function') {
let result = iconv.decode(Buffer.from(stderr,binaryEncoding), encoding);
callback(true,result);
}
} else{
console.log(cmd+ ' success');
if(callback && typeof callback === 'function') {
console.log('cmd:'+cmd);
let result = iconv.decode(Buffer.from(stdout,binaryEncoding), encoding);
callback(true,result);
}
}
});
return this;
}
};
function run() { return new func(); }
let init = function(option){
http.createServer(function(req, res){
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('X-Foo', 'bar');
console.log(req.method + ':' + req.url);
if(req.headers['x-gitee-token']===option.acceptToken && req.method===option.method && req.url === option.url && req.headers['user-agent']==='git-oschina-hook') {
// 验证成功
// 判断执行类型 file or command 默认:command
console.log('async:'+option.async);
if(option.type!=='file') {
let resText = '';
let loop = function loop(i){
run().execute(option.cmd[i],(success,result)=>{
resText+=result;
if(i<option.cmd.length-1) {
if(success===true) {
i++;
loop(i);
} else {
if(option.async===false) {
console.log('fail');
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end(resText);
}
return false;
}
} else {
if(option.async===false) {
console.log('All success');
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end(resText);
return false;
}
}
});
};
if(option.async===true) {
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('async success');
loop(0);
} else {
loop(0);
}
} else {
let bash = option.executeFile;
if(option.async===true) {
execFile(bash);
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
res.end('async success');
} else {
execFile(bash,options,(err, stdout, stderr)=>{
if(!stderr) {
console.log('All success');
res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
let result = iconv.decode(Buffer.from(stdout,binaryEncoding), encoding);
res.end(result);
} else {
console.log('fail');
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
let result = iconv.decode(Buffer.from(stderr,binaryEncoding), encoding);
res.end(result);
}
return false;
});
}
}
} else {
res.writeHead(402, { 'Content-Type': 'text/plain' });
res.end('fail');
}
}).listen(option.port);
console.log('自动部署服务启动于:' + system + '操作系统,端口号:' + option.port);
};
module.exports = init;
NodeJS
1
https://gitee.com/GLUESTICK/auto-deployment.git
git@gitee.com:GLUESTICK/auto-deployment.git
GLUESTICK
auto-deployment
auto-deployment
master

搜索帮助