Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do an automatic git pull on remote server?

Tags:

git

php

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 posts to a webhook URL and automatically has my remote server pull the changes on a development site.
  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).

Where I am at:

I have git installed on my local machine and the remote server. I can push mods to the development branch to 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? 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. 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.
  2. I am pretty sure the webhook post from git issue is due to user privileges. 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. 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).

I am a web developer by nature, so git and sysadmin tasks are generally the bane of my existence. 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.

like image 393
MrC Avatar asked May 10 '12 21:05

MrC


People also ask

How do I pull a remote git repository?

The content of the multiple remote repositories can be pulled to the local drive by using the command, `git pull origin` or `git pull upstream`.

How do I push a git repository to a remote server?

In order to push a Git branch to remote, you need to execute the “git push” command and specify the remote as well as the branch name to be pushed. If you are not already on the branch that you want to push, you can execute the “git checkout” command to switch to your branch.

Does git auto pull?

In short, git-auto-pull is a little javascript utility to automatically do a 'git pull' on a server when something new is pushed to the git repository.

Does git fetch affect remote?

git fetch really only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. Fetch is great for getting a fresh view on all the things that happened in a remote repository.


2 Answers

You can do this quite easily with GitHub service hooks.

You ll need to create one more file that will handle the process of performing the git pull. Add a new file, called github.php (or anything you wish ), and add:

<?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” E.g. 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.

To see this in more details to go to this tutorial

like image 196
Christos Papoulas Avatar answered Oct 03 '22 00:10

Christos Papoulas


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. 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.

Here a few articles you can read to get more info:

  • GitHub - Deploy with Capistrano

  • How to compile the Capistrano stack on your *nix system

  • Another example of how to deploy code with Capistrano

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. 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.

like image 21
Mike Purcell Avatar answered Oct 03 '22 00:10

Mike Purcell