Skip to content

Instantly share code, notes, and snippets.

@andre3k1
Forked from marcelosomers/git-php-webhook.php
Last active November 7, 2023 09:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andre3k1/97dd45d0c8eba443789b to your computer and use it in GitHub Desktop.
Save andre3k1/97dd45d0c8eba443789b to your computer and use it in GitHub Desktop.
<?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 = "git@github.com: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());
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment