前端项目部署时,nginx配置完成后,只需将打包后的文件上传至服务器指定目录下便可。 前端
通常使用如下方式完成:node
减小web项目在开发调试过程当中频繁编译打包后再使用ftp工具部署至服务器的手动过程,提升工做效率。nginx
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": "bash deploy.sh"
},
复制代码
npm run build
echo "打包完成"
node ./ssh.js
cd -
复制代码
const Client = require('ssh2-sftp-client')
const sftp = new Client()
const helper = require ('./helper')
const config = [
{
name: 'a', // 项目/服务器名称
ssh: {
host: '192.168.0.105',
port: 22,
username: 'root',
password: 'root',
},
romotePath: '/var/www/dist',// 远程地址
localPath:'./dist',// 本地地址
},
{
name: 'b',
ssh: {
host: '192.168.0.110',
port: 22,
username: 'root',
password: 'root',
},
romotePath: '/var/www/dist',
localPath:'./dist',
}
]
async function main() {
const SELECT_CONFIG = (await helper(config)).value // 所选部署项目的配置信息
console.log('您选择了部署 ' + SELECT_CONFIG.name)
sftp
.connect(SELECT_CONFIG.ssh)
.then(() => {
console.log('- 链接成功,上传中..')
return sftp.uploadDir(SELECT_CONFIG.localPath, SELECT_CONFIG.romotePath)
})
.then(data => {
console.log(data,' 上传完成,及时清除缓存' )
})
.catch(err => {
console.log(err,' 出错了!快看看怎么回事! ')
})
.finally(() => {
sftp.end()// 断开链接
})
}
main()
复制代码
winpty npm.cmd run deploy
复制代码
项目github地址。 欢迎star、留言、issue。 但愿本文对各位有所帮助,祝工做生活愉快!git