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.
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.
- Push code directly to the server from the local machine
- 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
2. Clone that repo inside of a directory that will contain your code
3. Add a
post-receive hook at
This directory will have a lot of hooks inside, but no
post-receive. We will create one using
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.
You can configure this file to suit your project's needs.
&&is to execute commands synchronously
\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.
post-receive hook executable
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
18.104.22.168 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.
We are all set. To test it out make some changes to the code, commit it and push it to your server using
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.