Updated for Git 2.4: There have been some improvements to push-to-deploy in Git 2.4, and I've updated the article below. Read more about Git 2.4
Git 2.3 released earlier this month, Github wrote a blog post with some of the highlights, the feature that stood out for me was a simpler way to handle deploying through a git push. I'll go through the steps to set it up.
This requires SSH access
I'm assuming you have your project hosted - if not I'd recommend Bitbucket which has free private repos for individuals and small teams.
SSH into the server then clone your project
ssh firstname.lastname@example.org /var/wwwgit clone email@example.com:username/project.git
Update: if you are using Git 2.4, you can now push to an empty git repository:
mkdir projectcd projectgit init
Now enable the new setting in your project
cd projectgit config receive.denyCurrentBranch updateInstead
exit the live server and
cd into your project directory.
Add the git remote to your local repository
git remote add deploy ssh://firstname.lastname@example.org/var/www/project
You should now be able to deploy with
git push deploy
Now you're setup you're probably wanting a way to do some build tasks during a server deploy, somes examples could be installing dependencies, pre-processing sass, restarting the process.
For my example I'll run an
npm install then restart the node process through pm2.
SSH back onto the server, we'll then use
vim to create a new update hook
ssh email@example.com /var/www/project/.git/hooksvim update
i to go into insert mode and enter:
#!/bin/shnpm install --productionpm2 restart project
esc to come out of insert mode then type
enter to write the file and exit. You can replace the above with any commands you want to run during the deployment process.
In order for the file to execute you'll need to set the correct permissions on the file
chmod +x update
That's now setup,
exit from the server. Now the next time you do a
git push deploy it will run the githook, output from the githook will be displayed in your terminal during the deployment process.
Update: If you have Git 2.4 there is an additional
push-to-checkout hook which will run only when the branch that is checked out on the server is pushed to.
Previous methods involved setting up a bare repository on the server, which is a folder on the server that just handles the version control and doesn't have the checked out source files and requiring a separate folder to run the project from.