【发布时间】:2014-03-28 10:18:14
【问题描述】:
我正在使用下面的脚本将我的 CI 站点从 GIT 自动部署到登台服务器。
我的问题是我在 GIT 方面是个菜鸟,当我推送到存储库时,代码似乎没有推送到服务器。
无论如何我可以让下面的脚本给出一个响应代码,比如 200 等。git hub 上的钩子设置设置为使用 JSON。
脚本:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Post-Receive Hooks API
*
* @author appleboy
* @copyright 2012 appleboy
* @link https://github.com/appleboy/PHP-Git-Deploy
* @package CodeIgniter Git Deploy
*/
class Deploy extends CI_Controller
{
/**
* Default __construct
*
* @author appleboy
*/
public function __construct()
{
parent::__construct();
$this->config->load('github');
}
/**
*
* Post-Receive Hooks
*
* @author appleboy
*/
public function receive()
{
// receive git payload
$payload = $this->input->get_post('payload');
// load git command path config
$git_config = $this->config->item('github');
$git_path = $this->config->item('git_path');
if ($payload and is_array($git_config)) {
$payload = json_decode($payload);
log_message('debug', 'Post-receive hook initial');
foreach ($git_config as $key => $val) {
// check repository name exist in config
$repository_name = strtolower($payload->repository->name);
if ($repository_name != strtolower($key)) {
continue;
}
foreach ($val as $k => $v) {
// check if match payload ref branch
$head = 'refs/heads/' . $k;
if ($payload->ref != $head) {
continue;
}
// git reset head and pull origin branch
if (isset($v['base_path']) and !empty($v['base_path'])) {
$base_path = realpath($v['base_path']) . '/';
$shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" reset --hard HEAD',
$git_path, $base_path, $base_path);
log_message('debug', '$shell value ' . $shell);
$output = shell_exec(escapeshellcmd($shell));
$shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" clean -f',
$git_path, $base_path, $base_path);
log_message('debug', '$shell value ' . $shell);
$output = shell_exec(escapeshellcmd($shell));
$shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" pull origin %s',
$git_path, $base_path, $base_path, $k);
log_message('debug', '$shell value ' . $shell);
$output = shell_exec(escapeshellcmd($shell));
}
}
}
}
}
}
【问题讨论】:
标签: php git codeigniter github