Automatically Trigger Git Pull For Website On Github Push

15,048

Solution 1

Okay have managed to figure this one out. Turns out there is no native feature in Git to trigger a remote fetch (as in pushing code to the repository and having the repository trigger a pull origin on the web server).

The way I have resolved this was to upload a PHP script to my web server under the default vhost. Within that file I have it set to run shell_exec("cd /path/to/my/site/root" && sudo git pull origin master". It is then set to send me an email with the message output through STDOUT to inform me if the pull was completed successfully or not.

I have then set Github up with a webhook so that every push to the repository will trigger a webhook call to the file which I simply have addressed as http://server.domain.com/github-deploy.php.

There are presently some undisclosed security checks I perform to make sure that no one else can access the file and in time I will probably add IP checks to make sure that requests to the file only come from IP's in Github's address block.

An important note that I should add here is that the only way to make this work is to allow the apache user access as a sudoer on the server but limit it to being able to run the sudo command to launch Git otherwise an access denied message will be triggered. By restricting it to git though and not passing any input from the calling script into the shel_exec command I believe I have been able to offset any concerns over security.

Eventually I will probably implement this as a service link with Github but for now this is ample.

Solution 2

Serving your site from a git "working directory" is considered bad practice - see reasons here and here.

The best way to do this is to set up a "bare" git repo on your server with a "post-receive" hook. This article is what I followed when I did this recently, the basic idea is:

  1. Create a "bare" repo with git init --bare

  2. Edit hooks/post-receive in your bare repo to copy the repo to your web root: GIT_WORK_TREE=/var/www/yoursite git checkout -f

  3. On your local machine add the bare repo as a remote.

  4. Use git push <remotename> to push to your live site.

Share:
15,048

Related videos on Youtube

Chris Rutherfurd
Author by

Chris Rutherfurd

Updated on September 18, 2022

Comments

  • Chris Rutherfurd
    Chris Rutherfurd over 1 year

    I have just finished re-designing my website development environment to have all my source code in Github and have managed to set up Git on the remote server so that simply running the git pull origin master command on the web server will update the entire application source code from the Github repository.

    What I am trying to figure out now is a way to automate the process. The code pulled from the Master branch which always has the code ready for deployment and has already gone through testing. When I push the code to the Master branch I want to use one of Github hooks to automatically run the pull from the server to update the application.

    I have SSH access to the server and am able to run the command manually each time I need to at the moment but it is time-consuming pushing code to Github, then opening an SSH connection to the server and pulling the updated code.

    Not sure if there is a native feature within Git that supports this or if I would need to use a web hook and run a file on the web server to trigger the git pull origin master command.

    Thanks

  • DisgruntledGoat
    DisgruntledGoat almost 8 years
    Care to explain the downvote?
  • Chris Rutherfurd
    Chris Rutherfurd almost 8 years
    Because the question was specifically how to trigger a git pull from a web server. I understand that this may not be the best way to do things, however I am doing it significantly differently to anyway that you point out which makes it a safe option for my needs. As for your answer it does not answer the question rather it tries to redirect the answer away from the original question. As it does not answer the specific question would be better suited to a comment.
  • DisgruntledGoat
    DisgruntledGoat almost 8 years
    Well the answer to your question is "you shouldn't do that", which is a perfectly valid answer. Sorry it doesn't work for you, but hopefully it will help others in future.
  • Azmat Karim Khan
    Azmat Karim Khan almost 3 years
    can you please share script you are using thanks