每次更新完代码,手动push 到远程仓库,若是想让服务器的代码也同步的话,须要手动去服务器上面,拉取,编译,把编译后的代码复制须要的路径。前端
在你的github项目中 点击 settings 会在左侧webhooksvue
hook 钩子,想要在git某个生命周期触发一个事件,就是想让程序在咱们触发一个git事件的时候,作一些事情。
举个列子:当咱们把代码 git push origin master 的时候,push成功之后,但愿服务器自动把代码拉取,更新,作一些本身但愿作的事情。git
下面以 vnshop10 为例子,点击图片的 add webhook 按钮github
Payload URL 当咱们提交代码后,git webhook 会像这个url提交一个post请求web
Content type
一个是json 类型 (选择json类型)
一个是x-wwwl-from-urlencoded 类型shell
Which events would you like to trigger this webhook?npm
咱们选择 Just the push event.json
git clone https://github.com/itguide/deploy-vnshop.git deploy
须要修改的
api
添加的文件浏览器
const http = require('http') const shell = require('shelljs') const createHandler = require('github-webhook-handler') const handler = createHandler({ path: '/webhook', secret: 'vnshop' }) // 上面的 secret 保持和 GitHub 后台设置的一致 const port = 9988 const projects = ['vnshop.shudong.wang', 'vnshop', 'deploy', 'deploy-vnshop'] const projectHandler = (event, action) => { const project = event.payload.repository.name // 提交的仓库名字 console.log(project); const branch = event.payload.ref if (projects.includes(project)) { console.log(new Date(), `Received a ${action} event for ${project} to ${branch}`) shell.exec(`sh ./projects/${project}.sh`, (code, stdout, stderr) => { console.log(new Date(), 'Exit code:', code) // console.log(new Date(), 'Program output:', stdout) console.log(new Date(), '执行完毕!错误信息:?', stderr) }) } } http.createServer((req, res) => { handler(req, res, err => { res.statusCode = 404 res.end('no such location') }) }).listen(port, () => { console.log(new Date(), `Deploy server Run!port at ${port}`) shell.exec('echo shell test OK!', (code, stdout, stderr) => { // console.log('Exit code:', code) // console.log('Program output:', stdout) // console.log('Program stderr:', stderr, stderr === '', !!stderr) }) }) handler.on('error', err => { console.error('Error:', err.message) }) handler.on('push', event => { projectHandler(event, 'push') }) handler.on('commit_comment', event => { projectHandler(event, 'commit') })
path:和git webhooks的payload 保持一致 记得前面加上完整url路径 好比: http://vx.itnote.cn:9988/webhook
至关于一个api,每次咱们往github 提交事件的时候,github 的webhooks 根据咱们设定的事件,而后像这个url提交一个post请求,而后服务器就会根据触发的请求,作一些事情。
secret:和git webhooks的secret 保持一致
注意:如下shell 是针对这个教程的项目仅供参考,若是你的项目不符合,请修改本身的shell,
个人项目是 vnshop10 因此这个shell文件名字是 vnshop10
须要添加的文件
vnshop10.sh
#!/bin/bash WEB_PATH='/home/wwwroot/vnshop/' WEB_PATH_CLIENT='/home/wwwroot/vnshop/client' WEB_USER='www' WEB_USERGROUP='www' # we can do echo "Start deployment vx.itnote.cn" cd $WEB_PATH echo "pulling source code..." # git reset --hard origin/release # git clean -f # 把项目拉取到最新 git pull git checkout master echo "changing permissions..." # 切换到client里面 cd $WEB_PATH_CLIENT # 把vue项目编译 npm run build chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH_CLIENT echo "Finished."
检查一下远程仓库是谁的 git remote -v 更改为本身的仓库 先删除远程的仓库地址 git remote remove origin 添加本身的仓库地址 git remote add origin 本身的仓库地址
cd /home/wwwroot git clone https://github.com/itguide/deploy-vnshop.git 进入到项目里面 cd /home/wwwroot/deploy-vnshop 安装依赖包 cnpm i
而后启动这个deploy项目
cd /home/wwwroot/deploy-vnshop 使用pm2 启动,须要提早安装 pm2 npm i -g pm2 pm2 start index.js --name deploy --watch -i max -e ./logs/deploy/error.log -o ./logs/deploy/out.log
netstat -anp | grep 9988
可使用浏览器打开
http://vx.itnote.cn:9988/webhook
若是浏览器不能够访问,是阿里云主机的话,须要配置安全组规则
上面的信息,都要和本身填写的保持一致
生成一个 webhook 点击进去
出现如下状况,表示配置成功
在本地修改代码,而后提交到master 通过几秒钟漫长的等待,发现线上代码成更改