Skip to content

Commit 4465b43

Browse files
committed
first commit
0 parents  commit 4465b43

File tree

7 files changed

+200
-0
lines changed

7 files changed

+200
-0
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GitHubHook ChangeLog
2+
====================
3+
4+
Version 1.0, 22 March, 2012
5+
---------------------------
6+
Initial Release

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2012 Chin Lee
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## GitHub Post-Receive Deployment Hook
2+
3+
Deploying applications to development, staging and production never been so easy with GitHub Post-Receive Deployment Hook script!
4+
5+
### Installation
6+
7+
Clone the script:
8+
9+
<pre><code>$ <strong>git clone https://github.com/kwangchin/GitHubHook.git</strong>
10+
</code></pre>
11+
12+
Go to your `GitHub repo` &gt; `Admin` &gt; `Service Hooks`, select `Post-Receive URLS` and enter your hook URL like this:
13+
14+
![GitHub Post-Receive URLs](http://s3.kcblog.net/images/GitHubHook-01.png)
15+
16+
### How It Works
17+
18+
GitHub provides [Post-Receive Hooks](http://help.github.com/post-receive-hooks/) to allow HTTP callback with a HTTP Post. We then create a script for the callback to deploy the systems automatically.
19+
20+
You will need to create branches like `stage` and `prod` in Git before proceeding into the configurations.
21+
22+
You then can have a brief look into `hook.php`, a WebHook example provided for you to experience how simple the configurations are.
23+
24+
<pre><code>&lt;?php
25+
require_once('class.GitHubHook.php');
26+
27+
// Initiate the GitHub Deployment Hook
28+
$hook = new GitHubHook;
29+
30+
// Enable the debug log, kindly make `log/hook.log` writable
31+
$hook-&gt;enableDebug();
32+
33+
// Adding `stage` branch to deploy for `staging` to path `/var/www/testhook/stage`
34+
$hook-&gt;addBranch('stage', 'staging', '/var/www/stage');
35+
36+
// Adding `prod` branch to deploy for `production` to path `/var/www/testhook/prod`
37+
$hook-&gt;addBranch('prod', 'production', '/var/www/prod', array('user@gmail.com'));
38+
39+
// Deploy the commits
40+
$hook-&gt;deploy();
41+
</code></pre>
42+
43+
In this example, we enabled the debug log for messages with timestamp. You can disable this by commenting or removing the line `$hook->enableDebug()`
44+
45+
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.
46+
47+
We then use `$hook->deploy()` to deploy the systems.
48+
49+
##
50+
51+
### Security
52+
53+
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.
54+
55+
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`.
56+
57+
### For Developers
58+
59+
We are trying to make developers life easier. Kindly fork this on GitHub and submit your pull requests to help us.

class.GitHubHook.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
error_reporting(0);
3+
4+
/**
5+
* GitHub Post-Receive Deployment Hook.
6+
*
7+
* @author Chin Lee <kwangchin@gmail.com>
8+
* @copyright Copyright (C) 2012 Chin Lee
9+
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
10+
* @version 1.0
11+
*/
12+
13+
class GitHubHook
14+
{
15+
/**
16+
* @var string Remote IP of the person.
17+
* @since 1.0
18+
*/
19+
private $_remoteIp = '';
20+
21+
/**
22+
* @var object Payload from GitHub.
23+
* @since 1.0
24+
*/
25+
private $_payload = '';
26+
27+
/**
28+
* @var boolean Log debug messages.
29+
* @since 1.0
30+
*/
31+
private $_debug = FALSE;
32+
33+
/**
34+
* @var array Branches.
35+
* @since 1.0
36+
*/
37+
private $_branches = array();
38+
39+
40+
41+
/**
42+
* Constructor.
43+
* @since 1.0
44+
*/
45+
function __construct() {
46+
$this->_remoteIp = $_SERVER['REMOTE_ADDR'];
47+
48+
if (isset($_POST['payload'])) {
49+
$this->_payload = json_decode($_POST['payload']);
50+
} else {
51+
header('HTTP/1.1 404 Not Found');
52+
echo '404 Not Found.';
53+
exit;
54+
}
55+
}
56+
57+
/**
58+
* Enable log of debug messages.
59+
* @since 1.0
60+
*/
61+
public function enableDebug() {
62+
$this->_debug = TRUE;
63+
}
64+
65+
/**
66+
* Add a branch.
67+
* @param string $name Branch name, defaults to 'master'.
68+
* @param string $title Branch title, defaults to 'development'.
69+
* @param string $path Relative path to development directory, defaults to '/var/www/'.
70+
* @param array $author Contains authorized users' email addresses, defaults to everyone.
71+
* @since 1.0
72+
*/
73+
public function addBranch($name = 'master', $title = 'development', $path = '/var/www/', $author = array()) {
74+
$this->_branches[] = array(
75+
'name' => $name,
76+
'title' => $title,
77+
'path' => $path,
78+
'author' => $author
79+
);
80+
}
81+
82+
/**
83+
* Log a message.
84+
* @param string $message Message to log.
85+
* @since 1.0
86+
*/
87+
public function log($message) {
88+
if ($this->_debug) {
89+
file_put_contents('log/hook.log', '[' . date('Y-m-d H:i:s') . '] - ' . $message . PHP_EOL, FILE_APPEND);
90+
}
91+
}
92+
93+
/**
94+
* Deploys.
95+
* @since 1.0
96+
*/
97+
public function deploy() {
98+
if ($this->_remoteIp == '207.97.227.253' || $this->_remoteIp == '50.57.128.197') {
99+
foreach ($this->_branches as $branch) {
100+
if ($this->_payload->ref == 'refs/heads/' . $branch['name']) {
101+
$this->log('Deploying to ' . $branch['title'] . ' server');
102+
shell_exec('./deploy.sh ' . $branch['path'] . ' ' . $branch['name']);
103+
}
104+
}
105+
}
106+
}
107+
}

deploy.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
cd $1
4+
git pull origin $2

hook.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once('class.GitHubHook.php');
3+
4+
// Initiate the GitHub Deployment Hook
5+
$hook = new GitHubHook;
6+
7+
// Enable the debug log, kindly make `log/hook.log` writable
8+
$hook->enableDebug();
9+
10+
// Adding `stage` branch to deploy for `staging` to path `/var/www/testhook/stage`
11+
$hook->addBranch('stage', 'staging', '/var/www/stage');
12+
13+
// Adding `prod` branch to deploy for `production` to path `/var/www/testhook/prod` limiting to only `user@gmail.com`
14+
$hook->addBranch('prod', 'production', '/var/www/prod', array('user@gmail.com'));
15+
16+
// Deploy the commits
17+
$hook->deploy();

log/hook.log

Whitespace-only changes.

0 commit comments

Comments
 (0)