当同一分支上有新的提交时,自动将本地git存储库与远程存储库同步

时间:2015-10-29 19:29:00

标签: git github webhooks

我在亚马逊ec2实例上克隆了一个git存储库。我想这样做,以便当对远程存储库上的同一分支进行新的提交时,ec2实例上的本地存储库总是自动同步(可能使用git pull command)。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

如果您希望让服务器同步到服务器所在分支上的最新更改,则可以按照此处的步骤操作。提交或拉取请求时,将会发出“Git pull”。

将此添加到您的代码库中以触发

// This function serves as the Payload URL for Github's webhook
// https://github.com/reponame/projectname/settings/hooks/
public function post_receive() 
{
    // copied from 'https://gist.github.com/1809044'
    $commands = array(
        'hostname',
        'echo $PWD',
        'whoami',
        'git fetch',
        'branch=$(git symbolic-ref -q --short HEAD)',
        'git reset --hard origin/$branch',
        'git pull',
        'git status'
    );

    // Run the commands for output
    $output = '';
    foreach($commands AS $command){
        // Run it
        $tmp = shell_exec($command);
        // Output
        $output .= "<span style=\"color: #6BE234;\">\$</span> <span style=\"color: #729FCF;\">{$command}\n</span>";
        $output .= htmlentities(trim($tmp)) . PHP_EOL.'<br/>';
    }

    print_r($output);

}

转到Github.com上的Webhooks和服务,https://github.com/reponame/projectname/settings/hooks

点击“添加Webhook”

在“Payload URL”中,指定要启用自动“git pull”的服务器的URL,例如

https://yourserver.com/github/post_receive”。我们需要确保URL是https(安全),否则我们将获得302

或者,点击“禁用SSL验证”

对于“您想要触发此webhook的哪些事件?”,请选择“让我选择单个事件”。并选择“拉取请求”和“推送”

点击“添加webhook”

在“最近发送”中,确认您获得了正确的响应并且它是绿色的。

Webhook Post请求会在您的服务器上触发以下命令,

在Apache服务器上设置www-data组权限

sudo apt-get install members

members www-data

adduser username www-data

chown www-data:www-data -R username

cp -R /home/username/.ssh /var/www/.ssh

chown www-data:www-data -R /var/www/.ssh

ls -la /var/www/.ssh

sudo chmod g+w .git -R
相关问题