forked from mozfr/transvision
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue mozfr#400: add github pushes integration hook
Action needed on the beta server after this push: - add secret key in config.ini - create a file web/github_log.txt with the user www-data:www-data We don't keep the log file in app/logs/ because we want to be able to access the date of the last update from GitHub into the browser
- Loading branch information
1 parent
4236e4c
commit 9ad6464
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/* Webhook to update a repo for each push on GitHub. */ | ||
|
||
date_default_timezone_set('Europe/Paris'); | ||
|
||
// app variables | ||
$app_root = realpath(__DIR__ . '/../'); | ||
$composer = $app_root . '/composer.phar'; | ||
|
||
// git variables | ||
$branch = 'master'; | ||
$header = 'HTTP_X_HUB_SIGNATURE'; | ||
$secret = parse_ini_file($app_root . '/app/config/config.ini')['github_key']; | ||
|
||
// Logging function to output content to /github_log.txt | ||
function logHookResult($message , $success = false) { | ||
$log_headers = "$message\n"; | ||
if (! $success) { | ||
foreach ($_SERVER as $header => $value) { | ||
$log_headers .= "$header: $value \n"; | ||
} | ||
} | ||
file_put_contents(__DIR__ . '/github_log.txt', $log_headers); | ||
} | ||
|
||
// CHECK: Download composer in the app root if it is not already there | ||
if (! file_exists($composer)) { | ||
file_put_contents( | ||
$composer, | ||
file_get_contents('https://getcomposer.org/composer.phar') | ||
); | ||
} | ||
|
||
if (isset($_SERVER[$header])) { | ||
$validation = hash_hmac( | ||
'sha1', | ||
file_get_contents("php://input"), | ||
$secret | ||
); | ||
|
||
if ($validation == explode('=', $_SERVER[$header])[1]) { | ||
// Pull latest changes | ||
exec("git checkout $branch ; git pull origin $branch"); | ||
|
||
// Install or update dependencies | ||
if (file_exists($composer)) { | ||
chdir($app_root); | ||
|
||
// www-data does not have a HOME or COMPOSER_HOME, create one | ||
if (! is_dir("{$app_root}/cache/.composer")) { | ||
mkdir("{$app_root}/cache/.composer"); | ||
} | ||
|
||
putenv("COMPOSER_HOME={$app_root}/cache/.composer"); | ||
|
||
if (file_exists($app_root . '/vendor')) { | ||
exec("php {$composer} update > /dev/null 2>&1"); | ||
} else { | ||
exec("php {$composer} install > /dev/null 2>&1"); | ||
} | ||
} | ||
|
||
// Delete cache | ||
exec("rm {$app_root}/cache/*.cache > /dev/null 2>&1"); | ||
|
||
logHookResult('Last update: ' . date('d-m-Y H:i:s'), true); | ||
} else { | ||
logHookResult('Invalid GitHub secret'); | ||
} | ||
} else { | ||
logHookResult("{$header} header missing, define a secret key for your project in GitHub"); | ||
} |