<?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[利用码云webhook自动部署脚本]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Sun, 15 Jul 2018 15:45:50 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	整体思路：<br/><br/>git提交本地代码-----码云----自动部署到测试服务器，这样就可以即时看到项目演示效果。<br/><br/><strong>第一步：</strong><br/><br/><strong>测试服务器检查git版本</strong><br/><br/>centos自带得git版本是1.7.*的，在克隆的时候有bug，如果是1.7.0请升级至2.7.0+。<br/><br/><div class="code"><br/># 版本测试<br/>git --version<br/></div><br/><br/>升级git，不需要的请略过<br/><br/>Development tools 没装的要装上，不然GCC编译时会出错<br/><div class="code"><br/>yum -y groupinstall Development tools<br/></div><br/>GCC 用于编译安装包<br/><div class="code"><br/>yum install gcc perl-ExtUtils-MakeMaker<br/></div><br/>再卸载CentOS自带的老版本git<br/><div class="code"><br/>yum -y remove git<br/></div><br/>下载git并安装<br/><div class="code"><br/>cd /usr/src<br/>wget https://www.kernel.org/pub/software/scm/git/git-2.12.0.tar.gz<br/>tar xzf git-2.12.0.tar.gz<br/></div><br/><br/>安装注意事项：<br/><br/><div class="code"><br/>cd git-2.12.0<br/>make prefix=/usr/local/git all<br/></div><br/><br/>此时报错<br/><div class="code"><br/>/usr/src/git-2.12.0/utf8.c:463：对‘libiconv’未定义的引用<br/>libgit.a(utf8.o)：在函数‘reencode_string_len’中：<br/>/usr/src/git-2.12.0/utf8.c:524：对‘libiconv_open’未定义的引用<br/>/usr/src/git-2.12.0/utf8.c:535：对‘libiconv_close’未定义的引用<br/>/usr/src/git-2.12.0/utf8.c:529：对‘libiconv_open’未定义的引用<br/>collect2: 错误：ld 返回 1<br/>make: *** &#91;git-credential-store&#93; 错误 1<br/></div><br/><br/>原方案：<br/><div class="code"><br/>make prefix=/usr/local/git all<br/>make prefix=/usr/local/git install<br/></div><br/><br/>解决方案：<br/><br/>可替换为<br/><div class="code"><br/> ./configure --without-iconv<br/>make CFLAGS=-liconv prefix=/usr/local/git all<br/>make CFLAGS=-liconv prefix=/usr/local/git install<br/></div><br/><br/>最后将git加入环境变量<br/><div class="code"><br/>echo &quot;export PATH=$PATH:/usr/local/git/bin&quot; &gt;&gt; /etc/bashrc<br/>source /etc/bashrc<br/></div><br/><br/>大功告成！查看一下git的版本<br/><div class="code"><br/>git --version<br/>&gt;&gt; git version 2.12.0<br/></div><br/><br/>在完成升级后一定要记住git的文件路径，按照如上的教程git的最终路径为/usr/local/git/bin，这个在后面配置有用到。<br/><br/><br/><strong>第二步：</strong><br/><br/><strong>打开PHP禁用函数</strong><br/><br/>在这个自动部署程序中主要用到的函数为shell_exec()，但是在默认情况下是被禁止的，需要去php.ini中修改配置，具体做法如下：<br/><br/><div class="code"><br/>1、找到php.ini文件<br/>2、查询disable_function<br/>3、删除shell_exec,<br/>4、保存<br/>5、重启PHP<br/></div><br/><br/><strong>第三步：</strong><br/><br/><strong>配置WWW用户</strong><br/><br/>首先要检查www是否有权限，打开/etc/passwd，查看www用户，如果是/sbin/nologin需要改为/bin/bash<br/><br/>第一次配置成功也是采用的公钥，是在切换到www用户下创建的，可是在码云那，再提交相同的公钥，提示已经存在，可是，如果我们项目很多的话，每次都创建新的公钥也很麻烦，于是就放弃在www下创建公钥的思路，但是www的权限，仍然需要改。<br/><br/><br/><strong>第四步：</strong><br/><br/><strong>配置webhook脚本</strong><br/><br/>webhook脚本如下：<br/><br/><div class="code"><br/>&lt;?php<br/><br/>$savePath = &quot;网站根目录&quot;;<br/>$gitPath&nbsp;&nbsp;= &quot;代码仓库&quot;;//采用带密码的方式，例如：https://user:password@gitee.com/zxzllyj/sample-project.git<br/>$email = &quot;用户仓库邮箱&quot;;<br/>$name&nbsp;&nbsp;= &quot;仓库用户名&quot;;<br/>$git = &quot;GIT全局路径&quot;;<br/>$password = &quot;在GITEE设置的密码&quot;;<br/>$branch = &quot;你需要pull的分支&quot;;<br/>$logName = &quot;LOG名称&quot;;<br/><br/><br/>file_put_contents(&quot;$logName.log&quot;, PHP_EOL.date(&#039;Y-m-d H:i:s&#039;, time()).&quot;: &quot;.PHP_EOL, FILE_APPEND);<br/>$requestBody = file_get_contents(&quot;php://input&quot;);<br/>if (empty($requestBody)) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;file_put_contents(&quot;$logName.log&quot;, &quot;FAILED&quot;.PHP_EOL, FILE_APPEND);<br/>&nbsp;&nbsp;&nbsp;&nbsp; die(&#039;send fail&#039;);<br/>&#125;<br/>file_put_contents(&quot;$logName.log&quot;, $requestBody, FILE_APPEND);<br/>$content = json_decode($requestBody, true);<br/>if($content&#91;&#039;password&#039;&#93; == $password)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ($content&#91;&#039;ref&#039;&#93;==&quot;refs/heads/$branch&quot;) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = shell_exec(&quot;cd $savePath &amp;&amp; $git clean -f &amp;&amp; $git pull origin $branch 2&gt;&amp;1&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res_log = &quot;&#91; PULL START &#93;&quot;.PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res_log .= $result;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res_log .= PHP_EOL . PHP_EOL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file_put_contents(&quot;$logName.log&quot;, $res_log, FILE_APPEND);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $result;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125; else &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;file_put_contents(&quot;$logName.log&quot;, &#039;Password is Incorrect!&#039;, FILE_APPEND);<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;Password is Incorrect!&#039;;<br/>&#125;<br/></div><br/><br/><strong>第五步：</strong><br/><br/><strong>克隆项目</strong><br/><br/>在要部署的目录下进行克隆。<br/><br/><div class="code"><br/><br/>git clone 项目地址 /data/websitel/<br/><br/></div><br/>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] 利用码云webhook自动部署脚本]]></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>