vue部署到服务器.sh文件,node.js脚本一键打包部署vue项目到liunx服务器

const { exec } = require("child_process")

const ssh2 = require("ssh2")

const compressing = require('compressing');

//打包

let build = exec("cd vuedemo && npm run build", null, (err) => {

if (err) throw err;

})

build.stdout.pipe(process.stdout)

build.on("exit", () => {

compress()    //build完成后,执行压缩

})

//压缩

function compress() {

compressing.zip.compressDir(path.join(__dirname+"/vuedemo/dist"), path.join(__dirname+"/vuedemo/dist.zip"))

.then(() => {

console.log('compress success!');

connect()

})

.catch(err => {

console.error(err);

});

}

var localPath = path.join(__dirname+"/vuedemo/dist.zip")   //本地项目打包路径

var romotePath = "/var/www/html/dist.zip"                  //远程服务器部署路径

//创建ssh链接

let conn = new ssh2.Client();

function connect() {

conn.on("ready", () => {

conn.sftp((err, sftp) => {

sftp.fastPut(localPath, romotePath, {}, (err, result) => {

if (err) throw err;

shell(conn)

})

})

}).connect({

host: 'xx.xx.xx.xx',   //远程服务IP地址

port: 22,

username: "root",

password: "root"

})

}

function shell(conn) {

conn.shell((err, stream) => {

stream.end(`

cd /var/www/html

mv dist bak/dist.$(date "+%Y%m%d%H%M%S").bak

unzip dist.zip

rm -rf dist.zip

exit

`).on("data", data => {

console.log(data.toString());

}).on("close", () => {

conn.end()

})

})

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值