Here at Kualo, we love to provide customers with new and efficient ways to get their work done, so we've done exactly that. We would recommend that you set up an SSH public key, but you can also use your cPanel password to authenticate over SSH.
In this tutorial, we'll go over how to push updates to your website. In order to do this, you'll need a few things:
- Your server's hostname
- Your cPanel username
- SSH access enabled on your account
- Basic BASH scripting knowledge
- A Git Client
edgar.secure.kualo.net
Let us also assume our username is:
mywebuser
Create a development folder
To begin, let's SSH into your account, and create a directory outside of the document root, which is usually the "public_html" folder. Note that here we use ~/, which in this case would resolve to /home/mywebuser/mywebuser@edgar$ mkdir -p ~/development/website.git
The above command will create the development directory in your home directory, if it doesn't already exist, then it will descend into development, and create a folder called website.git if it doesn't exist already.
Initialize the GIT repository
mywebuser@edgar$ cd ~/development/website.gitmywebuser@edgar$ git init --bare
What this will do is create an empty repository (--bare) on the server with an empty reference log and allow it to be cloned.
Clone the project locally
This step needs to be done locally or on your development environment.you@local$ git clone ssh://mywebuser@edgar.secure.kualo.net:22007/development/website.git
you@local$ cd website
Cloning will copy the current branch (default is master) from the server to where you executed the git clone from.
Add your files
Copy and paste, move, or create your files in the directory that was just made, and commit it to the development repo like so:you@local$ git add -A
you@local$ git commit -m "first commit"
you@local$ git push
Create a post-receive hook
Once you push, it should commit your changes to the repository you created on the server. However, this will only store the data about the commits, not the actual files themselves. In order to have your commits push live, we'll have to create a hook to do so.In the following file:
~/development/website.git/hooks/post-receive
Place the following code to have the hook deploy without the .git folder to your desired domain's document root:
#!/bin/bash
GIT_WORK_TREE=/home/mywebuser/public_html/staging git checkout -f
What the above script will do is change the output directory to ~/public_html/staging and place the the files from the repository into that directory, ignoring un-merged local changes, so it *will* overwrite your current files. This means if you are using git to develop your site, then you should *only* use git, never edit live files.
Set 0755 permissions on the post-receive hook script, and on the next push, it should clear out ~/public_html/staging and export the commit directly to that folder. You can do even more with this, for example you could push directly to public_html by changing the GIT_WORK_TREE path to the desired directory, copy assets from another folder, trigger manual backups before the staging site is updated, and many more things. For example, you could email yourself whenever the site gets updated by adding this line at the end of the post-receive hook script:
mail -s "Website updated at $(date)" $(<~/.contactemail)
More complex versions are available with lots more detail, such as this example post-receive contrib hook.
As always, feel free to send us a support request if you want to get this implemented on your website, we'll be happy to help!
Original credit for this technique goes to Abhijit Menon-Sen from toroid.org