Skip to content

Instantly share code, notes, and snippets.

@shadyvb
Created September 25, 2014 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shadyvb/d721c1a316dba39e5d45 to your computer and use it in GitHub Desktop.
Save shadyvb/d721c1a316dba39e5d45 to your computer and use it in GitHub Desktop.
GitHub Webhook Deployment handler
<?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