Automatic Deployment Workflow with Bitbucket(git) in Hostgator

Hostgator has git and svn preinstalled. This enables us to automate deployment! The end result is when you push a commit to bitbucket, the changes are automatically deployed your site… automatically.

For this tutorial I will assume you have
  1. Bitbucket account
  2. Your git repo already uploaded to Bitbucket
  3. sourcetree git client managing your local git repo
  4. root access to your hostgator
  5. SSH access
My bitbucket account is bruceoutdoors, and I’ll be using a private git repo deploy-test.

Set up SSH in hostgator

After you have logged in to the cpanel, use your legacy file manager and set it to home directory and show hidden files.
Create a new .ssh folder if it has not existed.
Download PuTTy if haven’t got it. NOTE: To paste contents to PuTTy, right click on the terminal. Enter something like:
Hostname: gator1111.hostgator.com:1111
Port: 2222
Click “Open”. Login into hostgator with your username and password
run:
cd .ssh
ssh-keygen
We won’t be using any passphrase, so press enter until the SSH Key is generated. This should generate 2 files:
  • id_rsa – private key
  • id_rsa.pub – public key
Create a new file in your .ssh/ directory and name it authorized_keys.
Copy paste the contents of id_rsa.pub inside.
Configure your site’s repo to use SSH instead of HTTPS
Deploy your git repository to your server.
Edit your .git/config in your site directory, change
url = https://bruceoutdoors@bitbucket.org/bruceoutdoors/deploy-test.git
to
url = ssh://git@bitbucket.org/bruceoutdoors/deploy-test.git

Install the public key on your Bitbucket account

Open a browser and log into Bitbucket.
Choose avatar > Manage Account from the menu bar.
The system displays the Account settings page.
Click SSH keys.
The SSH Keys page displays. It shows a list of any existing keys. Then, below that, a dialog for labeling and entering a new key.
copy the contents of the public key file id_rsa.pub.
Back in your browser, enter a Label for your new key, for example, Default public key.
Paste the copied public key into the SSH Key field.
Click the Add key button.

The system adds the key to your account.
Return to PuTTy and verify your configuration by entering the following command:
ssh -T git@bitbucket.org
It should ask you

The authenticity of host ‘bitbucket.org (111.113.10.161)’ can’t be established.
RSA key fingerprint is 11:8p:5b:f2:6f:14:6q:1t:1r:ec:aa:11:11:14:7c:40.
Are you sure you want to continue connecting (yes/no)?

type yes and press enter:
Warning: Permanently added ‘bitbucket.org,111.113.10.161’ (RSA) to the list of known hosts.
logged in as bruceoutdoors.
You can use git or hg to connect to Bitbucket. Shell access is disabled.

This will add a “known_hosts” file in ~/.shh/ in hostgator

This is how your .ssh directory will look like now:
Image
PHP Auto Deploy Script
We will now create a php script that will execute the git pull command without us needing to log into hostgator.
Create a file “git-hook.php” in your site repository with the following contents
<?php
echo "<b>Attempting to pull from git repo...</b> <br>";
exec('git pull', $output);
foreach ($output as $o) {
    echo $o . '<br>';
}
If you go to the URL of your site, it should output something like:
Attempting to pull from git repo...
 Already up-to-date.
Now to set up hooks so that every time a commit is pushed, it’s changes are automatically reflected in our site.
POST Hooks
In Bitbucket, in your repository settings, Select Hooks. Add a POST hook and enter the URL of your git-hook.php script. It should look like this when you’re done:
Image
Now every time you push a commit to bitbucket’s repo, it will automatically deploy your changes.
Reference:
Advertisements

5 thoughts on “Automatic Deployment Workflow with Bitbucket(git) in Hostgator

  1. Pingback: How to Deploy from Local to Bitbucket to Cpanel Automatically | Full Stack Blog

  2. i have done lot of thins. but i am running “git-hook.php” file from my server’s terminal.
    not from browser or bitbucket.
    when its running from browser, nothing happens..
    please reply me back asap

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s