blog-image

Apr 08, 2021

10 min read

Git Hooks — Easily deploy code via git push

Written by

Vippy The VPS
The best way to get into any field is to start with a problem statement, and then learn the necessary concepts as you try and solve that problem. The same is true with DevOps and today we will start with a very simple problem and craft a very rudimentary solution for it.

Goal: Deploying code to remote server via git push

Let's say you have a website hosted on a VPS, and you have a local copy of its source code on your laptop. As your needs change, you will inevitably want to make some changes to the website. If you are using git to track changes to your source code, you can use another functionality of git called git hooks to push your local changes to the remote server, and have the server deploy it for you. This is the crudest, and yet very effective way of getting started with DevOps. A practive which merges the Development workflow closely with that of Operations.

Prerequisites

To follow along this tutorial you will need:
  • A very basic familiarity with the Linux command line.
  • A very basic idea of how to use git, creating a repository and committing changes.
  • A VPS with a public IP for which you have key-based SSH access as root user.

Setting the stage

We will start with a very simple setup where an Nginx Server is serving plain HTML files on port 80 and we will make changes to these files locally and push the changes to the server running Nginx. If you follow along, by the end of the setup, just reloading the webpage at http://your_ip_address will show you all the changes that were pushed without you having to manually login into the server.

Local setup

To start with, you will need git installed on your local computer. Windows users should look at git-scm and if you are using Linux or WSL2 then you can install it via your package manager. Similarly, macOS users can install git via brew package manager or by simplying installing Xcode or Xcode Command Line Tools. Great! Now that we have git installed, we create a simple git repo, and add some very simple HTML code in it.
$ mkdir html
$ git init
Initialized empty Git repository in C:/Users/r/html/.git/
Create a file in ~/html directory named index.html and
Continue reading this article
by subscribing to our newsletter.
Subscribe now

A note about tutorials: We encourage our users to try out tutorials, but they aren't fully supported by our team—we can't always provide support when things go wrong. Be sure to check which OS and version it was tested with before you proceed.

If you want a fully managed experience, with dedicated support for any application you might want to run, contact us for more information.

Leave a Reply