分享
  1. 首页
  2. 文章

基于github webhook的代码自动部署工具

小苏梅 · · 1530 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

最近公司有个项目需要部署到公网上去测试,由于频繁更新,手动去服务器更新太麻烦,仔细研究githubweb hook,开发个自动获取代码的小工具咯

  • 逻辑分析图
    逻辑分析图
  • 动手搞小工具code-get,以及部分代码

    md5sum
    41a4c2615848eebedb261c61fd0d3074 code-get 
    25d3637a6fd821f419486548c32666af code-get.exe
    sha256sum
    b9b6ddcdb77af002a0f14f61192bfe90effaa5c533d3c5d2e0bdc7a3466d0a0c code-get
    3172dcfa76d9986520b96c998529571002cb3a25e285a6f6da0dba98a024b38a code-get.exe
     if !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("签名不一致"))
     return
     }
     }
     event := request.Header.Get("X-GitHub-Event")
     switch strings.ToLower(event) {
     case "ping":
     if github.PingEvent(data) {
     writer.WriteHeader(http.StatusOK)
     writer.Write([]byte("连接成功"))
     return
     }
     case "push":
     if github.PushEvent(data) {
     writer.WriteHeader(http.StatusOK)
     writer.Write([]byte("更新成功"))
     return
     }
     }
    
  • 服务器部署

    • 下载文件code-get到服务中,解压出来得到一下文件,并与上面的摘要验证
    code-get code-get.exe code-get.sum repositories.conf
    • 生成项目密钥, 很重要,后面要用到
    ssh-keygen -t rsa -C "<project>.$(hostname)" -f <path-to-save-ssh-key> -N ""
    • 修改repositories.conf中的配置,注意目录权限
    [test-repos] # 可以包含多个,为项目名
    key=<path-to-save-ssh-key> #填入上一步生成的private key路径
    path=<path-to-clone-repos> #填入你要保存github项目的目录,注意目录权限
    branch=master #默认响应的分支,可换成其他分支
    remote_path=git@github.com:xxxxx/test-repos.git #要响应的github地址,ssh方式可用于私有库
    #高级功能,接收到hook触发后响应的操作,没有则使用内置操作
    #可用于不仅仅只是clone代码, 还附带其他操作
    #script=/var/www/<my-diy-clone-script.sh>
    • 例如

      #!/bin/bash
      REPOS_PATH=${WORK_PATH:-/var/www/html/$REPOS}
      cd $REPOS_PATH || exit 2
      CUR_BRANCH=${BRANCH:-master}
      if [[ "$(git rev-parse --abbrev-ref HEAD)" != "$CUR_BRANCH" ]]; then
       git checkout -b $CUR_BRANCH
       git branch --set-upstream-to=origin/$CUR_BRANCH
      fi
      if ! git pull origin $CUR_BRANCH:$CUR_BRANCH; then
       git fetch origin/$CUR_BRANCH
       git reset --hard origin/$CUR_BRANCH
      fi
      #如果是composer管理的话要做一点而外工作
      if [[ -f composer.json ]]; then
       if git log -1 --name-only | grep -q '^composer\.json'; then
       if [[ -f composer.lock ]]; then
       composer update --optimize-autoloader --no-dev --no-plugins --no-scripts
       else
       composer install --optimize-autoloader --no-dev --no-plugins --no-scripts
       fi
       else
       #如果已经初始化好了,直接更新缓存
       composer dump-autoload --optimize
       fi
      fi
      if [ -f think ]; then
       php think optimize:autoload
       php think optimize:config
       php think optimize:route
       php think optimize:schema
      elif [ -f artisan ]; then
       php artisan route:cache
       php artisan view:cache
       php artisan config:cache
      fi
       
    • 生成daemon服务,提供systemd的模板,并启用systemctl enable <abs-path-to>/code-get.service && systemctl start code-get.service

      [Unit]
      Description=code-get
      [Service]
      User=www-data
      ExecStart=/usr/local/bin/code-get -token "112233" -port 17293 -c /etc/code-get/repositories.conf
      TimeoutStopSec=3s
      Restart=always
      [Install]
      WantedBy=multi-user.target
    • 通过netstat -lntp查看是否启用成功,并开启防火墙允许该服务端口
  • github端配置

    • 将生成的ssh公钥填入github项目的
      clipboard.png
    • 验证服务的code-get通讯,红色框填入上一步中的token
      clipboard.png
    • 最终呈现绿色对勾就表明成功了
      clipboard.png
  • 现在你本地拉取代码修改后pushgithub,服务器收到通知就自动拉取代码了????
  • 如果网络原因导致push没有被及时拉取,可手动更新-u

    code-get -token "112233" -c /etc/code-get/repositories.conf -u

    会更新所有repositories.conf列出来的项目


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:小苏梅

查看原文:基于github webhook的代码自动部署工具

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
1530 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏