Skip to content

Instantly share code, notes, and snippets.

@hawkins
Forked from marcelosomers/git-php-webhook.php
Last active April 25, 2022 13:30

Revisions

  1. hawkins revised this gist Jan 22, 2016. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions git-php-webhook.php
    Original file line number Diff line number Diff line change
    @@ -20,22 +20,25 @@
    $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";
    $USERNAME = "GitHubUserName"
    $REPO_NAME = "repo-name"
    $BRANCH = "master";
    $REMOTE_REPO = "git@github.com:{$USERNAME}/{$REPO_NAME}.git";

    if ( $_POST['payload'] ) {
    // Only respond to POST requests from Github

    if ($_SERVER['HTTP_X_GITHUB_EVENT'] == 'push') {
    // Only respond to push webhooks 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");
    shell_exec("cd {$LOCAL_REPO} && git pull origin {$BRANCH}");

    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}");
    shell_exec("cd {$LOCAL_ROOT} && git clone {$REMOTE_REPO} {$BRANCH}");

    die("done " . mktime());
    }
  2. @marcelosomers marcelosomers revised this gist Jan 8, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions git-php-webhook.php
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@
    * 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. @marcelosomers marcelosomers revised this gist Jan 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-php-webhook.php
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    $LOCAL_REPO_NAME = "REPO_NAME";
    $LOCAL_REPO = "{$LOCAL_ROOT}/{$LOCAL_REPO_NAME}";
    $REMOTE_REPO = "git@github.com:username/reponame.git";
    $BRANCH = "master";
    $BRANCH = "master";

    if ( $_POST['payload'] ) {
    // Only respond to POST requests from Github
  4. @marcelosomers marcelosomers renamed this gist Jan 7, 2014. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions php-webhook.php → git-php-webhook.php
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,16 @@
    <?php
    /**
    * This script is for easily deploying updates to Github repos to your local server
    * 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).
    *
    * 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 (e.g., 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)
    * 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)
    *
    **/

  5. @marcelosomers marcelosomers revised this gist Jan 7, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions php-webhook.php
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    * 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 (e.g., 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)
    *
    **/

  6. @marcelosomers marcelosomers revised this gist Jan 7, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion php-webhook.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,8 @@
    * 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 (e.g., sudo chown -R www-data:www-data)
    */
    *
    **/

    // Set Variables
    $LOCAL_ROOT = "/path/to/repo/parent/directory";
  7. @marcelosomers marcelosomers created this gist Jan 7, 2014.
    37 changes: 37 additions & 0 deletions php-webhook.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?php
    /**
    * This script is for easily deploying updates to Github repos to your local server
    *
    * 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 (e.g., sudo chown -R www-data:www-data)
    */

    // 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());
    }
    }

    ?>