Adaptable Github Webhook handler for PHP.
Features:
- Easy config setting up and configuration using a
config.jsonfile. - Multiple repositories support, with multiple branches and paths.
- Independant event handling.
- Support to commands previous and after pulling.
- Public file to access internal code preventing direct code access.
Basic software is needed:
- Git
- Web Server (tested with Apache 2.4)
- PHP 7 and over
If you're updating private repositories you might need github credentials It's highly recommended to use an SSH method.
Creating an SSH directory for www-data to git@github.com
sudo mkdir /var/www/.ssh
sudo chown -R www-data:www-data /var/www/.ssh
sudo chmod -R 0700 /var/www/.sshLog in with www-data to generate and install ssh keys
sudo -su www-data
ssh-keygen -t rsa -b 4096 -C "email@example.com"Name the key as /var/www/.ssh/github.
You might want to use a keyphrase that requires aditional settings not covered in this guide.
Open the file /var/www/.ssh/github.pub and copy its contents.
Go to https://github.com/settings/ssh/new and paste name the key and paste the content there.
Create a ~/.ssh/config to default loading key when server starts.
touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/configPut the following in the created file
Host github.com
User git
IdentityFile ~/.ssh/github
Test the connection
ssh -T git@github.comClone the private repository
git clone git@github.com:{user}/{repo} /path/to/local/repoOr change current repository remote location
git remote set-url origin git@github.com:{user}/{repo}Test by pulling the latest version from remote repository
cd /path/to/local/repo
git pullClone repository
git clone https://github.com/francerz/php-github-webhook /path/to/php-github-webhookInstall the composer dependencies
cd /path/to/php-github-webhook
composer installAdd Alias to public directory
Alias /.github /path/to/php-github-webhook/public
Setup config.json
cp /path/to/php-github-webhook/config.json.dist /path/to/php-github-webhook/config.json
nano /path/to/php-github-webhook/config.jsonSet the repositories by full_name, branch, path and action events.
Add Webhook to Github Repository by going to: {repository} > Settings > Webhooks > Add webhook
Capture your credentials, then fill the form:
- Payload URL:
https://my.site.com/.github/hook.php - Content-Type: application/json
- Secret: {any key you need, prefer a single use random string}
- Select the events you want to handle.
- Press button Add Webhook.
THAT'S IT!!!