zoukankan      html  css  js  c++  java
  • github使用Webhooks实现自动化部署

    参考:

      https://blog.csdn.net/u013764814/article/details/85240752

    --------------------------------------------

    前提:本地安装git,服务器安装git

    这是要放到服务器上的代码,git通过一个接口访问到go方法。从而实现git pull。我开放的接口是 http://XXX.cn/index/index/go

    public function go()
        {
            // webhook上设置的secret
            $secret = "asdf123456";
            // 校验发送位置,正确的情况下自动拉取代码,实现自动部署
            $signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];
            if($signature) {
                $hash = "sha1=".hash_hmac('sha1', file_get_contents("php://input"), $secret);
                if (strcmp($signature, $hash) == 0) {
                    set_time_limit(3 * 60); //最大过期时间3分钟
                    $shellPath = "/www/wwwroot/testwechat";
                    $cmd = "cd $shellPath && sudo git pull";
                    $res = $this -> doShell($cmd);
                    print_r($res); // 主要打印结果给github记录查看,自己测试时查看
                }
            }
        }
    
        /*
        * 执行shell命令
        */
        protected function doShell ($cmd, $cwd = null) {
            $descriptorspec = array(
                0 => array("pipe", "r"), // stdin
                1 => array("pipe", "w"), // stdout
                2 => array("pipe", "w"), // stderr
            );
            $proc = proc_open($cmd, $descriptorspec, $pipes, $cwd, null);
            // $proc为false,表明命令执行失败
            if ($proc == false) {
                return false;
                // do sth with HTTP response
                print_r("命令执行出错!");
            } else {
                $stdout = stream_get_contents($pipes[1]);
                fclose($pipes[1]);
                $stderr = stream_get_contents($pipes[2]);
                fclose($pipes[2]);
                $status = proc_close($proc); // 释放proc
            }
            $data = array(
                'stdout' => $stdout, // 标准输出
                'stderr' => $stderr, // 错误输出
                'retval' => $status, // 返回值
            );
    
            return $data;
        }

    调试:

      我们可以自己访问一下接口。

      

    以上是正确的返回

    然后去github的项目仓库设置

    通常以上设置完之后会报错,比如返回的stderr字段

    sudo: no tty present and no askpass program specified

     这是最常见的报错

    登录服务器执行的命令和shell执行的命令权限是不同的

    解决:

     # vim /etc/sudoers

  • 相关阅读:
    git 创建一个空分支
    github page的两种类型
    hexo-theme-next
    github网页
    Linux下的CPU使用率与服务器负载的关系与区别
    mysql数据库优化日志(更)-howyue
    图片延时加载
    jQuery实现页面滚动时顶部动态显示隐藏
    TCP与UDP区别
    记一次网站服务器迁移(my)
  • 原文地址:https://www.cnblogs.com/chenrunxuan/p/11352240.html
Copyright © 2011-2022 走看看