How to Perform Automatic Deployment with Git

Posted on

This article will discuss the automatic deployment that can be performed using Git version control. This article itself inspired by the writing which is posted in the article titled ‘How to Set Up Automatic Deployment with Git with a VPS’ in this link.

This article will describe the scenario which is a simple scenario and based on the scenario itself, it an be developed later on to be adjusted with the real implementation of the automatic deployment according to the need and the condition associated.

The image depicting the scenario which is going to be described in this article can be shown as follows :

The following are descriptions consisting of steps which is needed to be done in the server side and also in the client side from the above image :

In the server side :

1. Setup the folder which is going to be act as the Git repository. In the above image context, it is in /home/user/repo/app.git. Below is the command for achieving it :

[user@server ~]$ mkdir -p repo/app.git && cd repo/app.git
[user@server app.git]$  

The above command is used to create directory recursively with the name of repo/app.git. This folder will be used as the Git repository.  The above command will direct the bash prompt of the current working directory to the directory which have been created.

2. Initialize an empty Git repository without any intention to use that directory as a working folder. The following is the command :

[user@server ~]$ git init --bare
Initialized empty Git repository in /home/user/repo/app.git
[user@server app.git]$

3. Create a folder which is going to act as the folder where the application is going to be executed. In this context of the article, the folder for running the application also stored and located in the same server. For an example it is shown as follows :

[user@server ~]$ mkdir app && cd app 
[user@server app]$

4. Following the step above, create a file named post-receive and if it is already created, edit the file. It is located in hooks/ inside the already created folder which is initialized as a Git version control in the previous step. Below is how to create a post-receive file :

[user@server app.git]$ touch post-receive
  1. The file named post-receive must be edited further to be filled with the following content :
#!/bin/bash
git --work-tree=/home/user/app --git-dir=/home/user/repo/site.git checkout -f

After configuring on the server side for setting up the repository and also the location of the application where the auto deployment will be performed, below are the steps must be done in the client side.

In the client side :

1. Create a folder which is going to be used as the working folder in the client. It is where the files or folders of the source code application is created, modified or even removed. Below is the execution of the command to accomplish the task :

[user@localhost ~] $ mkdir source-code && cd source-code
[user@localhost ~/source-code] $

2. Initialize the already created folder in the first step to be associated with Git version control. Below is the command executed to do it :

[user@localhost ~/source-code] $ git init . 
Initialized empty Git repository in /home/user/source-code/.git/
[user@localhost ~/source-code] $

3. Add file to the folder initialized in the 2nd step. This file will be pushed to the repository server which in the end will be auto-deployed to the running application folder at the same server. Below is an example :

[user@localhost ~/source-code] $ touch test-file
[user@localhost ~/source-code] $

4. The next step is to push the already created file to the repository server and automatically deploy it to the running folder of the application. Below is the steps for accomplishing that task :

a. Add files or folders. This is useful to record files or folders which is going to be registered for further commit operation. Any commit operation only works on the files or folders added.

[user@localhost ~/source-code] $ git add .  
[user@localhost ~/source-code] $

b. Commit the files or folders. The commit operation is committing files or folders which has already been added to be prepared in the staging area at the local Git repository. Below is the command executed :

[user@localhost ~/source-code] $ git commit -m "Test Add a file named test-file
[user@localhost ~/source-code] $

The above command has an additional parameter -m which is added to specify a message attached in the commit operation.

c. The last step is the push operation which is done to push the already committed files or folders in the previous step to the remote repository server. And in the then, git utility version control will also auto deploy the files or folders pushed.

Check the content in the server, especially in the app folder where the location of the app which is intended for storing the app.  It is shown as follows :

[user@server app]$ ls -al
total 4
drwxrwxr-x. 2 user user   17 Jul 27 09:34 .
drwx------. 8 user user 4096 Jul 27 10:36 ..
-rw-rw-r--. 1 user user    0 Jul 27 09:34 test-file
[user@server app]$ 

Leave a Reply