webpack 一键发布到线上

需要安装一下插件

  • scp2
  • chalk
  • ssh2

根目录创建deploy文件夹

  deploy
    - config.js
    - env.js
    - test.js
    - production.js

config.js 代码

module.exports = (server) => {
  const scpClient = require('scp2');
  const ora = require('ora');
  const spinner = ora('正在发布到' + server.name + '服务器...');
  var Client = require('ssh2').Client;
  const chalk = require('chalk');
  function getNowFormatDate() {
    var date = new Date();
    var seperator1 = '-';
    var seperator2 = ':';
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = '0' + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = '0' + strDate;
    }
    var currentdate =
      date.getFullYear() +
      seperator1 +
      month +
      seperator1 +
      strDate +
      ' ' +
      date.getHours() +
      seperator2 +
      date.getMinutes() +
      seperator2 +
      date.getSeconds();
    return currentdate;
  }
  var conn = new Client();
  conn
    .on('ready', function () {
      // rm 删除dist文件,\n 是换行 换行执行 重启nginx命令 我这里是用docker重启nginx
      conn.exec(
        'rm -rf ' + server.path + '\ndocker restart nginx',
        function (
          // conn.exec('rm -rf ' + server.path + '\ndocker restart nginx', function (
          err,
          stream
        ) {
          if (err) throw err;
          stream
            .on('close', function (code, signal) {
              // 在执行shell命令后,把开始上传部署项目代码放到这里面
              spinner.start();
              scpClient.scp(
                './dist',
                {
                  host: server.host,
                  port: server.port,
                  username: server.username,
                  password: server.password,
                  path: server.path,
                },
                function (err) {
                  spinner.stop();
                  if (err) {
                    // console.log(chalk.red('发布失败.\n'));
                    console.log('发布失败.\n');

                    throw err;
                  } else {
                    console.log(
                      getNowFormatDate() +
                        '\nSuccess! 成功发布到' +
                        server.name +
                        '服务器! \n'

                    console.log('测试地址:' + chalk.green(server.domain));
                    );
                  }
                }
              );

              conn.end();
            })
            .on('data', function (data) {
              console.log('STDOUT: ' + data);
            })
            .stderr.on('data', function (data) {
              console.log('STDERR: ' + data);
            });
        }
      );
    })
    .connect({
      host: server.host,
      port: server.port,
      username: server.username,
      password: server.password,
      // privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa')
    });
};

env.js

const env = {
  production: {
    id: 0,
    name: 'A-生产环境',
    domain: '', // 域名
    host: '', // ip
    port: , // 端口
    username: '', // 登录服务器的账号
    password: '', // 登录服务器的账号
    path: ''// 发布至静态服务器的项目路径
  },
  test: { // 测试服
    id: 0,
    name: 'A-测试环境',
    domain: '', // 域名
    host: '', // ip
    port:  // 端口
    username: 'root', // 登录服务器的账号
    password: '', // 登录服务器的账号
    path: ''
  }
}

module.exports = env

test.js

  const config = require('./config')
  const env = require('./env')
  config(env.test)

production.js

const config = require('./config')
const env = require('./env')
config(env.production)

运行命令

"build:test": "vue-cli-service build && node ./deploy/test.js",

你可能感兴趣的:(webpack 一键发布到线上)