【问题标题】:Git deploy script make it show a responseGit部署脚本使其显示响应
【发布时间】: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


    【解决方案1】:

    无论如何我可以让下面的脚本给出一个响应代码,比如 200 等。

    header("HTTP/1.1 200 OK");
    

    会发出200 OK,但这并不意味着代码会被拉到服务器上。

    Github 钩子不会推送到服务器,钩子只是“ping”你的服务器,然后你从那里运行一些东西来执行来自 Github 的新代码的pull

    检查您的pull 代码。

    IMO bash 脚本有时可以更直接地处理此类内容。

    【讨论】:

    猜你喜欢
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2023-03-29
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多