为了解决自动部署问题,查询了一些解决方案后,为了稳定最终决定使用Git@OSC来实现该需求。
deploy.php
<?php header("Content-type: text/html; charset=utf-8"); if (! isset($_REQUEST['hook'])) die ('非法请求'); $config = require('config.php'); //hook内容详见http://git.oschina.net/oschina/git-osc/wikis/HOOK%E9%92%A9%E5%AD%90 $hook = json_decode($_REQUEST["hook"], true); //$hook = json_decode(file_get_contents('request.json'), true); $project = $hook['push_data']['repository']['name']; //判断密码 if ($hook['password'] != $config['projects'][$project]['password']) die ("密码错误"); //判断branch if (trim(strrchr($hook['push_data']['ref'], '/'), '/') != $config['projects'][$project]['branch']) die ("非自动部署分支"); $shell = <<<EOF WEB_PATH='{$config['projects'][$hook['push_data']['repository']['name']]['web_path']}' WEB_USER='{$config['web_user']}' WEB_GROUP='{$config['web_group']}' echo "Start deployment" cd \$WEB_PATH echo "pulling source code..." git reset --hard origin/master git clean -f git pull git checkout master echo "changing permissions..." chown -R \$WEB_USER:\$WEB_GROUP \$WEB_PATH echo "Finished." EOF; file_put_contents('deploy.sh', $shell); $res = shell_exec("bash deploy.sh"); $log_file = "{$project}.log"; foreach ($hook['push_data']['commits'] as $commit) { file_put_contents($log_file, "※" . date('Y-m-d H:i:s') . "\t" . $hook['push_data']['repository']['name'] . "\t" . $commit['message'] . "\t" . $commit['author']['name'] . PHP_EOL, FILE_APPEND ); } file_put_contents($log_file, $res . PHP_EOL, FILE_APPEND);
config.php
<?php return [ 'web_user' => 'www', 'web_group' => 'www', 'projects' => [ 'project' => [ 'password' => 'password', 'web_path' => '/home/wwwroot/default/project', ], ] ];