Created
June 26, 2012 15:19
-
-
Save willmot/2996390 to your computer and use it in GitHub Desktop.
Auto git pull on every push to github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// TODO Handle tagged releases | |
// TODO Check current branch is checked out before pulling | |
// TODO Store sites in separate config file | |
// TODO Allow config file to be in folder above root. | |
// TODO Allow updating via $_GET? | |
// TODO Make sure sane git status before pulling? | |
// You can't view this file directly | |
if ( ! isset( $_POST['payload'] ) ) { | |
header( 'Status: 301' ); | |
header( 'Location: http://hmn.md/' ); | |
exit; | |
} | |
global $github; | |
$github = json_decode( $_POST['payload'] ); | |
/** | |
* Array of sites and their github details | |
* | |
* Format is array( [github repo name] => array( [site directory], [pull from branch_name or tag] ); | |
* | |
*/ | |
$sites = array(); | |
$sites['Human-Made-website'] = array( | |
'dir' => 'humanmade', | |
'ref' => 'master' | |
); | |
define( 'ABSPATH', dirname( dirname( __FILE__ ) ) ); | |
// Check the payload is for one of the configured sites | |
if ( isset( $sites[$github->repository->name] ) && $site = $sites[$github->repository->name] ) { | |
// If the directory doesn't exist then bail | |
if ( ! file_exists( ABSPATH . '/' . $site['dir'] ) ) | |
return; | |
$response = ''; | |
// If we're set to pull from a branch and that branch was part of the payload | |
if ( $site['ref'] != 'tag' && $github->ref == 'refs/heads/' . $site['ref'] ) | |
// Then Git Pull | |
$response = git_pull( $site['dir'] ); | |
// Send a notification | |
if ( $response ) | |
notify( $response, $site ); | |
} | |
/** | |
* Perform a git pull & git submodule update command. | |
* | |
* @access public | |
* @param string $dir | |
* @return void | |
*/ | |
function git_pull( $dir ) { | |
return shell_exec( 'cd ' . escshellarg( ABSPATH . '/' . $dir ) . ' ; git pull ; git submodule update --init --recursive' ); | |
} | |
/** | |
* Send an email notification with details of the payload that was just auto pulled | |
* | |
* @access public | |
* @param string $response | |
* @param array $site | |
* @return void | |
*/ | |
function notify( $response, $site ) { | |
global $github; | |
mail( | |
'example@email.com', | |
'Git Callback from ' . $site['dir'], | |
' | |
<p><a href="mailto:' . reset( $github->commits )->author->email . '">' . reset( $github->commits )->author->name . '</a> pushed to <a href="http://' . $site['dir'] . '">' . $site['dir'] . '</a></p> | |
<p>Commit: <a href="' . reset( $github->commits )->url . '">' . reset( $github->commits )->id . '</a></p> | |
<p>Description:</p> | |
<p><em>' . reset( $github->commits )->message . '</em></p> | |
<p>Response:</p> | |
<p><em>' . $response . '</em></p> | |
<p>Full Details:</p> | |
<p><pre>' . print_r( $github, true ) . '</pre></p> | |
', | |
'Content-type: text/html; charset=iso-8859-1' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment