Deploying applications to development, staging and production never been so easy with GitHub Post-Receive Deployment Hook script!
Update your repo
-
Clone the script and copy it into your repo.
-
Do a search for
TODOto find the areas in the code you'll need to customize. In short, you'll need to specify the full server path to the repository you're wanting to update and the name of the specific branch you're wanting to update. -
Check the updated code into your repo.
Update GitHub
-
Go to your
GitHub repo>Admin>Teams>, selectMachinesfrom the list of Teams and add it to the respository. This will give the server access to the repository. -
Go to your
GitHub repo>Admin>Service Hooks, selectPost-Receive URLSand enter your hook URL like this:
Prepare the server
- SSH into the server.
cdinto one directory higher than your files will be served from.- Clone the repo onto the server.
git clone git@github.com:baminteractive/path-to-your-repo.git - Delete the directory your files will be served from and rename the cloned directory the same as the directory your deleted.
mv path/to/cloned/directory path/to/http/directory - Make sure to switch to the branch you're wanting to auto-deploy if it's not there already. Use
git fetchto pull down the branches from the remote server. - Set git repo directory permissions to 755
chmod -R 755 path/to/directory. Set any other directory permissions that are required for the site to function. - Set the
apacheuser as the owner of all files in the directory so the server is allowed to refresh the repochown -R apache:apache httpdocs - Deploy to the desired branch and it should update.
If you're having issues with the deploy process, the result of each deploy is logged into the /log/hook.log file.
GitHub provides Post-Receive Hooks to allow HTTP callback with a HTTP Post. We then create a script for the callback to deploy the systems automatically.
You will need to create branches like develop and staging in Git before proceeding into the configurations.
You then can have a brief look into hook.php, a WebHook example provided for you to experience how simple the configurations are.
<?php
require_once('class.GitHubHook.php');
// Initiate the GitHub Deployment Hook
$hook = new GitHubHook;
// Enable the debug log, kindly make `log/hook.log` writable
$hook->enableDebug();
// Adding `stage` branch to deploy for `staging` to path `/var/www/testhook/stage`
$hook->addBranch('stage', 'staging', '/var/www/stage');
// Adding `prod` branch to deploy for `production` to path `/var/www/testhook/prod`
$hook->addBranch('prod', 'production', '/var/www/prod', array('user@gmail.com'));
// Deploy the commits
$hook->deploy();
In the above, we enabled the debug log for messages with timestamp. You can disable this by commenting or removing the line $hook->enableDebug()
We have a staging site and a production site in this example. You can add more branches easily with $hook->addBranch() method if you have more systems to deploy.
We then use $hook->deploy() to deploy the systems.
We have enabled IP check to allow only GitHub hook addresses: 207.97.227.253, 50.57.128.197 to deploy the systems. We also return a 404 Not Found page when there is illegal access to the hook script.
For better security, rename this file to a bunch of random characters like a40b6cf7a5.php. I use this password generator to come up with random characters when I'm stuck.
We are trying to make developers life easier. Kindly fork this on GitHub and submit your pull requests to help us.

