11 / 100

How to make Continuous Deployment (CD) to work on your shared hosting

Wouldn’t it be great if you can deploy your website into the cloud with just a single push?
Yes absolutely.
This tutorial will just teach you how to do this.

It’s one of the best things I’ve learned to do with git and it’s making my deployment damn quicker and fast.

Using git for deployment makes it very very easier for us to deploy our websites/web applications to the cloud in an easy and quick way. If something is broken in the production, we can also easily revert to the previous build.
There are advantages such as,
-Managed version control
-Quick Deployment
– Quicker release management
– Easy to setup

Initially, you need to have web hosting with ssh access. 

If you do not have ssh access in your web hosting, you can go with git-ftp but git ftp is not so suitable option for most of the use cases and doesn’t provide much flexibility provided by this approach.

At first, Connect to the server with ssh. If you are in windows, use putty, or in Linux/mac just use ssh in your terminal.

I am using a2hosting for this tutorial. If you are using other web-hosting providers, you can follow the same approach.

A2hosting is the best-shared cloud service provider I’ve found till now.
open your terminal and hit these commands,
In a2hosting you can get all these credentials and ports in your dashboard.

ssh username@xxx.a2hosting.com -p 7822

now, it’ll ask for credentials and you can hit the password displayed on the homepage of your hosting provider.
The default port for ssh is set to 7822 in a2hosting.

 After a successful connection, go to the root of your cloud server and create the bare repo there.

git init --bare ~/project.git

Enter the following commands in a terminal:

cd ~/project.git/hooks
nano post-receive

copy and paste the following code exactly as it is. Please enlarge the terminal before pasting to avoid cut out texts.

#!/bin/bash
TARGET="/home/username/sabinkhanal.com.np" --your domain's home folder
GIT_DIR="/home/username/project.git" --your git repo URL
BRANCH="master"

while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [ "$ref" = "refs/heads/$BRANCH" ];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done

Giving execute permission to post-receive script.

chmod +x post-receive

It’s all we need to do.
After that whenever we push to this repo, our contents would be copied to the target website’s home folder.

Now in the client PC,
Go to your project’s distribution folder and open the terminal & paste the following code.

git remote add production ssh://username@xxx.a2hosting.com:7822/home/username/project.git
and common git commands:
git add .
git commit -m "initial changes"
git push origin master
git push production master

 

After doing this your codes would be pushed to the production server.

It’s very easy to set up and also very easy to deploy to the cloud using this method.

If you faced any issues please let me know in the comment section below.
Thank You.