Skip to content

Commit

Permalink
cleanup and release
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Kwang Chin committed Jul 26, 2012
1 parent 36e458e commit a03c0af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
6 changes: 0 additions & 6 deletions CHANGELOG

This file was deleted.

12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GitHubHook ChangeLog
====================

Version 1.1, 26 July, 2012
--------------------------
* Added support for EC2 Load Balancers - @acidjazz
* Updated GitHub's public hook IP addresses - @acidjazz
* Added optional logging upon 404 - @acidjazz

Version 1.0, 22 March, 2012
---------------------------
* Initial Release
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ We then use `$hook->deploy()` to deploy the systems.

### Security

Worry about securities? 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.
Worry about securities? We have enabled IP check to allow only GitHub hook addresses: `207.97.227.253`, `50.57.128.197`, `108.171.174.178` to deploy the systems. We also return a `404 Not Found` page when there is illegal access to the hook script.

For better security, try hiding this hook script in deep directories like `http://www.example.com/let/us/play/hide/and/seek/` and/or renaming the `hook.php` file into `a40b6cf7a5.php`.

### For Developers

We are trying to make developers life easier. Kindly fork this on GitHub and submit your pull requests to help us.
We are trying to make developers life easier. Kindly fork this on GitHub and submit your pull requests to help us.
47 changes: 25 additions & 22 deletions class.GitHubHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* GitHub Post-Receive Deployment Hook.
*
*
* @author Chin Lee <kwangchin@gmail.com>
* @copyright Copyright (C) 2012 Chin Lee
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
Expand All @@ -17,74 +17,76 @@ class GitHubHook
* @since 1.0
*/
private $_remoteIp = '';

/**
* @var object Payload from GitHub.
* @since 1.0
*/
private $_payload = '';

/**
* @var boolean Log debug messages.
* @since 1.0
*/
private $_debug = FALSE;

/**
* @var array Branches.
* @since 1.0
*/
private $_branches = array();

/**
* @var array githubs updated ip's used.
* @since 1.0
* @var array GitHub's IP addresses for hooks.
* @since 1.1
*/
private $_ips = array('207.97.227.253', '50.57.128.197', '108.171.174.178');

/**
* Constructor.
* @since 1.0
*/
function __construct() {
/* support for ec2 load balancers */
/* Support for EC2 load balancers */
if (
isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)
) {
$this->_remoteIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$this->_remoteIp = $_SERVER['REMOTE_ADDR'];
}

if (isset($_POST['payload'])) {
$this->_payload = json_decode($_POST['payload']);
} else {
$this->_notFound('payload not available');
$this->_notFound('Payload not available from: ' . $this->_remoteIp);
}
}

/**
* centralize our 404
* @since 1.0
* Centralize our 404.
* @param string $reason Reason of 404 Not Found.
* @since 1.1
*/

private function _notFound($reason=false) {
if ($reason !== false) {
private function _notFound($reason = NULL) {
if ($reason !== NULL) {
$this->log($reason);
}

header('HTTP/1.1 404 Not Found');
echo '404 Not Found.';
return false;
exit;
}

/**
* Enable log of debug messages.
* @since 1.0
*/
public function enableDebug() {
$this->_debug = TRUE;
}

/**
* Add a branch.
* @param string $name Branch name, defaults to 'master'.
Expand All @@ -101,7 +103,7 @@ public function addBranch($name = 'master', $title = 'development', $path = '/va
'author' => $author
);
}

/**
* Log a message.
* @param string $message Message to log.
Expand All @@ -112,7 +114,7 @@ public function log($message) {
file_put_contents('log/hook.log', '[' . date('Y-m-d H:i:s') . '] - ' . $message . PHP_EOL, FILE_APPEND);
}
}

/**
* Deploys.
* @since 1.0
Expand All @@ -121,12 +123,13 @@ public function deploy() {
if (in_array($this->_remoteIp, $this->_ips)) {
foreach ($this->_branches as $branch) {
if ($this->_payload->ref == 'refs/heads/' . $branch['name']) {

$this->log('Deploying to ' . $branch['title'] . ' server');
shell_exec('./deploy.sh ' . $branch['path'] . ' ' . $branch['name']);
}
}
} else {
$this->_notFound('ip address not recognized : '.$this->_remoteIp);
$this->_notFound('IP address not recognized: ' . $this->_remoteIp);
}
}
}

0 comments on commit a03c0af

Please sign in to comment.