Skip to content

Commit

Permalink
Issue mozfr#400: add github pushes integration hook
Browse files Browse the repository at this point in the history
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
pascalchevrel committed Dec 8, 2014
1 parent 4236e4c commit 9ad6464
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/config/config.ini-dist
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ l10nwebservice = "https://l10n.mozilla-community.org/~flod/mozilla-l10n-query/"

; Flag to know if we are working in production more or development mode
dev=false

; Github secret key for continuous integration on Beta site
github_key=putsecretkeyhere
72 changes: 72 additions & 0 deletions web/github_hook.php
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");
}

0 comments on commit 9ad6464

Please sign in to comment.