1、添加命令、依赖
?
1 2 |
|
“shelljs”: “^0.8.5”,
“scp2”: “^0.5.0”
2、添加环境变量
# 自动部署
VUE_APP_SERVER_ID = 0
3、添加deploy文件夹
?
1 |
|
require('shelljs/global');
const inquirer = require('inquirer');
// 发布的服务器列表
const ser = require('./servers');
const SERVER_LIST = ser.servers;
inquirer.prompt([{
name: 'conform',
message: '是否需要重新构建?',
type: 'list',
default: 0,
choices: [{name: '是',value: 1}, {name: '否',value: 0}]
},{
name: 'choose',
message: '选择发布的环境',
type: 'list',
default: 0,
choices: SERVER_LIST
}]).then(function (answers) {
process.env.VUE_APP_SERVER_ID = answers.choose;
if(answers.choose === 99) {
console.log('取消构建');
return;
}
let cmd = answers.conform ? 'npm run build && node ./deploy' : 'node ./deploy';
exec(cmd);
});
?
1 |
|
require('shelljs/global');
const ora = require('ora');
const chalk = require('chalk');
const scpClient = require('scp2');
// 发布的服务器列表
const ser = require('./servers');
const serverInfo = ser.servers[process.env.VUE_APP_SERVER_ID || 0];
if(!serverInfo) {
return;
}
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function () {
console.log('Client :: ready');
conn.exec('rm -rf ' + serverInfo.path, function (err,stream) {
if (err) throw err;
stream
.on('close', function (code, signal) {
// 在执行shell命令后,把开始上传部署项目代码放到这里面
const spinner = ora('准备发布到服务器' + serverInfo.name);
spinner.start();
console.log(chalk.green('开始发布
'));
scpClient.scp(
'dist/',
{
host: serverInfo.host,
port: serverInfo.port,
username: serverInfo.username,
password: serverInfo.password,
path: serverInfo.path
},
function (err) {
spinner.stop();
if (err) {
console.log(chalk.red('发布失败.
'));
throw err;
} else {
console.log(chalk.green('Success! 成功发布到' + serverInfo.name + '服务器!
'));
}
}
);
conn.end();
})
.on('data', function (data) {
console.log('STDOUT: ' + data);
})
.stderr.on('data', function (data) {
console.log('STDERR: ' + data);
});
});
})
.on('error', (err) => {
console.log(err);
})
.on('end', () => {
console.log('end');
})
.on('close', () => {
console.log('close');
})
.connect({
host: serverInfo.host,
port: serverInfo.port,
username: serverInfo.username,
password: serverInfo.password,
readyTimeout: 5000
});
?
1 |
|
/*
* name:自定义名称
* domain:域名
* host:ip
* port:端口
* username:服务器账号
* password:服务器密码
* path:发布到服务器的指定路径
*/
const servers = [
{
value: 0,
name: '测试环境41',
domain: '172.19.2.47',
host: '172.19.2.47',
port: '22',
username: 'root',
password: '002@121dsj',
path: '/home/web/rk-unified-portal/dist/'
}
];
module.exports = {
servers: servers
};
翻译
搜索
复制