Home  >  Article  >  Backend Development  >  使用git HOOK自动部署git命令执行不成功

使用git HOOK自动部署git命令执行不成功

WBOY
WBOYOriginal
2016-06-06 20:36:251379browse

git@OSC上,使用PUSH钩子,每次push就通过URL访问项目下面的自动部署php文件

<code>
header("Content-type: text/html; charset=utf-8");
$js=json_decode($_REQUEST["hook"]);//json转换
if($js->password!="xxxxxx")die("ERROR!");//判断密码
$fp=fopen("./log.txt",'a');
$lastcommit=$js->push_data->commits[count($js->push_data->commits)-1];//获取最后的commit
if(strstr($lastcommit->message,"release"))//这里意为:如果最后的commit包含"release"则进行自动发布。
{
exec('cd /home/xxx/');//进入目录
exec("git pull origin master");//进行git拉取,前提是使用了ssh
fwrite($fp,"!!!".date('Y-m-d H:i:s')."\t".$lastcommit->message."\t".$lastcommit->author->name."\t"."Y"."\n");//进行记录
}
else
{
fwrite($fp,date('Y-m-d H:i:s')."\t".$lastcommit->message."\t".$lastcommit->author->name."\t"."N"."\n");
}
</code>

每次push后,项目下面的log.text确实有记录,但是代码没更新,还是得每次都手动git pull一下。这该如何是好啊?

回复内容:

git@OSC上,使用PUSH钩子,每次push就通过URL访问项目下面的自动部署php文件

<code>
header("Content-type: text/html; charset=utf-8");
$js=json_decode($_REQUEST["hook"]);//json转换
if($js->password!="xxxxxx")die("ERROR!");//判断密码
$fp=fopen("./log.txt",'a');
$lastcommit=$js->push_data->commits[count($js->push_data->commits)-1];//获取最后的commit
if(strstr($lastcommit->message,"release"))//这里意为:如果最后的commit包含"release"则进行自动发布。
{
exec('cd /home/xxx/');//进入目录
exec("git pull origin master");//进行git拉取,前提是使用了ssh
fwrite($fp,"!!!".date('Y-m-d H:i:s')."\t".$lastcommit->message."\t".$lastcommit->author->name."\t"."Y"."\n");//进行记录
}
else
{
fwrite($fp,date('Y-m-d H:i:s')."\t".$lastcommit->message."\t".$lastcommit->author->name."\t"."N"."\n");
}
</code>

每次push后,项目下面的log.text确实有记录,但是代码没更新,还是得每次都手动git pull一下。这该如何是好啊?

<code>exec('cd /home/xxx/');//进入目录
exec("git pull origin master");//进行git拉取,前提是使用了ssh
</code>

在一个exec连在一起写吧,cd不会修改父进程所在目录,另外最好把输出重定向到文件方便排查。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn