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

 

转载于:https://www.cnblogs.com/chenrunxuan/p/11352240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值