经过php 执行git pull 自动部署

安装git,生成公钥,加公钥 都有教程,须要注意的是如下几个方面php

php以www用户执行,须要给到权限才能执行git pullhtml

用合适的工具干合适的活,php去操做git pull只是在测试环境图方便搞的,若是正式环境,应该考虑用其余方式,例如系统定时任务设置触发条件等。git

一、vim /etc/sudoers  shell

root下加一行json

www localhost=(ALL) NOPASSWD:/usr/bin/git (安装git后目录用which git查看)vim

二、容许php执行exec shell_execssh

三、改目录权限工具

四、.git改权限可写,我直接改为777了测试

五、.git/config https地址改为ssh的spa

<?
header("Content-type: text/html; charset=utf-8");
$js = json_decode($_REQUEST["hook"]); // json转换
if ($js->password != "password") {
    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 ./"); // 进入目录
    exec("git pull origin master"); // 进行git拉取,前提是使用了ssh
    fwrite($fp, "※" . date('Y-m-d H:i:s') . "\t" . $lastcommit->message . "\t" . $lastcommit->author->name . "\n"); // 进行记录
} else {
    fwrite($fp, date('Y-m-d H:i:s') . "\t" . $lastcommit->message . "\t" . $lastcommit->author->name . "\n");
}
相关文章
相关标签/搜索