Day 32/100 前端写脚本发布项目到远程服务器

(一)需求

每次是打好包,手动发布。

发的多了,就想着能不能写脚本,实现半自动发布项目。

(二)思路

1、需要提前做好免密登录

免密登录可查看
https://segmentfault.com/a/11...

2、JS中写Shell脚本

  • 安装ShellJS
    npm install [-g] shelljs
  • 项目打包
  • 上传打包好的文件

(三)实现代码

var shell = require('shelljs')
shell.echo('start build')

if (shell.exec('npm run test').code !== 0) { // 执行npm run build 命令
  shell.echo('Error: Git commit failed')
  shell.exit(1)
}
shell.echo('build end')

shell.echo('upload start')
// 将项目上传到服务器对应的目录下
shell.exec('scp dist/index.html root@IP:/目录/ ')
shell.exec('scp -r dist/js root@IP:/目录/ ')
shell.exec('scp -r dist/static root@IP:/目录/')

shell.echo('deploy end')
shell.exit(1)

参考链接

ShellJS GitHub项目

https://github.com/shelljs/sh...

你可能感兴趣的:(shell脚本自动化)