Created
September 25, 2014 08:20
-
-
Save shadyvb/d721c1a316dba39e5d45 to your computer and use it in GitHub Desktop.
GitHub Webhook Deployment handler
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 | |
header("Content-Type: text/plain"); | |
// Decode the payload | |
$payload = json_decode( $_POST ); | |
# Include config file if found | |
@include( __DIR__ . '/deploy-config.php' ); | |
// Ability to choose a custom branch to track / deploy from, in the webhook registered URL | |
// ie: http://staging.com/pull.php?branch=somecustomone | |
if ( empty( $branch ) ) { | |
$branch = ! empty( $_REQUEST['branch'] ) ? $_REQUEST['branch'] : 'staging'; | |
} | |
# check that this payload is sent to the branch we're targetting | |
$commit_branch = preg_match( '#refs/heads/(.*)#', $payload['ref'], $matches ) ? $matches[1] : null; | |
if ( $commit_branch !== $branch ) { | |
die( 'Not pushed to the tracking branch.'); | |
} | |
# execute before_deployment logic if found | |
function_exists( 'before_deployment' ) && before_deployment(); | |
# update and checkout the repo tracked branch | |
passthru("exec 2>&1 && git checkout -f $branch && git pull"); | |
# run db migrations if possible | |
passthru("exec 2>&1 && rake db:migrate"); | |
# execute after_deployment logic if found | |
function_exists( 'after_deployment' ) && after_deployment(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment