简体   繁体   English

你如何在远程服务器上进行自动 git pull?

[英]How do you do an automatic git pull on remote server?

Before I begin, I know there are a lot of questions similar to this one, but I am really having difficulty finding a concise, secure, best practice since the feedback on them has been so widely varied.在我开始之前,我知道有很多与这个问题类似的问题,但我真的很难找到一个简洁、安全、最佳的实践,因为对它们的反馈是如此广泛。

What I want to do:我想做的事:

  1. Complete work on my local machine on a development branch.在开发分支的本地机器上完成工作。
  2. Push changes to git.将更改推送到 git。 Git posts to a webhook URL and automatically has my remote server pull the changes on a development site. Git 发布到 webhook URL 并自动让我的远程服务器在开发站点上提取更改。
  3. Once QA'd and confirmed to be proper on the development site, push the master branch to the production site (on the same server as the development site).在开发站点上通过 QA 并确认正确后,将主分支推送到生产站点(与开发站点在同一台服务器上)。

Where I am at:我在哪里:

I have git installed on my local machine and the remote server.我在本地机器和远程服务器上安装了 git。 I can push mods to the development branch to git.我可以将 mod 推送到开发分支到 git。 On the remote server, I can pull the updates and it works like a charm.在远程服务器上,我可以拉取更新,它就像一个魅力。 The problem is that I cannot get the remote server to automatically update when changes are pushed from my local machine.问题是当从我的本地机器推送更改时,我无法让远程服务器自动更新。

My questions are:我的问题是:

  1. For the remote server development site directory, should I git init or git init --bare ?对于远程服务器开发站点目录,我应该git init还是git init --bare I don't plan on having updates made on the server itself.我不打算在服务器本身上进行更新。 I would like my dev team to work locally and push mods to the server.我希望我的开发团队在本地工作并将 mod 推送到服务器。 I believe I need to use git init as the working tree is needed to set-up a remote alias to the git repository, but I wanted to confirm.我相信我需要使用 git init 因为需要工作树来设置 git 存储库的远程别名,但我想确认一下。
  2. I am pretty sure the webhook post from git issue is due to user privileges.我很确定来自 git 问题的 webhook 帖子是由于用户权限造成的。 How can I safely get around this?我怎样才能安全地解决这个问题? I have read many tutorials that suggest updating git hook files, but I feel as though that is more drastic of a measure than I need to take.我已经阅读了许多建议更新 git hook 文件的教程,但我觉得这比我需要采取的措施更为严厉。 I would love to be able to have the webhook hit a URL that safely pulls the files without adding a boatload of code (if it is possible).我希望能够让 webhook 访问一个安全地拉取文件的 URL,而无需添加大量代码(如果可能的话)。

I am a web developer by nature, so git and sysadmin tasks are generally the bane of my existence.我本质上是一名 Web 开发人员,所以 git 和 sysadmin 任务通常是我存在的祸根。 Again, I know this question is similar to others, but I have yet to find a comprehensive, concise, secure, and most logical approach to resolving the issue.同样,我知道这个问题与其他问题类似,但我还没有找到一种全面、简洁、安全且最合乎逻辑的方法来解决这个问题。 I am about 16 hours in and have officially hit the "going in circles with no progress" point.我已经大约 16 个小时了,并且已经正式达到了“无进展地兜圈子”的点。

You can do this quite easily with GitHub service hooks.您可以使用 GitHub 服务挂钩轻松完成此操作。

You ll need to create one more file that will handle the process of performing the git pull.您需要再创建一个文件来处理执行 git pull 的过程。 Add a new file, called github.php (or anything you wish ), and add:添加一个名为 github.php 的新文件(或任何您希望的文件),然后添加:

<?php `git pull`;

Save that file, and upload it to the repository directory on your server.保存该文件,并将其上传到服务器上的存储库目录。 Then, go to Services Hooks -> Post-Receive URL and copy the URL to that file, and paste it into the “Post-Receive URL” Eg http://demo.test.com/myfolder/github.php然后,转到Services Hooks -> Post-Receive URL并将Services Hooks -> Post-Receive URL复制到该文件,并将其粘贴到“Post-Receive URL”中,例如http://demo.test.com/myfolder/github.php

So, when you push, GitHub will automatically visit this URL, thus causing your server to perform a git pull .所以,当你推送时,GitHub 会自动访问这个 URL,从而导致你的服务器执行git pull

To see this in more details to go to this tutorial要查看更多详细信息,请转到本教程

I had the same exact issue, strong in code and development skills, weak in sysadmin skills.我有同样的问题,代码和开发技能强,系统管理员技能弱。 When I was finally ready to push code I had to ask a GitHub rep what their suggested method was, and they responded with Capistrano.当我终于准备好推送代码时,我不得不询问GitHub代表他们建议的方法是什么,他们回复了 Capistrano。 It's a Ruby application that runs commands (such as git pull) on remote servers, along with pretty much any other command you can imagine.它是一个Ruby应用程序,可以在远程服务器上运行命令(例如 git pull),以及您可以想象的几乎任何其他命令。

Here a few articles you can read to get more info:您可以阅读以下几篇文章以获取更多信息:

Not going to lie, the learning curve was pretty steep, but once you start working with Capistrano, you will see that it works well for pushing code.不会撒谎,学习曲线非常陡峭,但是一旦您开始使用 Capistrano,您就会发现它非常适合推送代码。 I develop my applications in Symfony and I have my Capistrano set-up to pull code, clear cache and clear log files, all in one command from my local machine.我在 Symfony 中开发我的应用程序,并且我有我的 Capistrano 设置来提取代码、清除缓存和清除日志文件,所有这些都来自我本地机器的一个命令。

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

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