864Pulling a GitHub Repository with Tokens & Webhooks

Here is how to sync a Wordpress Theme from Github to your own server:

Create a Personal Access Token on GitHub

Webhook on GitHub

https://www.trembl.org/update-my-website.php

Webhook on GitHub for .htaccess protected directory:

https://username:passwd@www.trembl.org/update-my-website.php

Initialise

Go to your tmp directory, init the git repo and pull the repo

cd /my/local/tmp/directory/
git init        // initialize git
git reset --hard
git pull https://{$token}@github.com/{$repo}

Update

The update-my-website.php should look something like this:

<?php

// path to local tmp directory
$temp_dir = '/my/local/tmp/directory/';

// path to local theme directory
$theme_dir = "/my/local/theme/directory/";

// access token from GitHub
$token = 'abcdefghijk';

// Repo
$repo = 'github.com/trembl/SuperNiceRepo.git';

// - - - - - 

// change to local git directory
chdir($temp_dir);

// resets local git
exec('git reset --hard 2>&1') . ", ";

// get latest from github & store it in $msg
echo exec("git pull https://<span style="background:red">$token</span>@$repo 2>&1");
$msg = exec("git pull https://$token@$repo 2>&1");

if ($msg != "Already up to date.") {
  // Only copy if the git repo actually changed

  // delete theme dir
  echo "\n\nDeleting Theme Directory.\n";
  echo exec("rm -rf $theme_dir*");  // * deletes all files within the directory

  // Copying Theme from $temp_dir to $theme_dir
  echo "\n\nCopying Theme from tmp folder to wp-content.\n";

  echo exec("cp -a $temp_dir/* $theme_dir");

  // cp -a $temp_dir/* copies everything _except_ invisible files, like .git

} else {
  echo($msg);
}

?>

Q & A

update-my-website.php protected by .htaccess?
860 Access a URL behind htaccess/htpasswd

Protect update.php ONLY with .htaccess?
940 Protecting a Single File with .htaccess

I get the following error message: Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)/
That means git is not initalized. Run git init

860Access a URL behind htaccess/htpasswd

Access a htaccess/htpasswd protected URL programmatically:

https://username:passwd@www.trembl.org/protected/area/

See also: Pulling a GitHub Repository with Tokens & Webhooks