Skip to content

Instantly share code, notes, and snippets.

@marcelosomers
Last active October 17, 2024 17:17
Show Gist options
  • Save marcelosomers/8305065 to your computer and use it in GitHub Desktop.
Save marcelosomers/8305065 to your computer and use it in GitHub Desktop.
A basic webhook for deploying updates to repos on Github to your local server
<?php
/**
* This script is for easily deploying updates to Github repos to your local server. It will automatically git clone or
* git pull in your repo directory every time an update is pushed to your $BRANCH (configured below).
*
* Read more about how to use this script at http://behindcompanies.com/2014/01/a-simple-script-for-deploying-code-with-githubs-webhooks/
*
* INSTRUCTIONS:
* 1. Edit the variables below
* 2. Upload this script to your server somewhere it can be publicly accessed
* 3. Make sure the apache user owns this script (e.g., sudo chown www-data:www-data webhook.php)
* 4. (optional) If the repo already exists on the server, make sure the same apache user from step 3 also owns that
* directory (i.e., sudo chown -R www-data:www-data)
* 5. Go into your Github Repo > Settings > Service Hooks > WebHook URLs and add the public URL
* (e.g., http://example.com/webhook.php)
*
**/
// Set Variables
$LOCAL_ROOT = "/path/to/repo/parent/directory";
$LOCAL_REPO_NAME = "REPO_NAME";
$LOCAL_REPO = "{$LOCAL_ROOT}/{$LOCAL_REPO_NAME}";
$REMOTE_REPO = "[email protected]:username/reponame.git";
$BRANCH = "master";
if ( $_POST['payload'] ) {
// Only respond to POST requests from Github
if( file_exists($LOCAL_REPO) ) {
// If there is already a repo, just run a git pull to grab the latest changes
shell_exec("cd {$LOCAL_REPO} && git pull");
die("done " . mktime());
} else {
// If the repo does not exist, then clone it into the parent directory
shell_exec("cd {$LOCAL_ROOT} && git clone {$REMOTE_REPO}");
die("done " . mktime());
}
}
?>
@brunodbo
Copy link

Thanks for this script! $_POST['payload'] was empty for me though (in fact, the whole $_POST didn't contain anything). The payload seemed to be in $HTTP_RAW_POST_DATA (also see https://github.com/gitlabhq/gitlabhq/issues/660).

@benhosmer
Copy link

It seems that github doesn't deliver the payload index any longer.

I used:

if ($_SERVER['HTTP_X_GITHUB_EVENT'] == 'push') {

Instead of

if ( $_POST['payload'] ) {

Which I got from the headers that github was sending:

Expect: 
User-Agent: GitHub-Hookshot/2e7ffb9
X-GitHub-Delivery: a4dca180-a1b6-11e4-8998-1146c8ddb8a0
X-GitHub-Event: push

@dmitryzuev
Copy link

I don't see $BRANCH using anywhere in code.

@gsg-chris
Copy link

Based on the code, how does the string $branch get used in this script?

@hawkins
Copy link

hawkins commented Jan 16, 2017

If you want to see examples with $BRANCH used, check out the forks. Some users have made some useful improvements to this script. You can see this script with $BRANCH included on my fork as an example.

@Bachsau
Copy link

Bachsau commented Jan 28, 2023

git pull is simple but easy to break. This is better:

git fetch -q github main
git reset --hard github/main
git clean -d -x

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