vue自动部署
Vue3 + vite项目利用scp2自动部署到服务器
1、项目根目录下新建deploy文件夹,创建index.js文件
import scpClient from 'scp2'
import ora from 'ora'
import chalk from 'chalk'
import ssh from 'ssh2'
const spinner = ora(
'正在发布到' +
(process.env.NODE_ENV === 'prod' ? '生产' : '测试') +
'服务器...'
)
const server = {
domain: '192.168.3.171:22',
host: '192.168.3.171',
port: 22,
username: 'username',
password: 'password
path: '/home/front'
}
const Client = ssh.Client
var conn = new Client()
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'))
throw err
} else {
console.log(
chalk.green('Success! 成功发布到' + (process.env.NODE_ENV === 'prod' ? '生产' : '测试') + '服务器! \n')
)
}
}
)
conn.end()
2.package.json中打包后执行该文件
"scripts": { "dev": "vite --open", "build": "vite build", "deploy": "vite build && node ./deploy", // 打包并上传到服务器 "serve": "vite preview" },
3、执行命令便会自动上传dist文件到服务器