<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[Gitee码云通过WebHooks实现自动同步代码部署]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Tue, 22 May 2018 06:59:38 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	码云(Gitee)的WebHooks功能,可以在我们每次提交代码后,向我们设定的地址post一个更新的json信息,这样我们就可以根据该信息,来自动拉去我们的代码,实现自动同步功能.<br/><br/>第一步 配置WebHooks<br/><br/><div class="code"><br/>在&#91;码云&#93;(https://gitee.com/)上,自己的项目中,选择&quot;管理&quot; --&gt; &quot;WebHooks&quot;,这个时候你能看到下图界面.<br/>注:1 URL填写为自己接收端的地址,配置后,每次代码更新后会通过该地址发送更新的消息.<br/>&nbsp;&nbsp; 2 密码:可以不填写.<br/></div><br/><br/><a href="http://www.dzhope.com/attachment.php?fid=140" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=140" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>第二步 服务器脚本配置<br/><br/><div class="code"><br/>在保存好脚本文件后,注意一下事项:<br/>1 该脚本必须在PHP环境中运行,其他环境根据情况修改.<br/>2 需要将该文件访问地址,替换到第一步的URL中.<br/>3 根据实际情况,修改脚本中的参数:savePath,gitPath,email ,name.<br/>4 isClone 变量,第一次clone的时候,需要设置成false,已经clone过后,需要设置成true.<br/></div><br/><br/><div class="code"><br/>&lt;?php<br/><br/>//git webhook 自动部署脚本<br/>//项目存放物理路径,第一次clone时,必须保证该目录为空<br/>$savePath = &quot;/www/wwwroot/eia/server/&quot;;<br/>$gitPath&nbsp;&nbsp;= &quot;https://gitee.com/jerry_hui/tools.git&quot;;//代码仓库<br/>$email = &quot;your email&quot;;//用户仓库邮箱<br/>$name&nbsp;&nbsp;= &quot;your name&quot;;//仓库用户名,一般和邮箱一致即可<br/><br/>$isClone = false;//设置是否已经Clone到本地,true:已经clone,直接pull,false:先clone.<br/><br/>//如果已经clone过,则直接拉去代码<br/>if ($isClone) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$requestBody = file_get_contents(&quot;php://input&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (empty($requestBody)) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&#039;send fail&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;//解析Git服务器通知过来的JSON信息<br/>&nbsp;&nbsp;&nbsp;&nbsp;$content = json_decode($requestBody, true);<br/>&nbsp;&nbsp;&nbsp;&nbsp;//若是主分支且提交数大于0<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ($content&#91;&#039;ref&#039;&#93;==&#039;refs/heads/master&#039; &amp;&amp; $content&#91;&#039;total_commits_count&#039;&#93;&gt;0) &#123;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res = PHP_EOL.&quot;pull start --------&quot;.PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res .= shell_exec(&quot;cd &#123;$savePath&#125; &amp;&amp; git pull &#123;$gitPath&#125;&quot;);//拉去代码<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res_log = &#039;-------------------------&#039;.PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res_log .= $content&#91;&#039;user_name&#039;&#93; . &#039; 在&#039; . date(&#039;Y-m-d H:i:s&#039;) . &#039;向&#039; . $content&#91;&#039;repository&#039;&#93;&#91;&#039;name&#039;&#93; . &#039;项目的&#039; . $content&#91;&#039;ref&#039;&#93; . &#039;分支push了&#039; . $content&#91;&#039;total_commits_count&#039;&#93; . &#039;个commit：&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res_log .= $res.PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res_log .= &quot;pull end --------&quot;.PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file_put_contents(&quot;git-webhook_log.txt&quot;, $res_log, FILE_APPEND);//写入日志到log文件中<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;else &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$res = &quot;clone start --------&quot;.PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;//注:在这里需要设置用户邮箱和用户名,不然后面无法拉去代码<br/>&nbsp;&nbsp;&nbsp;&nbsp;$res .= shell_exec(&quot;git config --global user.email &#123;$email&#125;&#125;&quot;).PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$res .= shell_exec(&quot;git config --global user.name &#123;$name&#125;&#125;&quot;).PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$res .= shell_exec(&quot;git clone &#123;$gitPath&#125; &#123;$savePath&#125;&quot;).PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$res .= &quot;clone end --------&quot;.PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;file_put_contents(&quot;git-webhook_log.txt&quot;, $res, FILE_APPEND);//写入日志到log文件中<br/>&#125;<br/></div><br/><br/>注意事项<br/>如果仓库是公开的,那么上述配置则不需要修改,因为公开的仓库,在pull的时候,不需要账户和密码.<br/>如果仓库是私有的,那么还需要做一下配置: <br/>Windows配置: <br/>1 进入 C:&#92;Documents and Settings&#92;xxx.,找到.git-credentials文件, <br/>touch .git-credentials<br/><br/>2 用记事本修改.git-credentials.格式如下: <br/><a href="https://&#123;" target="_blank">https://&#123;</a> username &#125;:&#123; password &#125;@ xxx .com <br/>eg:<a href="https://zhangsan:123456@gitee.com" target="_blank">https://zhangsan:123456@gitee.com</a><br/><br/>3 在任意目录 打开git-bash，输入: <br/>git config –global credential.helper store<br/><br/>4 执行完后去查看 C:&#92;Documents and Settings&#92;Administrator.gitconfig 这个文件，发现多了一项： <br/>[credential] helper = store<br/><br/>5 重新使用git pull.此时便不再需要输入密码.<br/><br/>Linux配置: <br/>1 进入 /home/chinaestone(有些在root下面),找到.git-credentials文件,<br/><br/>2 输入内容如下: <br/><a href="https://&#123;username&#125;:&#123;password&#125;@github.com" target="_blank">https://&#123;username&#125;:&#123;password&#125;@github.com</a> <br/>eg:<a href="https://zhangsan:123456@gitee.com" target="_blank">https://zhangsan:123456@gitee.com</a><br/><br/>3 保存文件退出后,输入以下指令: <br/>git config –global credential.helper store<br/><br/>4 此时在/home/chinaestone/.gitconfig 会新增一项 <br/>helper = store<br/><br/>5 重新使用git pull.此时便不再需要输入密码.<br/>Tags - <a href="http://www.dzhope.com/tags/git/" rel="tag">git</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] Gitee码云通过WebHooks实现自动同步代码部署]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>