将本地包上传到远程服务器方法探讨
Aug 3, 2015
在制作preview插件的过程中,由于涉及到将本地环境部署到gaea主机,因此产生一个问题是通过什么方式将环境部署上去。
目前想到的有四种方式:
- ssh登陆gaea,然后通过git从gitlab上拉下来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27var Client = require('ssh2').Client;
sshCommand(host, user, pwd, "git clone xxx&&git checkout " + branchName + "&& git pull");
function sshCommand(host, user, pwd, cmd) {
var conn = new Client();
var conn = new Client();
conn.on('ready', function() {
console.log('登陆ssh');
conn.exec(cmd, function(err, stream) {
if (err) throw err;
console.log(cmd);
stream.on('close', function() {
console.log('退出ssh');
conn.end();
}).on('data', function(data) {
console.log(data.toString());
}).stderr.on('data', function(data) {
console.log('抛出异常: ' + data);
});
});
}).connect({
host: host,
username: user,
password: pwd
});
}
通过child_process运行scp命令从本地复制到gaea主机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20var child_process = require('child_process');
var child = child_process.exec('scp /Users/Virgil/Downloads/server.js virgil@106.186.127.226:/home/virgil/server.js');
child.stdin.on('data', function(data) {
console.log('success');
});
child.stdout.on("data", function(data) {
console.log("stdout:" + data);
});
child.stderr.on("data", function(data) {
console.log("stderr:" + data);
});
child.on("close", function(code) {
if (code === 0) {
console.log("成功退出");
} else {
console.log("异常退出")
}
process.exit();
});通过node自身的stream传输过去
通过http协议post方法传输过去