Deployment with Git featured image

How to Configure Automatic Deployment with Git with a VPS

Git is the most popular version control system in the field of software development. It’s open-source and actively maintained since 2005 by Linus Torvalds, the famous creator of Linux. Today, a staggering number of software projects (including commercial ones) use Git for version management.

Git follows a distributed architecture, making it a perfect example of a DVCS (Distributed Version Control System). Using Git, we can manage both a local and a remote repo simultaneously. In this guide, we will demonstrate how to configure an automatic deployment of a Git project with a VPS.

Prerequisites

In this guide, there will be one local repo and a remote repo on the VPS. The local repo will be pushing changes to the remote repo. From the remote repo, we can push the changes to the beta or  live branch. To achieve this, there are a couple of prerequisites you need to take care of first.

To establish a secure connection to the VPS, we will be using SSH. Thankfully, Git also supports SSH as a protocol to connect to remote repos. This guide explores configuring SSH to connect to remote servers on Ubuntu.

Configuring the VPS

On our remote server, we’ll have the following configuration:

  • Live directory: /var/www/dummy-domain.com
  • Server repo: /var/repo/site.git

From the local machine, Git will push the updates to the VPS repo. From the VPS repo, we’ll configure a script that Git will use to automatically push it to the live directory. It’s also possible to configure a repo for beta releases.

  • Creating the Repositories

Connect to the VPS and create the repo locations:

Deployment with Git screenshot 1

Next, initiate the Git repo:

Deployment with Git screenshot 2

Here, the flag --bare describes that there will be no source files, only the version control components.

  • Hooks

Next, we’ll configure the hooks for the repo. Every Git repo contains a folder named hooks. It contains sample files that serve as a template for hooks and various custom actions.

According to Git’s official documentation, there are three types of hooks:

  • pre-receive: It’s executed as soon as the server receives a push request.
  • post-receive: It’s executed when a push request is completed.
  • update: Similar to pre-receive. However, it executes once per branch.

From the location of the repo, run the ls command:

Deployment with Git code screenshot 3

Change the current directory to hooks:

Deployment with Git code screenshot 4

Now, create the post-receive script:

Inside the script, enter the following code. Make sure to change the directory locations accordingly:

Deployment with Git code screenshot 6

Save the file and close the editor. Set the file as an executable:

Deployment with Git code screenshot 7

Now, every time a push is completed, Git will look into the post-receive script and put the files in place accordingly.

Configuring the Local Machine

The remote machine is ready to accept incoming Git project updates. Next, we will configure the local repo to use the VPS as the remote Git server.

Let’s make a sample Git project directory:

Configuring the Local Machine

Deployment with Git code screenshot 8

sample Git project directory:

Now, initiate the repo:

Deployment with Git code screenshot 9

We have to declare the remote path of the repo. Here, we’re going to refer to the remote location as the live branch. Then, run the following command:

sudo git remote

Let’s test it out. We added a shell script to the project directory:

project directory

Next, add the file to the project and perform a commit:

Deployment with Git code screenshot 10

In the next command, we’ll push the project to the live server:

Deployment with Git code screenshot 11

Git will connect to the VPS using SSH. If the SSH connection was configured to use a password, Git will ask for the authentication password.

Beta Repository

What if the project isn’t to be deployed at one step? Maybe it requires further testing within a beta directory. Using the method described so far, we can create another branch in the VPS, referred to as the beta branch.

  • Configuring the Beta Branch on VPS

First, we need to create the directories for the VPS. Connect to the VPS and create a directory under /var/www:

Now, we need a Git repo that will act as the beta branch:

Initiate the repo:

sudo git init

Just like before, implement a post-receive script:

sudo nano

After that, enter the following code:

GNU nano

Save the file and close the editor. Mark post-receive as an executable file:

sudo chmod
  • Adding the Beta Repo on the Local Machine

We’re now ready to add the beta repo to our local repo. From within the local project directory, run the following Git command:

Voila! The local repo is now configured with the beta remote repository. Try pushing changes:

sudo git add
  • Moving from Beta to Live in VPS

Let’s say all the changes are accumulated to the beta repo. How do you push the changes to the live repo? It’s very simple. Within the beta repo on VPS, we add the local live repo. Then, we simply push the beta release to the live repo.

Connect to the VPS, and run the following commands:

Then, you can push the beta release to live:

sudo git push live master

Final Thoughts

This guide successfully demonstrates how to configure and use a VPS as a remote repository for your Git project. The VPS can automatically deploy the latest codes and files without any complex process. It can also act as the center for collaboration among multiple development teams.

With the help of Git, you can establish a simple mechanism for automatic deployment. For proper project management, however, it’s recommended to use something like GitLab. The following guide showcases installing and deploying GitLab on a VPS that manages multiple projects. You can also check out how to set up GitLab Continuous Integration (CI) pipelines on Ubuntu 20.04.

Happy Computing!

About Hark Labs

Software Engineer and enthusiast about new technologies