使用场景:配合Jenkins部署项目
git.js
/**
* server exec shell
*/
const http = require('http');
const url = require("url");
const PORT = 8888;
const projects = [
{
path: '/aaa',
key: 'pwd',
cmd: 'cd /Users/frank/workspace/emlog && git pull',
}
];
http.createServer(async function (request, response) {
const pathname = url.parse(request.url).pathname;
const parsedUrl = url.parse(request.url, true); // true to get query as object
const queryAsObject = parsedUrl.query;
const {execSync, exec, spawn } = require('child_process');
response.writeHead(200, {'Content-Type': 'text/plain'});
const options = {
encoding: 'utf8'
};
for(let p of projects){
if(p.path == pathname){
if(!queryAsObject || !queryAsObject.key || queryAsObject.key != p.key){
response.end('Unauthorization\n');
return;
}
let result = await execSync(p.cmd, options);
console.log(result);
response.end(result);
}
}
}).listen(PORT);
console.log('Server running at http://127.0.0.1:' + PORT);
发表评论 登录: