Git Hook - 在我推送到github后让服务器拉动 [英] Git Hook - Make server pull after I push to github

查看:81
本文介绍了Git Hook - 在我推送到github后让服务器拉动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地存储库,并且在我的实时服务器上建立了另一个存储库:



我想要实现的是非常简单的:在我推送到GitHub之后,我需要在 www.site.com/projects/ProjectA 到PULL - 因此更新了我的客户可以看到的项目的实时版本。



我一直在读关于钩子的吨,我找不到一个我需要的简单例子。所有教程都是针对高级功能而制作的。



我所做的


  1. 我已完成SSH访问设置。我有一个远程存储库,位于 www.site.com/projects/ProjectA 我使用Putty登录并执行git clone(此时我的本地repo,GitHub repo和服务器repo全部同步)创建了它们。
  2. 我将.git / hooks中的post-receive.sample复制到 www.site.com/projects/ProjectA /


  3. 我重命名post-receive .sample to post-receive

  4. 在github中,在回购设置/ webhooks下,我创建了一个新的钩子,指向 www.site.com/projects/ProjectA/post-receive 即可。所有其他设置保持不变。

我做了本地更改,提交和推送。 Hub更新,挂钩显示最近一次新的传送。一切正常,但服务器存储库不会更新。



hook代码是./ usr / share / git-core / contrib / hooks / post-收到电子邮件,我不明白这是什么。我查看了其他钩子示例,并且我看到其中一些具有熟悉的命令,如: exec git update-server-info ,所以我想我可以编写自己的命令。所以我写了 git pull ,保存了它并做了更改 - 提交 - 再次从本地回购中推送。结果和以前一样。然后我尝试了 exec git pull 。同样的事情。

我的问题是 - 我做错了什么,第二个问题是,为什么heck没有这个功能的简单教程,因为这似乎是最常见的场景之一。谢谢!

解决方案



Webhooks要求您在服务器上设置HTTP POST侦听器。您可以在您的项目中添加一个http路由并执行适当的pull操作。



参考: Webhooks



我在项目中使用了一个替代实现:

假设〜/ example是你的项目public / www文件夹。
ssh到你的服务器:

  $ cd〜/ example&& mkdir .git&& cd .git&& git init --bare 
$ cat>挂钩/后接收<< EOF
> #!/ bin / sh
> GIT_WORK_TREE =〜/ example git checkout -f
> EOF
$ chmod + x hooks / post-receive

以上将创建一个纯粹的git回购在〜/ example / .git 文件夹中。

在本地存储库中:



<$ p $添加一个post-receive可执行的钩子并在示例目录中执行checkout。

p> $ git remote add server ssh://my_user@my_server.com:/absolute_path/example/.git/
$ git push server + master:refs / heads / master

这对我很好。只要我需要,我就可以推送,还原提交。



参考:服务器存储库


I have a local repository, and I have set up another repository on my live server:

www.site.com/projects/ProjectA

What I want to achieve is very simple: After I PUSH to GitHub, I want the repository at www.site.com/projects/ProjectA to PULL - hence update the live version of the project, that my client can see.

I've been reading tons about hooks and I cannot find a very simple example for what I need. All tutorials are made for advanced functionality.

What I have done

  1. I have the SSH access settings done.
  2. I have a remote repository at www.site.com/projects/ProjectA that I created while logged in with Putty and doing git clone (at this point my local repo, the GitHub repo and the server repo were all in sync)

  3. I copied post-receive.sample from .git/hooks to www.site.com/projects/ProjectA/

  4. I renamed post-receive.sample to post-receive
  5. In github, under the repo settings / webhooks I created a new hook that points to www.site.com/projects/ProjectA/post-receive. All other settings I left intact.

I made a local change, commit, push. The Hub updates, the hook shows a new recent delivery. All is well, but the server repository does not update.

The "hook" code is ". /usr/share/git-core/contrib/hooks/post-receive-email" and I don't understand what that does. I looked at other hook samples and I saw some of them have familiar commands like: exec git update-server-info so I figured I could write my own command. so I wrote git pull, saved it and did the change - commit - push again from my local repo. The result was the same as before. I then tried exec git pull. Same thing.

My question is - what am I doing wrong, and the second question is, why the heck isn't there a simple tutorial for this functionality, since this seems to be one of the most usual scenarios. Thank you!

解决方案

Just saw this unanswered even after years.

Webhooks require you to setup a HTTP POST listener on your server. You can add a http route in your project and perform appropriate pull action.

Reference: Webhooks

There is an alternate implementation which I use in my projects:

Suppose ~/example is your project public/www folder. Ssh into your server:

$ cd ~/example && mkdir .git && cd .git && git init --bare
$ cat > hooks/post-receive << EOF
> #!/bin/sh
> GIT_WORK_TREE=~/example git checkout -f
> EOF
$ chmod +x hooks/post-receive

The above will create a bare git repo in ~/example/.git folder. Add a post-receive executable hook and perform checkout into the example directory.

On local repo:

$ git remote add server ssh://my_user@my_server.com:/absolute_path/example/.git/
$ git push server +master:refs/heads/master

This works well for me. I can push, revert commits whenever I need.

Reference: Server repository

这篇关于Git Hook - 在我推送到github后让服务器拉动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆