使用git webhook實現代碼自動部署


需求來源於最近准備做一款區塊鏈交易系統的應用,本地編碼,服務器運行調試,來回頻繁切換效率地,費時間,所以就有了自動部署的需求。

折騰了大半天,終於搞定了git的自動部署。中間踩過的坑遇到記錄一下,不詳細講解了,容易誤導他人。

第一步:部署docker,安裝lnmp。基本按照這一篇實現:https://www.jianshu.com/p/34a625621a9a

注意事項:
其一:
文中第三步:
[root@b1fc5ed806d1 /]# wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && ./install.sh lnmp
lnmp1.5.tar.gz的版本安裝屢次失敗,改為lnmp1.7.tar.gz。猜測測試centos版本跟lnmp版本不適應導致的。【不重要,如果遇到失敗,可以試試其他版本】

其二docker-compose.yml配置。
version: '3.3' services: coins: image: 'jiangtian2020/lnmp:1.0' ports: - '7777:80' - '8888:8080' expose: - 80 - 8080 - 8081 - 8082 environment: TZ: Asia/Shanghai tty: 'true'
簡單配置即可,無需太復雜。文檔:https://deepzz.com/post/docker-compose-file.html#toc_5
配置不難,仔細閱讀一遍,一般沒有依賴容器,沒有共享網絡等,無需過深研究這個配置。因為我是lnmp集成環境,都裝了,所以無需依賴,也無需共享其他網絡。

其三問過運維同事:

第二步:git--webhook配置回調地址:

配置教程參考:https://www.weipxiu.com/4103.html

自動部署代碼(php版)

`<?php
/**

  • Created by PhpStorm.
  • Author: jt
  • Date: 2020/6/6
  • Time: 上午9:53
  • E-Mail: tibertwater@gmail.com
  • QQ: 284053253
    */

ini_set('display_errors', 'On');
error_reporting(E_ALL);
//git webhook 自動部署腳本
$requestBody = file_get_contents("php://input"); //該方法可以接收post傳過來的json字符串
defined("LOG_DIR") or define("LOG_DIR", "/home/wwwroot/coins/App/Runtime/Logs/Git/");
defined("SRC_DIR") or define("SRC_DIR", "/tmp/coins");
defined("DES_DIR") or define("DES_DIR", "/home/wwwroot/coins");
if (empty($requestBody)) { //判斷數據是不是空
die('send fail');
}

$content = json_decode($requestBody, true); //數據轉換
if (!$content) parse_str(urldecode($requestBody), $content);
$content = json_decode($content['payload'], true);

//若是主分支且提交數大於0
if ($content['ref'] == 'refs/heads/master') {
file_put_contents(LOG_DIR . "git-webhook.log", "寫入日志" . PHP_EOL, FILE_APPEND);
//PHP函數執行git命令
/**$bool = chdir(SRC_DIR);
if ($bool === false) {
die("Could not chdir()");
}*/
//$re = shell_exec("ls -al");
$res = shell_exec('chmod -R 777 /tmp/coins;cd /tmp/coins;git reset --hard origin/master && git clean -f && git pull 2>&1 && git checkout master;');
$dir = getcwd();
file_put_contents(LOG_DIR . "git-content.log", "當前目錄:{$dir}" . PHP_EOL, FILE_APPEND);
file_put_contents(LOG_DIR . "git-content.log", $res . PHP_EOL, FILE_APPEND);
file_copy(SRC_DIR, DES_DIR);

$res_log.= ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name']
	. '項目的' . $content['ref'] . '分支push' . $res . PHP_EOL;
//將每次拉取信息追加寫入到日志里
file_put_contents(LOG_DIR . "git-webhook.log", $res_log, FILE_APPEND);

}

function file_copy($src, $dst) {
$dir = opendir($src);
if (!file_exists($dst)) mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..') && $file != '.git') {
if (is_dir($src . '/' . $file)) {
file_copy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
`

其中有幾個重大的坑:
其一:shell_exec在執行命令的時候,cd始終沒有反應,最終追查來追查去,是因為lnmp中nginx的fastcgi.conf中加了個參數:
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/:/home:/tmp/coins";
它限定了可訪問的目錄。導致我指定的目錄始終無權限,以上是我修改過的參數。
其二:php的函數chdir是同樣問題。

第三:以上問題解決完,進入測試自動部署階段。可是測來測去,始終報一個錯誤:
fatal: could not read Username for 'https://github.com': No such file or directory?
該問題出現的原因是我是在root下做的所有操作,但是nginx及php用戶是www,用戶組是www,懷疑是權限問題,但chown或者chmod777都不可以。
后來找到一種說法,說是用ssh,服務器跟git建立起公私鑰配對。
便試着操作了下:
在root下,更改倉庫.git/configx下的url。

然后git pull會自動要求生成公私鑰,或者用ssh-keygen -t rsa -C 'abc@xxx.com' (git郵箱)

在git配置ssh公鑰:

並且將id_rsa.pub中的字符串粘貼到git去。

以為大功告成?一運行,提示:
Host key verification failed. fatal: Could not read from remote repository.

啥原因?因為我的公鑰是root賬戶下生成的,而非nginx,php賬戶,所以得給它生成一個。 usermod -s /bin/bash www,讓php,nginx賬戶成為一個普通賬戶。按以上步驟進行一遍。再運行,大功告成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM