简体   繁体   English

Git:从存储库自动拉取?

[英]Git: auto pull from repository?

Is there any way to set up git such that it listens for updates from a remote repo and will pull whenever something changes?有什么方法可以设置 git 以便它监听来自远程仓库的更新并在发生变化时拉取? The use case is I want to deploy a web app using git (so I get version control of the deployed application) but want to put the "central" git repo on Github rather than on the web server (Github's interface is just soooo nice). The use case is I want to deploy a web app using git (so I get version control of the deployed application) but want to put the "central" git repo on Github rather than on the web server (Github's interface is just soooo nice) .

Has anyone gotten this working?有没有人得到这个工作? How does Heroku do it? Heroku 是如何做到的? My Google-fu is failing to give me any relevant results.我的 Google-fu 没有给我任何相关的结果。

Git has "hooks", actions that can be executed after other actions. Git 有“钩子”,可以在其他动作之后执行的动作。 What you seem to be looking for is "post-receive hook".您似乎正在寻找的是“接收后挂钩”。 In the github admin, you can set up a post-receive url that will be hit (with a payload containing data about what was just pushed) everytime somebody pushes to your repo.在 github 管理员中,您可以设置一个 post-receive url,每当有人推送到您的 repo 时,该 url 都会被命中(带有包含有关刚刚推送的内容的数据的有效负载)。

For what it's worth, I don't think auto-pull is a good idea -- what if something wrong was pushed to your branch ?就其价值而言,我认为自动拉取不是一个好主意 - 如果将错误推送到您的分支怎么办? I'd use a tool like capistrano (or an equivalent) for such things.我会使用像capistrano (或类似的)这样的工具来处理这些事情。

On unix-likes you can create cron job that calls "git pull" (every day or every week or whatever) on your machine.在类 Unix 上,您可以在您的机器上创建调用“git pull”(每天或每周或其他)的 cron 作业。 On windows you could use task scheduler or "AT" command to do the same thing.在 Windows 上,您可以使用任务调度程序或“AT”命令来做同样的事情。

There are continuous integrations programs like Jenkins or Bamboo, which can detect commits and trigger operations like build, test, package and deploy.有像 Jenkins 或 Bamboo 这样的持续集成程序,它们可以检测提交并触发构建、测试、打包和部署等操作。 They do what you want, but they are heavy with dependencies, hard to configure and in the end they may use periodical check against git repository, which would have same effect like calling git pull by cron every minute.他们做你想做的事,但他们有很多依赖,难以配置,最后他们可能会定期检查 git 存储库,这与每分钟通过 cron 调用 git pull 具有相同的效果。

You could write a script to achieve this by doing the following things: 您可以通过执行以下操作来编写脚本来实现此目的:

  1. get remote HEAD commit id 获取远程HEAD提交ID
  2. compare the id with local HEAD 将ID与本地HEAD进行比较
  3. if they're different, use git pull and use the deploy command 如果它们不同,请使用git pull并使用deploy命令
  4. if the same, pass 如果相同,则通过
  5. write a cron job to execute the script (NOTE : prevent duplicate executing.) 编写cron作业以执行脚本(注意:防止重复执行。)

我知道这个问题有点旧,但是您可以使用 Windows 日志和 git 使用 webhook 和 php 自动拉取您的项目(假设您的项目涉及网络服务器。请在此处查看我的要点: https : //gist.github.com/ upggr/a6d92e2808e9628ebe0d01fd93569f4a

As some have noticed after trying this, if you use php exec() , it turns out that solving for permissions is not that simple.正如一些人在尝试后注意到的那样,如果您使用 php exec() ,结果证明解决权限并不那么简单。

The user that will execute the command might not be your own, but www-data or apache .将执行命令的用户可能不是您自己的用户,而是www-dataapache

If you have root/sudo access, I recommend you read this Jonathan's blog post如果您有 root/sudo 访问权限,我建议您阅读这篇Jonathan 的博客文章

When you aren't allowed/can't solve permissions当您不被允许/无法解决权限时

My solution was a bit creative.我的解决方案有点创意。 I noticed I could create a script under my username with a loop and git pull would work fine.我注意到我可以在我的用户名下创建一个带有循环的脚本,并且 git pull 可以正常工作。 But that, as pointed out by others, bring the question of running a lot of useless git pull every, say, 60 seconds.但是,正如其他人所指出的那样,会带来每隔 60 秒运行大量无用git pull的问题。

So here the steps to a more delicate solution using webhooks :所以这里是使用webhooks的更精细解决方案的步骤:

  • deploy key : Go to your server and type: ssh-keygen -t rsa -b 4096 -C "deploy" to generate a new deploy key, no need write-permissions (read-only is safer).部署密钥:转到您的服务器并键入: ssh-keygen -t rsa -b 4096 -C "deploy"以生成新的部署密钥,不需要写权限(只读更安全)。 Copy the public key to your github repository settings, under "deploy key".将公钥复制到您的 github 存储库设置中的“部署密钥”下。
  • Webhook : Go to your repository settings and create a webhook. Webhook :转到您的存储库设置并创建一个 Webhook。 Lets assume the payload address is http://example.com/gitpull.php让我们假设有效负载地址是http://example.com/gitpull.php
  • Payload : create a php file with this code example bellow in it.有效负载:创建一个包含以下代码示例的 php 文件。 The purpose of the payload is not to git pull but to warn the following script that a pull is necessary.负载的目的不是git pull而是警告以下脚本需要pull Here the simple code:这里是简单的代码:

gitpull.php : gitpull.php :

<?php

/* Deploy (C) by DrBeco 2021-06-08 */

echo("<br />\n");
chdir('/home/user/www/example.com/repository'); 
touch(GITPULLMASTER);
?>
  • Script : create a script in your preferred folder, say, /home/user/gitpull.sh with the following code:脚本:在您的首选文件夹中创建一个脚本,例如/home/user/gitpull.sh使用以下代码:

gitpull.sh gitpull.sh

#!/bin/bash

cd /home/user/www/example.com/repository
while true ; do
      if [[ -f GITPULLMASTER ]] ; then
            git pull > gitpull.log 2>&1
            mv GITPULLMASTER GITPULLMASTER.`date +"%Y%m%d%H%M%S"`
      fi
      sleep 10
done
  • Detach : the last step is to run the script in detached mode, so you can log out and keep the script running in background.分离:最后一步是在分离模式下运行脚本,这样您就可以注销并保持脚本在后台运行。

There are 2 ways of doing that, the first is simpler and don't need screen software installed:有两种方法可以做到这一点,第一种更简单,不需要安装screen software

  • disown :否认

    • run ./gitpull.sh & to put it in background运行./gitpull.sh &将其置于后台
    • then type disown -h %1 to detach and you can log out然后键入disown -h %1以分离,您可以注销
  • screen :屏幕

    • run screen运行screen
    • run ./gitpull.sh运行./gitpull.sh
    • type control+ad to detach and you can log out键入control+ad以分离,您可以注销

Conclusion结论

This solution is simple and you avoid messing with keys, passwords, permissions, sudo, root, etc., and also you prevent the script to flood your server with useless git pull s.这个解决方案很简单,您可以避免弄乱密钥、密码、权限、sudo、root 等,还可以防止脚本用无用的git pull s 淹没您的服务器。

The way it works is that it checks if the file GITPULLMASTER exists;它的工作方式是检查文件GITPULLMASTER存在; if not, back to sleep.如果没有,就回去睡觉。 Only if it exists, then do a git pull .仅当它存在时,才执行git pull

You can change the line:您可以更改行:

mv GITPULLMASTER GITPULLMASTER. date +"%Y%m%d%H%M%S"`日期 +"%Y%m%d%H%M%S"`

to

rm GITPULLMASTER

if you prefer a cleaner directory.如果您更喜欢更干净的目录。 But I find it useful for debug to let the pull date registered (and untracked).但我发现让拉取日期注册(和未跟踪)对调试很有用。

For our on-premises Windows test servers, we use Windows Task Scheduler tasks, set to run every 3 minutes, pulling from Bitbucket Cloud to repositories on those servers.对于我们的本地 Windows 测试服务器,我们使用 Windows 任务计划程序任务,设置为每 3 分钟运行一次,从 Bitbucket 云拉到这些服务器上的存储库。 While not instantaneous, it meets our needs, and has proven to be reliable.虽然不是即时的,但它满足了我们的需求,并且已被证明是可靠的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM