Skip to content

Instantly share code, notes, and snippets.

@phedoreanu
Last active January 25, 2024 17:34
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save phedoreanu/11321236 to your computer and use it in GitHub Desktop.
Save phedoreanu/11321236 to your computer and use it in GitHub Desktop.
git PHP webhook
sudo mkdir -m 0700 /var/www/.ssh
sudo chown -R apache:apache /var/www/.ssh
sudo -u apache ssh-keygen (empty passphrase)
paste public key into repo manager
(git-repo) sudo -u apache git pull origin branch (this will create /var/www/.ssh/known_hosts)
call git_hook.php?branch=xxx
<?php
$gh_ips = array('171.149.246.236', '95.92.138.4');
if (in_array($_SERVER['REMOTE_ADDR'], $gh_ips) === false) {
header('Status: 403 Your IP is not on our list; bugger off', true, 403);
mail('root', 'Unfuddle hook error: bad ip', $_SERVER['REMOTE_ADDR']);
die(1);
}
$BRANCH = $_GET['branch'];
if (!empty($BRANCH)) {
$output = shell_exec("cd /srv/www/git-repo/; git pull origin {$BRANCH};");
echo "<pre>$output</pre>";
}
die("done " . mktime());
Copy link

ghost commented Jun 11, 2014

I know this is a silly question, but where do I access the repo manager, as part of this step above:
paste public key into repo manager

@dark991
Copy link

dark991 commented Jul 21, 2014

@lowgos
Copy link

lowgos commented Nov 25, 2016

I'm trying to understand all of this, I've been struggling on all of this for like 4 hours now. I'm just trying to catch the webhook from my Github repo, and if I catch it : launch a script to "git pull" ! I know a bit of php so I thought it wouldn't be too difficult but know : either apache doesn't have the right permissions to perform any of my commands (like exec or anything else), so I came here to see that with a customization of ssh I could maybe overcome this difficulty, and maybe, but I don't understand the "call git_hook.php?branch=xxx" part... Do I have to call "myserver.com/.../git_hook.php?branch=xxx" in the webhook ?
I'm just lost and tired so I really like to make a clear, simple solution for webhooks-autopull ...

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