Automate project deployment

Imagine a GitHub repository but on your server that you can push code to, a configuration file that will be responsible for building and deploying your app, and you are one command away from deployment. Let's dive right in.

cover image

Before you start reading I suggest you read the previous article about setting up a systemd service for starting your project. Click here to read it, and also make sure that git is available.

Let's dive right in.


  1. Push code directly to the server from the local machine
  2. Restart the app automatically with the latest code changes

How do we achieve this

This is done in 4 steps.

1. Login to your server and initialize a bare repo using this command

git init --bare /opt/my-project.git

2. Clone that repo inside of a directory that will contain your code

git clone /opt/my-project.git /opt/live/my-project

3. Add a post-receive hook at /opt/my-project.git/hooks

This directory will have a lot of hooks inside, but no post-receive. We will create one using nano post-receive.

Inside of this hook, we will write a configuration that will be triggered when you push your code to the server.

I will write a configuration for a simple React project.

NOTE! Before proceeding click here to read about systemd service since we will use that to start/restart the app.

#!/bin/bash echo 'process started' cd /opt/live/my-project echo 'git check out…' git --git-dir=/opt/my-project.git --work-tree=/opt/live/my-project checkout master -f echo 'post-receive: yarn install…' yarn install \ && echo 'building project' \ && yarn build \ && echo ‘done.’ \ && systemctl restart my-application && echo 'app successfully restarted'

You can configure this file to suit your project's needs.

  1. && is to execute commands synchronously
  2. \ is to write && in multiple lines

You can also omit all the echo lines, but it is convenient to have it so we know what step the process is currently on.

4. Make post-receive hook executable

chmod +x ./post-receive

What now?

All that is left to do is add the remote repo to a local project, so we can push to it.

Open up the terminal inside your project and type

git remote add prod ssh://root@

Change with your server's IP address. You can also change prod to anything you'd like, as it is just a name you will use to push your code to.

Testing it

We are all set. To test it out make some changes to the code, commit it and push it to your server using

git push prod master

This will push code directly to your server, trigger the post-receive hook, and all the commands in it. You will also have feedback in your terminal from where you push your code. It will print out what is happening as if you are building a project on your local machine.