Quick web deployments using git-bare repositories

Thilina Lakshan
3 min readJan 18, 2021

--

git-wall

Here we are going to talk about what is a git bare repository, how to create one, and how to use a bare repository to deploy a web project to a server. We will deploy a small nodejs+express web application to AWS EC2 container as an example.

What is a bare repository?

We all know git init can be used to initialize a new git repository. This command will initialize the current directory as a git working tree. We can write code, commit, change, again commit, create a branch, and much more things. A regular git repository consists of both a working tree as well as the “.git” directory which contains all the information that necessary for the git repository including the history of the repository.

A bare repository is a git repository that doesn't contain the working tree. It only consists of the repository data. It basically means, we cannot have project files there like regular repositories but we can choose a different location for storing files. Therefore, It is handy if we can use it as a remote repository for deployment purposes. Didn’t get it? Wait for the example.

Let’s create a bare repository

It is just like you creating a regular repository but the command is a little bit different, open your terminal navigate to a directory then type,

git init --bare

This will initialize the directory as a git bare repository. Then you will see a strange list of directories and files. These are the repository data if the repo was a regular one these things are included inside the “.git” subdirectory.

Let’s use the bare repo as a remote repository

The actual use of a bare repository comes when we use it as a remote repository. Let’s create a remote repository. I will be login into an AWS EC2 ubuntu 18 LTS container via an SSH connection. It doesn't matter the way you connect to your remote container but you need to have the correct access rights to your container and the accessing port should be open. Simply make sure the firewall doesn't block 22, 80, 443 ports. By default, they are open.

After you logged into the container, create a directory structure as below,

home_dir
|--- repo
|--- sample_app.git
|--- sample_app_files

We are going to use “sample_app.git” for the bare repository and “sample_app_files” for web application files. “home_dir” is your home directory (Ex: /home/ubuntu or ~)

Navigate to the “sample_app.git” directory and initialize it as a bare repository. Then we need to create a post-receive script for the bare repository which is responsible for unpacking files into “sample_app_files”

#!/bin/shFILE_DIR=/home/ubuntu/repo/sample_app_files
BARE_DIR=/home/ubuntu/repo/sample_app.git
git --work-tree=$FILE_DIR --git dir=$BARE_DIR checkout -f deploy

Create a file named “post-receive” which contains the above content then put it inside the hooks directory. (~/repo/sample_app.git/hooks/post-receive)

Then we need to make it executable, following command will make the “post-receive” script executable,

chmod +x ~/repo/sample_app.git/hooks/post-receive

Now everything is ready.

Let’s deploy a small app

Sample Application

If you don’t have a nodejs application clone this repository https://github.com/ThilinaTLM/blog1_sample_express_app

Update Post Receive

The only thing our post-receive script doing is unpacking files into “sample_app_files”. If we need to deploy our app automatically we need to add something extra. Update your post-receive script as below,

#!/bin/shFILE_DIR=/home/ubuntu/repo/sample_app_files
BARE_DIR=/home/ubuntu/repo/sample_app.git
git --work-tree=$FILE_DIR --git dir=$BARE_DIR checkout -f deploycd $FILE_DIR
npm run start&

The last two lines are responsible to start our nodejs application. Also, you need to have installed nodejs on your container.

Final step

Now we need to figure out the public URL of our remote bare repository.

# If you are using an SSH
username@host_address:absolute_path_to_sample_app.git"
# If your are using HTTPS
https://host_name/path_to_sample_app

Now we need to add the bare repo URL as a remote in our local repo. Navigate to local repo then inside the terminal,

git remote add aws url_of_bare_repo

Change the remote name “aws” as you like. Now we can push to new remote,

git push aws master:deploy

If you are using SSH make sure your public key is correctly added to an ssh-agent or provide it when you are pushing. Little bit of research can be help full you to configure SSH correctly. Also If you willing to add remote host details to ssh config file It will be very easy for you.

Great! 😀

Thank you!

--

--

Thilina Lakshan

Software Developer, Geek, Undergraduate of Department of Computer Science and Engineering, University of Moratuwa