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

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

  • 逻辑分析图
    《基于github webhook的代码自动部署工具》
  • 动手搞小工具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项目的
      《基于github webhook的代码自动部署工具》
    • 验证服务的code-get通讯,红色框填入上一步中的token
      《基于github webhook的代码自动部署工具》
    • 最终呈现绿色对勾就表明成功了
      《基于github webhook的代码自动部署工具》
  • 现在你本地拉取代码修改后pushgithub,服务器收到通知就自动拉取代码了😎
  • 如果网络原因导致push没有被及时拉取,可手动更新-u

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

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

    原文作者:小苏梅
    原文地址: https://segmentfault.com/a/1190000019946973
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞