Vue打包并自动部署到指定服务器

安装依赖

npm install scp2 --save-dev 
npm install chalk --save-dev 
npm install ssh2 --save-dev 
npm install cross-env --save-dev 

配置备用环境变量
在根目录下创建.env.development文件,并添加环境变量

VUE_APP_SERVER_ID = 0

在根目录下创建.env.production文件,并添加环境变量

VUE_APP_SERVER_ID = 1

创建自动化部署脚本
在package.json同级目录下创建文件夹deploy

在deploy文件下创建config.js和servers.js和index.js

servers.js:配置服务器相关信息

/*
* name:自定义名称
* domain:域名
* host:ip
* port:端口
* username:服务器账号
* password:服务器密码
* path:发布到服务器的指定路径
*/
const servers = [
    {
        value: 0,
        name: 'xxx',
        domain: 'xxx',
        host: 'xxx',
        port: 'xxx',
        username: 'xxx',
        password: 'xxx',
        path: 'xxxx'
      },
      {
        value: 1,
        name: 'xxx',
        domain: 'xxx',
        host: 'xxx',
        port: 'xxx',
        username: 'xxx',
        password: 'xxx',
        path: 'xxxx'
      }
]

module.exports = {
    servers:servers
}

config.js:发布前的一些处理

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);
})

index.js:发布到服务器的处理逻辑

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 || 2];
if (!serverInfo) {
  return
}

var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function () {
  console.log('已连接服务器:'+serverInfo.name+'\nhost:'+serverInfo.host+'|port:'+serverInfo.port);
  if(!serverInfo.path){
    console.log('连接已关闭');
    conn.end()
    return false;
  }
  conn.exec('rm -rf ' + serverInfo.path + '/*', function (err, stream) {
    if (err) throw err;
    console.log("已删除");
    stream
      .on('close', function (code, signal) {
        // 在执行shell命令后,把开始上传部署项目代码放到这里面
        console.log(chalk.green('开始发布\n'));
        const spinner = ora('正在发布到服务器...');
        spinner.start();
        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('发布失败.\n'));
              throw err;
            } else {
              console.log(chalk.green('Success! 成功发布到' + (process.env.NODE_ENV === 'prod' ? '生产' : '测试') + '服务器! \n'));
            }
            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
  });

添加package.json中的scripts命令

"scripts": {
    "deploy": "node ./deploy",
    "config": "node ./deploy/config.js"
}

效果如下


image.png

你可能感兴趣的:(Vue打包并自动部署到指定服务器)