Git: pushing & automatically deploying code changes
Do you develop code stored in git, and want to push changes out to production boxes automatically? I’ve used this method successfully plenty of times: ###Target Server
On the receiving server, we first need to create a bare git repo. We then use the post-receive hook to automatically deploy changes to a working directory. In this case, the code will be deployed to /var/www/html.
# Make the 'bare' git repo mkdir /home/app/git cd /home/app/git git --bare init # Add a hook to automatically deploy the code when updated cat << EOF > hooks/post-receive #!/bin/bash GIT_WORK_TREE=/var/www/html git checkout -f EOF chmod +x hooks/post-receive
Next we add a remote repo called prodservers with the first server’s details:
git remote add prodservers ssh://firstname.lastname@example.org/home/app/git/
Remember to stick your SSH key on the target box, so git doesn’t need to ask for a password.
Then push your code out!
git push prodservers master
If everything has worked, you should see your code appear in /var/www/html of your application server.
Note: git remote doesn’t support adding more than one remote target, so to add multiple target servers you’ll need to edit .git/config by hand (please correct me if I’m wrong!). In this case, your .git/config file would have a section which looks a bit like this:
[remote "prodservers"] url = ssh://email@example.com/home/app/git/ url = ssh://firstname.lastname@example.org/home/app/git/ url = ssh://email@example.com/home/app/git/ url = ssh://firstname.lastname@example.org/home/app/git/ fetch = +refs/heads/*:refs/remotes/prodservers/*