github使用Webhooks实现自动化部署

参考:php

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

--------------------------------------------github

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

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

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; }

 

调试:vim

  咱们能够本身访问一下接口。服务器

  

以上是正确的返回测试

 

而后去github的项目仓库设置this

 

 

一般以上设置完以后会报错,好比返回的stderr字段spa

sudo: no tty present and no askpass program specified

 这是最多见的报错

登陆服务器执行的命令和shell执行的命令权限是不一样的

解决:

 # vim /etc/sudoers

相关文章
相关标签/搜索