前端项目自动打包,自动部署,nginx提前配置,目录对应好
//deploy.js 放置项目根目录
const { NodeSSH } = require('node-ssh');
const scpClient = require('scp2');
const path = require('path');
const ssh = new NodeSSH();
const config = {
host: 'xxxx',
port: 22,
pathUrl: '/home',
userName: 'root',
password: 'xxxx'
};
const connectServer = function () {
ssh.connect({
host: config.host,
username: config.userName,
port: config.port,
password: config.password,
}).then(() => {
console.log('SSH login success, 服务器连接成功!');
ssh.execCommand('sh upgrade.sh', { cwd: config.pathUrl }).then((result) => {
// console.log(`The update message is: ${result.stderr}`);
console.log('%j', result);
if (!result.stderr) {
console.log('sh upgrade.sh 脚本执行完成!');
syncUpload()
} else {
console.log('Something wrong:', result);
}
})
})
}
const syncUpload = function () {
console.log('进入了',);
scpClient.scp(
'./dist',
{
host: config.host,
port: config.port,
username: config.userName,
password: config.password,
path: config.pathUrl+'/dist'
},
function (err) {
if (err) {
console.log('发布失败');
// throw err;
} else {
console.log('Success! 成功发布到' + config.host + '服务器! ');
}
},
function (aaa) {
console.log('aaa', aaa);
}
);
}
process.on('unhandledRejection', (reason, p) => {
console.log('Promise: ', p, 'Reason: ', reason)
// do something
})
connectServer()
//upgrade.sh 放置代码部署的目录下
rm -rf dist
rm -rf dist.zip
mkdir dist
//项目打包命令修改
xxxxx bulid && node ./deploy