Mobile app version of vmapp.org
Login or Join
Si4351233

: Automatically Trigger Git Pull For Website On Github Push I have just finished re-designing my website development environment to have all my source code in Github and have managed to set up

@Si4351233

Posted in: #Automation #Git #Github

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 code ready for deployment and has already gone through testing. When I push the code to the Master branch I wan to use one of Githubs 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 serer to trigger the git pull origin master command.

Thanks

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Si4351233

2 Comments

Sorted by latest first Latest Oldest Best

 

@Yeniel560

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:


Create a "bare" repo with git init --bare
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
On your local machine add the bare repo as a remote.
Use git push <remotename> to push to your live site.

10% popularity Vote Up Vote Down


 

@Si4351233

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme