Git & Github part 2 --> WebHook for automated deploy

In this article I will focus on how I could make a simple automatic actualization of the code on my webserver (running linux / apache / php) , each time new code is pushed on my Github Repo.

The last article was focused on how to use Git and how to publish code on a public Github repository.
One of the great advantages of github is to be able to access it from everywhere, and to connect as many machines is needed.  My webserver being a linux machine, we can easily clone our github repository and make a script that will pull code from it each time the code has been actualized.

This is done through a "webhook". Kind of the same process I have been doing quite some time ago with SVN "post-commit" hooks.

These articles shows how to do:

https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps

http://behindcompanies.com/2014/01/a-simple-script-for-deploying-code-with-githubs-webhooks/

Basically, here are the steps to do the trick:

1) you need to clone your github repo in --bare mode into your web server home directory

2) create a post-update script with the following lines:

#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/home/billyJoe/site.git checkout -f
Don't forget to add execute permission to the script you created,  and to check the permissions on your web directory.

3) go back to your local computer's repositiory and add the web server as a new remote:

git remote add web ssh://myUser@serverurl/home/billyJoe/site.git


Here it is, now you should be able to do :

git push web master 

to push your changes directly to the web server!!!




Commentaires