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 hidden or 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 Handle auto pulling other branches | |
| // TODO Check current branch is checked out before pulling | |
| // TODO Re-write as class | |
| // TODO Store sites in separate config file | |
| // TODO Allow config file to be in folder above root or 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' | |
| ); | |
| $sites['Yell-Marketing-Site'] = array( | |
| 'dir' => 'yell-marketing.hmn.md', | |
| 'ref' => 'master' | |
| ); | |
| define( 'ABSPATH', dirname( dirname( __FILE__ ) ) ); | |
| // Is the payload 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 ); | |
| } | |
| function git_pull( $dir ) { | |
| return shell_exec( 'cd ' . ABSPATH . '/' . $dir . ' ; git pull ; git submodule update --init --recursive' ); | |
| } | |
| function git_checkout( $ref ) { | |
| } | |
| function get_is_branch_checked_out( $branch ) { | |
| } | |
| function notify( $response, $site ) { | |
| global $github; | |
| mail( | |
| 'tom@humanmade.co.uk, joe@humanmade.co.uk', | |
| '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