Deploy Code with Git Push and a Post Receive Hook

This is a handy trick for pushing your code and deploying it with the same command.

In your git repo on your remote server there's a directory called hooks. Create a new file in hooks called post-receive and set it as an executable by running chmod +x post-receive.

Now open post-receive and insert the following code:

A few things to note:

  • $RCA_THEME_PATH is an environment variable set to the absolute path to where I want my repo to checkout the code.

  • The file has to be modified to be executable. If you don't do this, you're going to be banging your head against your keyboard until you realize you need to.

Now, once you've added your updated code and committed it, when you run git push origin master (replace origin with your remote name), you'll see your code change on your remote server.

Issues with this technique

For the most part, this technique is a pretty good solution for pushing code and deploying it at the same time. One major drawback is that you have to version control your distribution directory (the compiled code for your js, css and templates), which is not generally considered a good practice.