Setting up GIT deployment on Nexcess hosting

Setting up GIT deployment for hosts that do not have it baked in to the control panel can be daunting if you’ve never done it before. After Googling around quite a bit I’ve put together a step-by-step process that makes it fairly straight forward.

Step 1: Connect and gather directory information

The first step is to make sure you have SSH access to the host machine as well as confirming that GIT is installed and available. Once you have done this you can begin by connecting to the host via SSH:

ssh SSHUSERNAME@SSHDOMAINorIP

Once connected, run this command to get the directory information that will be used later on:

pwd

This command will list the path to the current directory should be something like /home/USERNAME/

Step 2: Create directories and initialize GIT

Create the directory for your new, empty GIT repository. Replace SITENAME with the domain name for the site or whatever you want it to be:

mkdir repo/SITENAME.git

Then switch to that newly created directory and initialize GIT:

cd repo/SITENAME.git
git init --bare

Step 3: Create post-receive hook to deploy code changes

Hooks in GIT are a way to execute scripts after specific events occur. In this case we want to use the ‘post-receive’ hook. This hook runs after everything else has occurred during a GIT push.

Switch to the GIT directory’s `hooks` subdirectory

cd hooks

Create and open a file named `post-receive`

nano post-receive

This will open a nano editor within Terminal for editing the `post-receive` file.
Grab the contents of `post-receive` in this GIST and update them to match your data:

Paste the updated contents in to the nano document then save and close the document.

ctrl+o

Hit enter to confirm the file name which should be `post-receive`.

ctrl+x

Finally, set the correct permissions on the file so that it can process the requests.

chmod +x post-receive