前端项目打包,自动部署

该博客介绍了如何使用NodeSSH和scp2库实现前端项目的自动打包、上传及通过ssh执行升级脚本来部署到服务器。首先,通过deploy.js连接并执行升级脚本,清理旧的dist目录,然后创建新的dist目录。接着,项目打包后,使用scpClient进行文件同步上传。整个过程实现了自动化,提高了部署效率。
摘要由CSDN通过智能技术生成

前端项目自动打包,自动部署,nginx提前配置,目录对应好

//deploy.js 放置项目根目录
const { NodeSSH } = require('node-ssh');
const scpClient = require('scp2');
const path = require('path');
const ssh = new NodeSSH();

const config = {
    host: 'xxxx',
    port: 22,
    pathUrl: '/home',
    userName: 'root',
    password: 'xxxx'
};

const connectServer = function () {
    ssh.connect({
        host: config.host,
        username: config.userName,
        port: config.port,
        password: config.password,
    }).then(() => {
        console.log('SSH login success, 服务器连接成功!');
        ssh.execCommand('sh upgrade.sh', { cwd: config.pathUrl }).then((result) => {
            // console.log(`The update message is: ${result.stderr}`);
            console.log('%j', result);
            if (!result.stderr) {
                console.log('sh upgrade.sh 脚本执行完成!');
                syncUpload()
            } else {
                console.log('Something wrong:', result);
            }
        })
    })
}

const syncUpload =  function () {
    console.log('进入了',);
     scpClient.scp(
        './dist',
        {
            host: config.host,
            port: config.port,
            username: config.userName,
            password: config.password,
            path: config.pathUrl+'/dist'
        },
        function (err) {
            if (err) {
                console.log('发布失败');
                // throw err;
            } else {
                console.log('Success! 成功发布到' + config.host + '服务器! ');
            }
        },
        function (aaa) {
            console.log('aaa', aaa);
        }
    );
}
process.on('unhandledRejection', (reason, p) => {
    console.log('Promise: ', p, 'Reason: ', reason)
    // do something
})

connectServer()

//upgrade.sh 放置代码部署的目录下

rm -rf dist
rm -rf dist.zip 
mkdir dist
//项目打包命令修改
xxxxx bulid && node ./deploy
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值