Last active
May 29, 2018 17:36
-
-
Save fugudesign/4f571af1eb121f35c173fb83ffea48c0 to your computer and use it in GitHub Desktop.
PHP script to automatically deploy from github webhooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* AUTODEPLOY GITHUB REPO ON HOSTING | |
* | |
* @author Vincent Lalanne <[email protected]> | |
* | |
* | |
* Step 1: create the SSH key on your hosting | |
* https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 | |
* | |
* Step 2: add your ssh key on GitHub | |
* in your profile > settings > SSH and GPG keys | |
* or in your repo > settings > Deploy keys | |
* | |
* Step 3: clone your GitHub repo on your hosting and checkout the branch your want autodeploy | |
* | |
* Step 4: add and commit this script into a directory in your repo | |
* (you can use it as a submodule) | |
* | |
* Step 5: create a webhook on GitHub to call this script | |
* https://developer.github.com/webhooks/creating/ | |
* Set your url to this script, ex. http://mydomain.tld/gitploy/gitploy.php | |
* Choose "application/x-www-form-urlencoded" as content-type | |
* Choose "Just the push event." as trigger | |
* | |
* | |
* Alternately you can follow the instructions on this page (french): | |
* https://www.alsacreations.com/article/lire/1647-synchroniser-son-serveur-web-avec-github.html | |
* | |
* | |
* Troubleshootings | |
* | |
* If the script failed, you should edit/create the ~/.ssh/config file on your hosting | |
* and add those lines | |
* | |
* Host github.com | |
* Hostname ssh.github.com | |
* Port 443 | |
* | |
*/ | |
/** | |
* Check if the request comes from GitHub Webhook | |
* else return 401 unauthorized error | |
*/ | |
$headers = getallheaders(); | |
$userAgent = $headers['User-Agent']; | |
$deploy = true; | |
if ( empty($userAgent) || !preg_match('/^GitHub\-Hookshot\//i', $userAgent)) { | |
header('HTTP/1.0 401 Unauthorized'); | |
exit; | |
} | |
function getGitBranch() | |
{ | |
// Get the current ref from git HEAD | |
$shellOutput = shell_exec("cd ..; cat .git/HEAD"); | |
// Extract branch name | |
$branch = preg_replace('/^ref:\srefs\/heads\/([0-9a-zA-Z_\-]+)$/i', '$1', $shellOutput); | |
// Remove line breaks | |
$branch = preg_replace( "/\r|\n/", "", $branch ); | |
return !empty($branch) ? $branch : null; | |
} | |
if (!empty($_POST['payload'])) { | |
// If payload exists, get the pushed branch | |
// and compare it to the current branch | |
$payload = json_decode($_POST['payload']); | |
$branch_pushed = preg_replace('/refs\/heads\//i', '', $payload->ref); | |
$current_branch = getGitBranch(); | |
// Allow to deploy or not | |
$deploy = $current_branch == $branch_pushed; | |
} | |
echo "Need to deploy: " . ($deploy ? "true" : "false") . "\n"; | |
if ($deploy) { | |
/** | |
* The ordered command list your want to execute | |
*/ | |
$commands = [ | |
'whoami', | |
'cd ../', | |
'pwd', | |
'git checkout -f', | |
'git reset --hard', | |
'git pull', | |
'git status', | |
]; | |
/** | |
* Run all command and echo the result for logs | |
*/ | |
echo shell_exec(implode(' && ', $commands)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment