Using github webhook
Getting started with Github webhook
- Read a Github API
- Build a webhook server/script
- install PHP GitHub webhook - PHP example
- install webhook server using composer
<?php require(__DIR__ . "/vendor/autoload.php"); use GitHubWebhook\Handler; $handler = new Handler("<your secret>", __DIR__); if($handler->handle()) { echo "OK"; } else { echo "Wrong secret"; }
Register webhook service at github project
Settings > Webhooks & services > Add webhook
item description Payload URL Enter full URL to your webhook script Content type Can be either “application/json” or “application/x-www-form-urlencoded” Secret Same secret you pass to constructor of Handler object PHP GitHub webhook handler
function description __construct($secret, $gitDir, $remote = null) Constructor. Constructs new webhook handler that will verify that requests coming to it are signed with $secret. $gitDir must be set to path to git repo that must be updated. Optional $remote specifies which remote should be pulled. getData() Getter. After successful validation returns parsed array of data in payload. Otherwise returns null. getDelivery() Getter. After successful validation returns unique delivery number coming from GitHub. Otherwise returns null. getEvent() Getter. After successful validation returns name of event that triggered this webhook. Otherwise returns null. getGitDir() Getter. Returns $gitDir that was passed to constructor. getGitOutput Getter. After successful validation returns output of git as array of lines. Otherwise returns null. getRemote() Getter. Returns $remote that was passed to constructor. getSecret() Getter. Returns $secret that was passed to constructor. handle() Handle the request. Validates that incoming request is signed correctly with $secret and executes git pull upon successful validation. Returns true on succes or false if validation failed. validate() Validate request only. Returns boolean that indicates whether the request is correctly signed by $secret.