{"id":1627,"date":"2022-03-17T10:21:02","date_gmt":"2022-03-17T02:21:02","guid":{"rendered":"https:\/\/www.siediyer.cn\/?p=1627"},"modified":"2022-03-18T16:54:20","modified_gmt":"2022-03-18T08:54:20","slug":"centos7git%e7%a0%81%e4%ba%91%ef%bc%8cwebhook%e8%87%aa%e5%8a%a8%e5%90%8c%e6%ad%a5%e9%83%a8%e7%bd%b2","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=1627","title":{"rendered":"CentOS7+Git+\u7801\u4e91\uff0cWebHook\u81ea\u52a8\u540c\u6b65\u90e8\u7f72"},"content":{"rendered":"<h1 id=\"git\u5b89\u88c5\">Git\u5b89\u88c5<\/h1>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">yum install -y git \r\ngit --version #\u7248\u672c \r\nrpm -qal |grep git #\u8def\u5f84\/usr\/bin\/git<\/pre>\n<h1 id=\"\u751f\u6210\u516c\u94a5\">\u751f\u6210<a class=\"hl hl-1\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E5%85%AC%E9%92%A5&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noopener\" data-report-click=\"{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https:\/\/so.csdn.net\/so\/search?q=%E5%85%AC%E9%92%A5&amp;spm=1001.2101.3001.7020&quot;}\" data-tit=\"\u516c\u94a5\" data-pretit=\"\u516c\u94a5\">\u516c\u94a5<\/a><\/h1>\n<p>\u751f\u6210\u516c\u94a5\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u66f4\u5b89\u5168\u66f4\u65b9\u4fbf\u7684<a class=\"hl hl-1\" href=\"https:\/\/so.csdn.net\/so\/search?q=SSH&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noopener\" data-report-click=\"{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https:\/\/so.csdn.net\/so\/search?q=SSH&amp;spm=1001.2101.3001.7020&quot;}\" data-tit=\"SSH\" data-pretit=\"ssh\">SSH<\/a>\u62c9\u53d6\u65b9\u5f0f\uff0c\u540c\u65f6\u8ba9git\u547d\u4ee4\u8bed\u53e5\u66f4\u7b80\u6d01\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo -u www ssh-keygen -t rsa -C \"6273302@qq.com\"<\/pre>\n<p>\u6ce8\u610f\uff1awww\u8868\u793a\u6709\u6743\u9650\u6267\u884c<a href=\"https:\/\/www.baidu.com\/s?wd=PHP&amp;tn=24004469_oem_dg&amp;rsv_dl=gh_pl_sl_csd\">PHP<\/a>\u7684\u7528\u6237<br \/>\n\u90ae\u7bb1\u53ef\u4ee5\u4e0d\u540c\u4e8e\u7801\u4e91\u5e10\u53f7\uff0c\u4e09\u6b21\u56de\u8f66\u5373\u53ef\u751f\u6210 ssh key<br \/>\n\u8bb0\u4e0b\u751f\u6210\u516c\u94a5\u7684\u76ee\u5f55\uff1aYour public key has been saved in \/home\/www\/.ssh\/id_rsa.pub.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cat \/home\/www\/.ssh\/id_rsa.pub<\/pre>\n<p>\u5f97\u5230\u4f60\u7684 public key\uff0c\u6dfb\u52a0\u5230\u7801\u4e91\uff0c SSH key\u6dfb\u52a0\u5730\u5740: \u4f60\u7684\u9879\u76ee\u5730\u5740\/deploy_keys \uff08\u6ce8\u610f\u8fd9\u91cc\u7528\u7684\u9879\u76ee\u90e8\u7f72\u516c\u94a5\uff0c\u53ea\u8bfb\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7533\u8bf7\u4e2a\u4eba\u516c\u94a5\uff0c\u53ef\u5199\uff09<br \/>\n\u7136\u540e\u6309\u5982\u4e0b\u547d\u4ee4\u9a8c\u8bc1\u662f\u5426\u751f\u6548\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo -u www ssh -T git@gitee.com<\/pre>\n<p>\u5982\u679c\u9047\u5230 Host Key Verification Failed\uff0c\u5c31\u6267\u884c\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo -u www ssh-keyscan -t rsa gitee.com &gt;&gt; \/home\/www\/.ssh\/known_hosts\r\nsudo -u www ssh-keyscan -t rsa 120.55.226.24 &gt;&gt; \/home\/www\/.ssh\/known_hosts<\/pre>\n<h1 id=\"clone\u514b\u9686\u4ee3\u7801\">Clone\u514b\u9686\u4ee3\u7801<\/h1>\n<p>\u521d\u6b21\u4f7f\u7528\uff0c\u5148\u514b\u9686\u4e00\u6b21\u4ee3\u7801\u5230\u7f51\u7ad9\u76ee\u5f55\u3002<br \/>\n\u7531\u4e8e\u521a\u624d\u662f\u9488\u5bf9www\u751f\u6210\u7684\u516c\u94a5\u548c\u79c1\u94a5\uff0c\u6240\u4ee5\u5728\u547d\u4ee4\u884c\u754c\u9762\u4e5f\u8981\u7528www\u7528\u6237\u6743\u9650\u6267\u884cgit\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo -u www git clone \u7801\u4e91\u4ed3\u5e93SSH\u5730\u5740  \/\u7f51\u7ad9\u6839\u76ee\u5f55<\/pre>\n<h1 id=\"pull\u62c9\u53d6\u4ee3\u7801\">Pull\u62c9\u53d6\u4ee3\u7801<\/h1>\n<p>reset\u53ea\u5f71\u54cd\u88abtrack\u8fc7\u7684\u6587\u4ef6, \u6240\u4ee5\u9700\u8981clean\u6765\u5220\u9664\u6ca1\u6709track\u8fc7\u7684\u6587\u4ef6<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo -u www git reset --hard &amp;&amp; git clean -f &amp;&amp; git pull 2&gt;&amp;1<\/pre>\n<p>\u7801\u4e91WebHook\u901a\u77e5\u670d\u52a1\u5668\u62c9\u53d6\u4ee3\u7801\uff1a<br \/>\n\u5173\u952e\u51fd\u6570\uff1ashell_exec<br \/>\n\u53c2\u8003\uff1a\u89e3\u51b3PHP\u8c03\u7528exec\u6216shell_exec\u7684\u95ee\u9898 https:\/\/blog.csdn.net\/Qiang1370373713\/article\/details\/84539990<\/p>\n<p>\u6ce8\u610f\uff1a\u5f00\u653eshell_exec\u8fd9\u4e2aphp\u51fd\u6570\u662f\u975e\u5e38\u5371\u9669\u7684\uff0c\u56e0\u6b64\u5207\u8bb0\u4e0d\u8981\u5728\u751f\u4ea7\u73af\u5883\u5f00\u653e\u8fd9\u4e2a\u51fd\u6570\uff0c\u66f4\u52a0\u4e0d\u80fd\u7528root\u6743\u9650\u53bb\u6267\u884cphp\u3002<br \/>\n\u5efa\u8bae\u5c06\u4ee5\u4e0b\u4ee3\u7801\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u914d\u7f6e\u7684\u6d4b\u8bd5\u7528\u7f51\u7ad9\u73af\u5883\uff0c\u9664log\u65e5\u5fd7\u6587\u4ef6\u4ee5\u5916\uff0c\u6b64\u73af\u5883\u4e0b\u6240\u6709\u6587\u4ef6\u7981\u6b62www\u7528\u6237\u7684\u5199\u5165\u548c\u4fee\u6539\u3002<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<br \/>\n\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300cLuke\u00b7Wong\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002<br \/>\n\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/Qiang1370373713\/article\/details\/87889009<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n \r\n\/**\r\n * GETEE HOOK \u6d4b\u8bd5\u670d\u52a1\u5668\u7aef\u4ee3\u7801\r\n *\r\n * @author   hb1707 &lt;hb1707@live.cn&gt;\r\n *\/\r\n \r\n \r\n$git = \"git\"; \/\/\u9ed8\u8ba4\u662f\u7528git\u5168\u5c40\u53d8\u91cf\uff0c\u6709\u7684\u73af\u5883\u53ef\u80fd\u8981\u6307\u660e\u5177\u4f53\u5b89\u88c5\u8def\u5f84\r\n$branch = \"\"; \/\/\u6307\u5b9apull\u5206\u652f\uff0c\u4e3a\u7a7a\u5c31\u662f\u9ed8\u8ba4\u5206\u652f\r\n$logName = \"git_data\"; \/\/\u672c\u5730\u65e5\u5fd7\u540d\u79f0\uff0c\u4e0e\u5f53\u524dphp\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\r\n$savePath = \"\/home\/wwwroot\/web\/\"; \/\/\u7f51\u7ad9\u6839\u76ee\u5f55\uff0c\u521d\u6b21\u514b\u9686\u786e\u4fdd\u76ee\u5f55\u4e3a\u7a7a\r\n$gitSSHPath  = \"git@gitee.com:xxx\/xxx.git\";\/\/\u4ee3\u7801\u4ed3\u5e93SSH\u5730\u5740\r\n$password = \"password\"; \/\/\u5728GITEE\u8bbe\u7f6e\u7684\u5bc6\u7801\r\n$is_test = false;\/\/\u6d4b\u8bd5\u6a21\u5f0f\uff0c\u65e0\u9700\u5bc6\u7801\uff1atrue\u6253\u5f00\uff0c\u5e73\u65f6false\u5173\u95ed\r\n$isCloned = true;\/\/\u8bbe\u7f6e\u662f\u5426\u5df2\u7ecfClone\u5230\u672c\u5730,true:\u5df2\u7ecfclone,\u76f4\u63a5pull,false:\u5148clone.\r\n \r\n\/\/\u5982\u679c\u5df2\u7ecfclone\u8fc7,\u5219\u76f4\u63a5\u62c9\u53bb\u4ee3\u7801\r\nif ($isCloned) {\r\n    $requestBody = file_get_contents(\"php:\/\/input\");\r\n    if (empty($requestBody) &amp;&amp; empty($is_test)) {\r\n        die('send fail');\r\n    }\r\n \r\n    \/\/\u89e3\u6790\u7801\u4e91\u53d1\u8fc7\u6765\u7684JSON\u4fe1\u606f\r\n    $content = json_decode($requestBody, true);\r\n    \/\/\u82e5\u662f\u4e3b\u5206\u652f\u4e14\u63d0\u4ea4\u6570\u5927\u4e8e0\r\n    \/\/\u5bc6\u7801\u8981\u6b63\u786e\r\n    if($content['password'] == $password || !empty($is_test)){\r\n        if($content['total_commits_count']&gt;0 || !empty($is_test)) {\r\n            if ($content['ref'] == \"refs\/heads\/$branch\" || !$branch || !empty($is_test)) {\r\n                $cmd = \"cd $savePath &amp;&amp; $git reset --hard &amp;&amp; $git clean -f &amp;&amp; $git pull $branch 2&gt;&amp;1\";\r\n                $result = shell_exec($cmd); \/\/\u5173\u952e\u547d\u4ee4\uff0c\u62c9\u53d6\u4ee3\u7801\uff0c2&gt;&amp;1\u540e\u53f0\u6267\u884c\r\n                $res_log = \"[ PULL START ]\" . PHP_EOL;\r\n                if(!empty($is_test)){\r\n                    $res_log .= date('Y-m-d H:i:s') . '\u6267\u884c\u6d4b\u8bd5\uff01'. PHP_EOL;\r\n                }else{\r\n                    $res_log .= date('Y-m-d H:i:s') . '\u5411' . $content['repository']['name'] . '\u9879\u76ee\u7684' . $content['ref'] . '\u5206\u652fpush\u4e86' . $content['total_commits_count'] . '\u4e2acommit\uff1a'. PHP_EOL;\r\n                }\r\n                $res_log .= $cmd. PHP_EOL;\r\n                $res_log .= $result. PHP_EOL;\r\n                $res_log .= \"[ PULL END ]\" . PHP_EOL;\r\n                $res_log .= PHP_EOL . PHP_EOL;\r\n                file_put_contents($logName.\".log\", $res_log, FILE_APPEND);\/\/\u5199\u5165\u65e5\u5fd7\r\n                echo $result;\r\n            }\r\n        }\r\n    } else {\r\n        file_put_contents($logName.\".log\", '\u5bc6\u7801\u9519\u8bef!', FILE_APPEND);\r\n        echo '\u5bc6\u7801\u9519\u8bef!';\r\n    }\r\n \r\n}else {\r\n    $res = \"[ CLONE START ]\".PHP_EOL;\r\n    $res .= shell_exec(\"$git clone $gitSSHPath $savePath\").PHP_EOL;\r\n    $res .= \"[ CLONE END ]\".PHP_EOL;\r\n    file_put_contents($logName.\".log\", $res, FILE_APPEND);\/\/\u5199\u5165\u65e5\u5fd7\r\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1630\" src=\"https:\/\/qiniu.shijitianchen.com\/2022\/03\/20220318085421616.png\" width=\"1112\" height=\"468\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git\u5b89\u88c5 yum install -y git git &#8211;version #\u7248\u672c rpm -qal |gr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1628,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[125,83,123,124],"class_list":["post-1627","post","type-post","status-publish","format-standard","hentry","category-linux","tag-centos7","tag-git","tag-webhook","tag-124"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1627"}],"version-history":[{"count":2,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1627\/revisions"}],"predecessor-version":[{"id":1631,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1627\/revisions\/1631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/media\/1628"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}