Web Developer / Web Designer




Using git and its post-receive hook to manage your website on GoDaddy

Category : Tech · by May 6th, 2016

After a day of headaches I got my GoDaddy hosted site to work with git and it’s post-receive hook on the server.  I’m going to share it with you.

Things to note:

First things first, I assume you have some knowledge of how SSH and git work.  If you need to know in depth information on git or SSH please visit their sites (linked).

Setup a repo directory on your GoDaddy hosted site via SSH

Open a Terminal window, I use iTerm instead of Mac’s terminal

Then follow the steps to ssh into your hosted account

If successful, a password prompt will display.  Enter the password you created when you activated SSH on your GoDaddy account.

When you have successfully logged in you should see:

You are now in the home directory of your GoDaddy site.  If you list the files and directories by typing “ls”, you should see the “html” directory and just as important, “git”.  The “git” directory is the git application itself, just leave it be.  The “html” directory is your site.

Next, create a directory called repo. 

“cd” into that directory and create another directory called yoursitename.git.
yoursitename.git will be the home for your bare git repo.

“cd” into yoursitename.git directory

In the terminal window create a bare git repo:

If successful, this will add the necessary files for your repo.  If at this point you get an error stating “command not found”, then you do not have git installed.  Install git.

Before we get into the post-receive hook, lets verify that the remote repo will successfully accept pushes from your local git repo.

Installing a local git repo and adding a remote path in the directory where your local files live

Open a new terminal window (Command + N on a Mac), this should start at your home directory.

“cd” into the folder container your local website files.

Create a local git repo

If you are successful you should receive a message in the terminal like this:

Perfect! Next we want to point our git push command to a remote repo.  This next step will add the path to the GoDaddy bare repo we created in the previous section.

Note: “godaddy” above is the alias name of the remote.  You can name it whatever you want.

To verify this was successfully added type:

It should display two lines:

Lets test it.  If all is successful, you will be able to push to your remote repo on GoDaddy.

First you need to add at least one file to your local directory.

Next, type:

When you push to your remote, you may be asked for your SSH password.  If so, enter it.  If the following was successful you should receive a success message:

Now the fun part.

Automatically deploying changes to your site with git post-receive hook

What a post-receive hook does for you is, once it’s setup, lets you push to your remote repo any changes to the code and it will grab the code instantaneously and replace it on your live site.  You can setup a post-receive hook for each branch you have also and make that branch deploy to another location on your server.  Nifty!  So if you have a production, QA, and development branch you can deploy code to each of those with post-receive hook.

Now lets get into it.

First you want to login to your remote GoDaddy server using SSH.

Enter your password if prompted.  Once you are logged in “cd” to your repo on the server.

“cd” into the hooks directory.

If you list the files there you will see a lot of .sample files you can configure.  Most likely post-receive.sample does not exist.  We’ll have to create it.

BEFORE WE CREATE THE POST-RECEIVE FILE, we need to get the full path to both the site files directory (where your website files live) and the full file path to the git repo on our remote server.  To do that, what I did was “cd” into the /yoursite.git directory and typed “pwd” as well as “cd” into the site files directory and used “pwd”

I use VIM to edit and create a file on the server.

Inside the vim editor, press “i” for insert.  This allows you to type inside the file.  In the bottom left of your terminal screen it should display the word “INSERT”,  This means it’s in edit mode.

Inside that file all you have to do is type basically where your files will deploy and where the remote repo is located.  Including a git command called checkout.

Here is an example:

Make sure to include the “/” before the beginning of the path.

To save the file, press escape to exit out of edit mode.  Then type “:x”, this saves and quits VIM.

That’s it!  Now test your post-receive hook by making changes to files locally and pushing them to your remote repo.

Check your website to see the changes.

 

SHARE :