您当前的位置:主页 > 技术探讨 >

    vue 自动部署打包脚本 ssh2-sftp-client

    时间:2021-07-24 10:28 日记人:arlen.zhou

    打包提交是项目常用的 工作流程 目前市面上大公司 基本是有专门的运维处理,没有运维就得什么都自己干了,本次用的工具   ssh2-sftp-client

    话不多说,狠人开干~~~


    1.、首先在 src 同目录下创建配置执行文件 upload  , config.js   , index.js





    2、config.js 主要处理服务相关配置


    module.exports = {
        ip: "192.168.0.88"// ssh地址
        username: "root"// ssh 用户名
        port:"22",      //端口
        password: "ymf"// ssh 密码
        path: '/opt/supermarket/web/wuhou-admin'// 操作开始文件夹 可以直接指向配置好的地址
        rmpath: '/opt/supermarket/web/wuhou-admin' // 需要删除的文件夹
      }


    3、index.js  主要就是链接服务器后操作服务
    const config = require('./config.js')
    const shell = require('shelljs')
    const path = require('path');
    let Client = require('ssh2-sftp-client');
    // 打包 npm run build
    const compileDist = async () => {
      if(shell.exec(`npm run build`).code==0) {
        console.log("打包成功")
      }
    }

    async function connectSSh() {
      let sftp = new Client();
      sftp.connect({
        host: config.ip// 服务器 IP
        port: config.port,
        username: config.username,
        password: config.password
      }).then(() => {
        console.log("删除完成");
        return sftp.rmdir(config.rmpathtrue);
      }).then(() => {
        // 上传文件
        console.log("开始上传")
        return sftp.uploadDir(path.resolve(__dirname'../dist'), config.path);
      }).then((data=> {
        console.log("上传完成");
        sftp.end();
      }).catch((err=> {
        console.log(err'失败');
        sftp.end();
      });
    }
    async function runTask() {
      await compileDist()     //打包完成
      await connectSSh()      //提交上传
    }
    runTask()

     

    4、然后需要在  package.json  文件添加启动执行指令


    "upload": "node upload/index.js"

    5、npm run upload  直接执行,就可以成功替换服务器上的文件目录了。。。。。

    自动部署其实大可不必运维!!!嘎嘎 他们要失业了~