应用场景
通常情况下,项目需要部署上下通常采用下面几种方式
以Vue项目为例,打包之后
以上两种方式都不是最优解,第一种方式操作步骤繁琐;第二种服务器需要安装node&git环境,并且多了项目源码,浪费服务器资源
实现自动化部署
仅需一个指令,实现自动化部署
- 在项目中下载依赖
npm install scp2 ora --save-dev
- 根目录新建:deploy.js(ora部分可有可无,不用可隐去)
const scpClient = require('scp2')
const ora = require('ora')
const server = {
host:'xxx.xxx.xxx.xxx',//服务器IP
port:22,//服务器端口
username:'xxxxxx',//服务器ssh登录用户名
password:'xxxxxx',//服务器ssh登录密码
path:'/www/xxxx/xxxx'//服务器web目录
}
const loading = ora('正在部署至 ' + server.host )
loading.start()
scpClient.scp('dist/', server ,(err)=>{
loading.stop()
if(err) {
console.log('部署失败')
throw err
}else {
console.log('部署成功')
}
})
- 在package.json中的script中添加命令
"scripts": {
...
"deploy": "npm run build && node ./deploy.js",
...
},