前端自动打包部署上线记录( • ̀ω•́ )✧

理一下思路:(其实这个过程就是执行一堆命令的事情了,后续会一步步优化,比如说输出日志什么的)

1、从github上拉取代码到服务器指定目录(git clone 代码仓库地址),

2、进入到 服务器指定目录/仓库目录 执行依赖安装(例 npm install),

3、根据安装依赖的结果,成功则执行打包编译(例 npm run build),

4、根据编译结果,执行部署的操作(应该就是通过shh远程上传文件到对应服务器酱紫吧)

开坑:

在危险边缘试探~

最初为了测试远程执行命令,做了比较简单的试探

直接把仓库先拷到服务器了,然后用nodejs的exec去执行命令

这里踩了一个坑,就是exec()无法执行‘cd 目录名’的命令,原因如下

Yes, cd is not supported by exec. This is because exec spawns a child-process, and child-processes cannot change their parent's cwd. For example, exec('cd baz; myCommand'); would change the cwd for myCommand, but not for node.

然后找到了解决办法,这里要去看nodejs关于进程的文档

https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Each command is executed in a separate shell, so the first cd only affects that shell process which then terminates. If you want to run git in a particular directory, just have Node set the path for you:

exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);

cwd (current working directory) is one of many options available for exec.


一本正经起来~

猜你喜欢

转载自www.cnblogs.com/cheeseCatMiao/p/9856669.html
今日推荐