Curious Chase

Curiously Chase

Deploy Code with Git Push and a Post Receive Hook

Share on Twitter

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

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:



DEPLOYDIR=$DEPLOYDIR # The place to deploy to.

echo -e "[log] Received push request at $( date +%F)" >> $LOGFILE
echo "[log] - Old SHA: $oldrev New SHA: $newrev Branch Name: $refname" >> $LOGFILE
echo "[log] Starting Deploy..." >> $LOGFILE

echo "[log] - Starting code update"
GIT_WORK_TREE="$DEPLOYDIR" git checkout -f
echo "[log] - Finished code update"

echo "[log] - Starting npm install..."
cd "$DEPLOYDIR"; npm install; cd -
echo "[log] - Finished npm install."

echo "[log] - Starting gulp build..."
cd "$DEPLOYDIR"; gulp build; cd -
echo "[log] - Finished gulp build."

echo "[log] - Building Hugo blog..."
cd "$DEPLOYDIR" && hugo
echo "[log] - Finished building hugo blog"

echo "[log] Finished Deploy" >> $LOGFILE

A few things to note:

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.

Share on Twitter