随着前端工程化的推动,如今的前端开发流程已经日趋规范化,在咱们的平常开发中,每完成一次功能的开发,通常须要通过如下步骤:html
若是这些步骤每一次都须要手动执行,不免显得繁琐、效率低下,做为一个程序员,怎么能忍受?若是能作到咱们一提交代码,自动的帮咱们打包构建=>部署=>重启服务就省事多了。以前一直有了解到GitHub有Webhooks这个功能,这篇记录就来填个坑,实现前端工程的自动化部署。前端
出于示例目的,先在Github中开一个新的仓库命名为Webhooks-demo,使用vue-cli生产初始代码,并上传到仓库中,并把项目部署到服务器(本文使用腾讯云,操做系统为ubuntu)运行。vue
本文项目服务器环境使用nginx做为代理服务器运行,须要在nginx配置中设置目录指向项目的dist目录(也就是打包出来的目录)。node
location / {
root /home/ubuntu/webhooks-demo/dist; #网站主页路径。此路径仅供参考,具体按照实际目录操做。
index index.html index.htm;
}
复制代码
使用浏览器访问服务器域名或者公网IP地址,能正常访问则项目运行环境搭建完成nginx
首先分析实现自动化部署功能的需求:git
git push
操做时,须要执行git pull
操做拉取最新代码npm install
,项目可能会有新加的npm包npm run build
(参考使用,以具体项目为主),使用Webpack打包源码,生成dist目录sudo service nginx restart
,重启nginx服务搭建Node服务器,用于监听Webhooks发出的POST请求,并作出下一步操做。程序员
在项目根目录下新建autoBulid.js
文件,代码比较简单github
const http = require('http')
const createHandler = require('github-webhook-handler')
const handler = createHandler({ path: '/webhook', secret: 'myhashsecret' })
// 上面的 secret 保持和 GitHub Webhooks 后台设置的一致 下文会提到
// secret: 'myhashsecret' 记住 后面会用到
// path 是webhook访问的url 后面会提到
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(7777)
handler.on('error', function (err) {
console.error('Error:', err.message)
})
handler.on('push', function (event) {
console.log('Received a push event for %s to %s',
event.payload.repository.name,
event.payload.ref)
})
复制代码
这里用到了github-webhook-handler这个包,用于监听Webhooks发出的请求,具体介绍可前往npm查看,此时Node服务基本搭建完成,在服务器中运行autoBulid.js
web
node autoBulid.js
复制代码
首先在项目的GitHub主页设置中开启Webhooks功能vue-cli
Webhooks allow external services to be notified when certain events happen. When the specified events happen, we’ll send a POST request to each of the URLs you provide.
大概意思就是Webhooks在检测到仓库有事件触发时会发送一个POST请求到你给定的URL去,从而进行后续的部署工做。点击“Add webhook”按钮,建立一个webhook。
到这一步,项目已经能检测到项目仓库的变动,接下来须要编写脚原本跑命令,实现项目的更新和部署。
一样在根目录下新建一个autoBuild.sh
文件,写入如下代码。
git pull origin master # 拉取最新代码
npm install # 安装依赖
npm run build # 打包
sudo service nginx restart # 重启nginx
复制代码
有了脚本文件,咱们须要在前面建立的Node服务中调用它,修改autoBuild.js
代码。
const http = require('http')
const createHandler = require('github-webhook-handler')
const handler = createHandler({ path: '/webhook', secret: 'myhashsecret' })
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(7777)
handler.on('error', function (err) {
console.error('Error:', err.message)
})
handler.on('push', function (event) {
console.log('Received a push event for %s to %s',
event.payload.repository.name,
event.payload.ref)
run_cmd('sh', ['./autoBuild.sh'], function(text){ console.log(text) }); // 执行autoBuild.sh
})
// 新增run_cmd函数
function run_cmd(cmd, args, callback) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = "";
child.stdout.on('data', function(buffer) { resp += buffer.toString(); });
child.stdout.on('end', function() { callback (resp) });
}
复制代码
执行git push
提交代码,服务器上拉取代码并运行node autoBuild.js
。
接下来咱们在本地修改一下项目的代码,而后提交到Github,测试脚本是否能正常运行。
// App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
- <HelloWorld msg="Welcome to Your Vue.js App"/>
+ <HelloWorld msg="Webhook test"/>
</div>
</template>
复制代码
本地执行git push
提交代码更新到仓库中,经过Xshell观察服务器的输出。
autoBuild.sh
脚本,此时打开浏览器刷新查看页面变化。
到这里,使用Github提供的Webhooks来实现自动化部署就已经大体完成了,总体过程没什么难度,一些细节的地方还能够继续优化,其实能实现的不止有这么多,能够在autoBuild.sh
文件中加入单元测试之类,能够发挥的空间还很大。固然,实现自动化部署的方案不止Webhook这一种,如今我所在公司使用的是GitLab的CI,可使用还有Travis CI和Jenkins等等。此次的记录就写到这里,今年刚毕业萌新第一次在掘金发文章,写的很差的地方望你们谅解。