美文网首页
半自动化部署webHook钩子配置

半自动化部署webHook钩子配置

作者: Super三脚猫 | 来源:发表于2018-08-30 16:01 被阅读0次

    半自动化部署(钩子)

    码云-logo.png

    这里没有用github,因为github私有库是收费的,并且github上的爬虫太多。有一些私有的东西不方便,所以选择码云,它的私有库不收钱不限制,还是中文的,所以选择用码云来操作。(项目成熟之后再开源)

    • 先设置 WebHook


      WebHooks设置.png

    这里记得设置一下,二级域名,用来钩子的调用。

    • 服务器配置域名指向钩子的【测试】文件
    <?php
            // echo shell_exec('whoami');die;  # 测试web访问用户组 (隐藏的坑)
            $result = shell_exec("cd /home/wwwroot/blog && git pull origin master && echo 'shell_success'");
            file_put_contents('test.txt', 'success', FILE_APPEND);
            file_put_contents('test.txt', '================', FILE_APPEND);
            var_dump($result);
            echo 'success';
    
    • 正式钩子,写入提交记录log日志代码:
    <?php
        // 项目存放物理路径
        $savePath = "/home/wwwroot/blog";
        // 代码仓库
        $gitPath  = "git@gitee.com:xxxxxxxx/blog.git";
    
        // 获取钩子post参数
        $requestBody = file_get_contents("php://input");
        if (empty($requestBody)) {
            die('send fail');
        }
    
        // 解析Git服务器通知过来的JSON信息
        $content = json_decode($requestBody, true);
    
        // 若是主分支且i && 提交数大于0
        if ($content['ref'] == 'refs/heads/master' && $content['total_commits_count'] > 0) {
            $res = PHP_EOL . "pull start ------------------"; // PHP_EOL 代表 \r\n 换行
            $res .= ' <' . implode('', array_column($content['commits'], 'message')) . '>' . PHP_EOL; // commit 版本名称
    
            // 拉取代码
            $res .= shell_exec("cd {$savePath} && git pull {$gitPath}");
    
            // 日志编写
            $res_log = PHP_EOL . '>>>>>>>>>>>>========================================<<<<<<<<<<<<' . PHP_EOL . PHP_EOL;
            $res_log .= '【' . $content['user_name'] . '】在【' . date('Y-m-d H:i:s');            // 谁在什么时间
            $res_log .= '】向【' . $content['repository']['name'] . '】项目的【' . $content['ref'] . '】'; // 向谁的什么
            $res_log .= '分支push了' . $content['total_commits_count'] . '个commit:' . PHP_EOL;  // 怎么做了什么事情
            $res_log .= $res; // 放入git记录代码
            $res_log .= "pull end --------------------" . PHP_EOL;
    
            // 写入日志到log文件中
            file_put_contents("git-webhook_log.txt", $res_log, FILE_APPEND);
    
            echo 'Success';die;
        }
    
        echo 'Error';die;
    
    • 正式钩子后的日志样式
    >>>>>>>>>>>>========================================<<<<<<<<<<<<
    
    【SuperHaoHao】在【2018-12-26 16:02:22】向【Laravel55-LTS】项目的【refs/heads/master】分支push了1个commit:
    
    pull start ------------------ <- test: 测试正式版Hook脚本-完成样式OK的v3>
    Updating 203c107..c292f87
    Fast-forward
     app/Http/Controllers/Applets/v1/User/LoginController.php | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    pull end --------------------
    
    {"ref":"refs/heads/master","before":"729aa4bbffadd38c10049e1bac4b21d06e32db48","after":"fbd3204cd86b774711c08501141775f06b0690f3","total_commits_count":1,"commits_more_than_ten":false,"created":false,"deleted":false,"compare":"https://gitee.com/XuChen****/blog/compare/729aa4bbffadd38c10049e1bac4b21d06e32db48...fbd3204cd86b774711c08501141775f06b0690f3","commits":[{"id":"fbd3204cd86b774711c08501141775f06b0690f3","tree_id":"386c9c4a600ec6955ad4b624ddb7f55741f31a96","message":"- test: 测试正式版Hook脚本v4","timestamp":"2018-12-26T15:13:47+08:00","url":"https://gitee.com/XuChen****/blog/commit/fbd3204cd86b774711c08501141775f06b0690f3","author":{"time":"2018-12-26T15:13:47+08:00","id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****"},"committer":{"id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****"},"distinct":true,"added":[],"removed":[],"modified":["app/Http/Controllers/Applets/v1/User/LoginController.php"]}],"head_commit":{"id":"fbd3204cd86b774711c08501141775f06b0690f3","tree_id":"386c9c4a600ec6955ad4b624ddb7f55741f31a96","message":"- test: 测试正式版Hook脚本v4","timestamp":"2018-12-26T15:13:47+08:00","url":"https://gitee.com/XuChen****/blog/commit/fbd3204cd86b774711c08501141775f06b0690f3","author":{"time":"2018-12-26T15:13:47+08:00","id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****"},"committer":{"id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****"},"distinct":true,"added":[],"removed":[],"modified":["app/Http/Controllers/Applets/v1/User/LoginController.php"]},"repository":{"id":4613621,"name":"Laravel55-LTS","path":"blog","full_name":"SuperHaoHao/Laravel55-LTS","owner":{"id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****","login":"XuChen****","avatar_url":"https://gitee.com/uploads/51/1178551_XuChen****.png?1509607253","html_url":"https://gitee.com/XuChen****","type":"User","site_admin":false},"private":true,"html_url":"https://gitee.com/XuChen****/blog","url":"https://gitee.com/XuChen****/blog","description":"","fork":false,"created_at":"2018-10-29T15:28:53+08:00","updated_at":"2018-12-26T14:50:07+08:00","pushed_at":"2018-12-26T14:50:07+08:00","git_url":"git://gitee.com/XuChen****/blog.git","ssh_url":"git@gitee.com:XuChen****/blog.git","clone_url":"https://gitee.com/XuChen****/blog.git","svn_url":"svn://gitee.com/XuChen****/blog","git_http_url":"https://gitee.com/XuChen****/blog.git","git_ssh_url":"git@gitee.com:XuChen****/blog.git","git_svn_url":"svn://gitee.com/XuChen****/blog","homepage":null,"stargazers_count":0,"watchers_count":1,"forks_count":0,"language":"PHP","has_issues":true,"has_wiki":true,"has_pages":false,"license":null,"open_issues_count":0,"default_branch":"master","namespace":"XuChen****","name_with_namespace":"SuperHaoHao/Laravel55-LTS","path_with_namespace":"XuChen****/blog"},"project":{"id":4613621,"name":"Laravel55-LTS","path":"blog","full_name":"SuperHaoHao/Laravel55-LTS","owner":{"id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****","login":"XuChen****","avatar_url":"https://gitee.com/uploads/51/1178551_XuChen****.png?1509607253","html_url":"https://gitee.com/XuChen****","type":"User","site_admin":false},"private":true,"html_url":"https://gitee.com/XuChen****/blog","url":"https://gitee.com/XuChen****/blog","description":"","fork":false,"created_at":"2018-10-29T15:28:53+08:00","updated_at":"2018-12-26T14:50:07+08:00","pushed_at":"2018-12-26T14:50:07+08:00","git_url":"git://gitee.com/XuChen****/blog.git","ssh_url":"git@gitee.com:XuChen****/blog.git","clone_url":"https://gitee.com/XuChen****/blog.git","svn_url":"svn://gitee.com/XuChen****/blog","git_http_url":"https://gitee.com/XuChen****/blog.git","git_ssh_url":"git@gitee.com:XuChen****/blog.git","git_svn_url":"svn://gitee.com/XuChen****/blog","homepage":null,"stargazers_count":0,"watchers_count":1,"forks_count":0,"language":"PHP","has_issues":true,"has_wiki":true,"has_pages":false,"license":null,"open_issues_count":0,"default_branch":"master","namespace":"XuChen****","name_with_namespace":"SuperHaoHao/Laravel55-LTS","path_with_namespace":"XuChen****/blog"},"user_id":1178551,"user_name":"SuperHaoHao","user":{"id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****"},"pusher":{"id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****"},"sender":{"id":1178551,"name":"SuperHaoHao","email":"61****123@qq.com","username":"XuChen****","user_name":"XuChen****","url":"https://gitee.com/XuChen****","login":"XuChen****","avatar_url":"https://gitee.com/uploads/51/1178551_XuChen****.png?1509607253","html_url":"https://gitee.com/XuChen****","type":"User","site_admin":false},"enterprise":null,"hook_name":"push_hooks","password":""}
    

    Hook - 坑

    正常来说上面的流程已经结束了,但是... 其实肯定能中奖下面的这些问题

    • 解除 php.ini 禁用的 shell_exec() 函数

    【Lnmp的php.ini路径】# vim /usr/local/php/etc/php.ini

    php.ini.png
    • 无法执行 git 的所有命令,比如说 git pull origin master (但是可以执行cd /home/wwwroot/blog),并且无法切换 su www 用户
    切换www用户.png
    Error-www操作git.png

    解决 www 权限的各种问题

    【查看用户组】  cat /etc/passwd
    【修改nologin】 vim /etc/passwd   把 /sbin/nologin 改为 /bin/bash
    【使用www登陆】 su www
    【修改文件属组】 chown -R www:www blog/
    
    • 上面正式版代码,有写入文件,记得给文件写入权限
    【修改文件权限】chmod -R 755 git-webhook_log.txt
    
    • 需要先手动拉取一遍代码(因为必须是.ssh)
    www用户操作git.png
    • 附上我16遍钩子的 git commit 图 # emmmmmmm
    commitList.png

    emmmmm。。。。。

    相关文章

      网友评论

          本文标题:半自动化部署webHook钩子配置

          本文链接:https://www.haomeiwen.com/subject/yqhdwftx.html