Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created January 11, 2011 20:56
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save badsyntax/775125 to your computer and use it in GitHub Desktop.
Save badsyntax/775125 to your computer and use it in GitHub Desktop.
An example PHP & BASH Post-Receive github web hook to package projects
#! /usr/bin/env bash
#clone the repo
git clone -q "${1}" "clones/${2}"
cd "clones/${2}"
#update the submodules (how do we handle errors here?)
git submodule --quiet update --init --recursive
cd ../../
#tar up the directory
tar --exclude=.git -cf "archives/${2}-master.tar" "clones/${2}"
#remove the cloned repo
rm -rf "clones/${2}"
echo "A new archive has been created at /archives/${2}-master.tar" | mail -s 'Github project build successul!' 'your@email.com'
<?php
error_reporting(0);
try
{
// Decode the payload json string
$payload = json_decode($_REQUEST['payload']);
}
catch(Exception $e)
{
exit(0);
}
// Pushed to master?
if ($payload->ref === 'refs/heads/master')
{
// Log the payload object
file_put_contents('logs/github.txt', print_r($payload, TRUE), FILE_APPEND);
// Prep the URL - replace https protocol with git protocol to prevent 'update-server-info' errors
$url = str_replace('https://', 'git://', $payload->repository->url);
// Run the build script as a background process
`./build.sh {$url} {$payload->repository->name} > /dev/null 2>&1 &`;
}
?>
@matzipan
Copy link

Really nice. thank you!

@nicolae-olariu
Copy link

Thank you! But one quick note here, we've made it work by using file_get_contents('php://input') instead of $_REQUEST['payload'], if anyone else is having issues with it.

@wogsland
Copy link

Thanks @nicolae-olariu!

@i-make-robots
Copy link

Thank you @nicolae-olariu, your file_get_contents() tip helped me fix my webhook in https://github.com/marginallyclever/Robot-Overlord. Kudos!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment