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

相關文章
相關標籤/搜索