最近公司有个项目需要部署到公网进行测试。由于更新频繁,手动更新服务器太麻烦。仔细研究了github上的webhook,开发了一个自动获取代码的小工具。LogicalanalysisdiagramDothesmalltoolcode-get,以及部分代码md5sum41a4c2615848eebedb261c61fd0d3074code-get25d3637a6fd821f419486548c32666afcode-get.exesha256sumb9b6ddcdb77af002a0f14f61192bfe90effaa5c533d3c5d2e0bdc7a3466d0a0ccode-get3172dcfa76d9986520b96c998529571002cb3a25e285a6f6da0dba98a024b38acode-get.exeif!Debug{mac:=hmac.New(sha1.New,[]byte(*token))_,_=mac.Write(data)expectedMAC:=hex.EncodeToString(mac.Sum(nil))if!hmac.Equal([]byte(signature[5:]),[]byte(expectedMAC)){writer.WriteHeader(http.StatusInternalServerError)writer.Write([]byte("Inconsistentsignature"))return}}event:=request.Header.Get("X-GitHub-Event")开关strings.ToLower(事件){case"ping":ifgithub.PingEvent(data){writer.WriteHeader(http.StatusOK)writer.Write([]byte("connectedsuccessfully"))return}case"push":ifgithub.PushEvent(data){writer.WriteHeader(http.StatusOK)writer.Write([]byte("更新成功"))return}}服务器部署下载文件code-get到服务,解压出来拿文件,用上面的总结验证code-getcode-get.execode-get.sumrepositories.conf生成项目key,这个很重要,后面会用到ssh-keygen-trsa-C".$(hostname)"-f-N""修改repositories.conf中的配置,注意目录权限[test-repos]#可以包含多个,对于项目名key=#填写上一步生成的私钥路径path=#填写你想要的目录保存github项目,注意目录权限branch=master#默认响应分支可以换成其他分支remote_path=git@github.com:xxxxx/test-repos.git#响应的github地址,ssh方法可以在私有库中使用#高级功能,接收钩子触发后响应操作,如果没有内置操作#不仅可以用于克隆代码,还可以用于其他操作#script=/var/www/例如#!/bin/bashREPOS_PATH=${WORK_PATH:-/var/www/html/$REPOS}cd$REPOS_PATH||退出2CUR_BRANCH=${BRANCH:-master}if[["$(gitrev-parse--abbrev-refHEAD)"!="$CUR_BRANCH"]];然后gitcheckout-b$CUR_BRANCHgitbranch--set-upstream-to=origin/$CUR_BRANCHfiif!gitpullorigin$CUR_BRANCH:$CUR_BRANCH;然后gitfetchorigin/$CUR_BRANCHgitreset--hardorigin/$CUR_BRANCHfi#如果是composer管理的,你需要做一些额外的工作if[[-fcomposer.json]];那么如果gitlog-1--name-only|grep-q'^composer\.json';然后如果[[-fcomposer.lock]];然后composerupdate--optimize-autoloader--no-dev--no-plugins--no-scriptselsecomposerinstall--optimize-autoloader--no-dev--no-plugins--no-scriptsfielse#If已经初始化好了,直接更新缓存composerdump-autoload--optimizefifiif[-fthink];然后php认为优化:自动加载php认为优化:配置php认为优化:路由php认为优化:schemaelif[-fartisan];然后phpartisanroute:cachephpartisanview:cachephpartisanconfig:cachefi生成daemon服务,提供systemd模板,启用systemctlenable/code-get.service&&systemctlstartcode-get.service。[单位]说明=code-get[Service]User=www-dataExecStart=/usr/local/bin/code-get-token"112233"-port17293-c/etc/code-get/repositories.confTimeoutStopSec=3sRestart=always[安装]WantedBy=multi-user.target使用netstat-lntp查看是否开启成功,并开启防火墙允许服务端口github配置将生成的ssh公钥填入该验证服务的code-get通信项目。默认情况下,`http://:17293如果不想暴露太多端口,可以使用nginx转发location=/code-get-server{proxy_passhttp://127.0.0.1:17293;}分享网站的端口,http:///code-get-server红框中填写上一步的token,最后出现一个绿色复选标记表示成功。现在你在本地拉取代码修改后push到github,服务器收到通知后自动拉取代码?如果因为网络原因没有及时拉取推送,可以手动更新-ucode-get-token"112233"-c/etc/code-get/repositories.conf-u会更新repositories.conf中列出的所有项目