deploy.php
Simple PHP Git deploy script
ACCESS DENIED!');
}
if (SECRET_ACCESS_TOKEN === 'BetterChangeMeNowOrSufferTheConsequences') {
die("You're suffering the consequences!
Change the SECRET_ACCESS_TOKEN from it's default value!
");
}
?>
Checking the environment ...
Running as .
BACKUP_DIR `%s` does not exists or is not writeable.
', BACKUP_DIR));
}
}
if (defined('USE_COMPOSER') && USE_COMPOSER === true) {
$requiredBinaries[] = 'composer --no-ansi';
}
foreach ($requiredBinaries as $command) {
$path = trim(shell_exec('which '.$command));
if ($path == '') {
die(sprintf('
%s not available. It needs to be installed on the server for this script to work.
', $command));
} else {
$version = explode("\n", shell_exec($command.' --version'));
printf('
%s : %s'."\n"
, $path
, $version[0]
);
}
}
?>
Environment OK.
Deploying
to ...
%s'
, TMP_DIR
, TMP_DIR
, VERSION_FILE
);
}
// Backup the TARGET_DIR
// without the BACKUP_DIR for the case when it's inside the TARGET_DIR
if (defined('BACKUP_DIR') && BACKUP_DIR !== false) {
$commands[] = sprintf(
"tar --exclude='%s*' -czf %s/%s-%s-%s.tar.gz %s*"
, BACKUP_DIR
, BACKUP_DIR
, basename(TARGET_DIR)
, md5(TARGET_DIR)
, date('YmdHis')
, TARGET_DIR // We're backing up this directory into BACKUP_DIR
);
}
// Invoke composer
if (defined('USE_COMPOSER') && USE_COMPOSER === true) {
$commands[] = sprintf(
'composer --no-ansi --no-interaction --no-progress --working-dir=%s install %s'
, TMP_DIR
, (defined('COMPOSER_OPTIONS')) ? COMPOSER_OPTIONS : ''
);
if (defined('COMPOSER_HOME') && is_dir(COMPOSER_HOME)) {
putenv('COMPOSER_HOME='.COMPOSER_HOME);
}
}
// ==================================================[ Deployment ]===
// Compile exclude parameters
$exclude = '';
foreach (unserialize(EXCLUDE) as $exc) {
$exclude .= ' --exclude='.$exc;
}
// Deployment command
$commands[] = sprintf(
'rsync -rltgoDzvO %s %s %s %s'
, TMP_DIR
, TARGET_DIR
, (DELETE_FILES) ? '--delete-after' : ''
, $exclude
);
// =======================================[ Post-Deployment steps ]===
// Remove the TMP_DIR (depends on CLEAN_UP)
if (CLEAN_UP) {
$commands['cleanup'] = sprintf(
'rm -rf %s'
, TMP_DIR
);
}
// =======================================[ Run the command steps ]===
$output = '';
foreach ($commands as $command) {
set_time_limit(TIME_LIMIT); // Reset the time limit for each command
if (file_exists(TMP_DIR) && is_dir(TMP_DIR)) {
chdir(TMP_DIR); // Ensure that we're in the right directory
}
$tmp = array();
exec($command.' 2>&1', $tmp, $return_code); // Execute the command
// Output the result
printf('
$ %s
%s
'
, htmlentities(trim($command))
, htmlentities(trim(implode("\n", $tmp)))
);
$output .= ob_get_contents();
ob_flush(); // Try to output everything as it happens
// Error handling and cleanup
if ($return_code !== 0) {
printf('
Error encountered!
Stopping the script to prevent possible data loss.
CHECK THE DATA IN YOUR TARGET DIR!
'
);
if (CLEAN_UP) {
$tmp = shell_exec($commands['cleanup']);
printf('
Cleaning up temporary files ...
$ %s
%s
'
, htmlentities(trim($commands['cleanup']))
, htmlentities(trim($tmp))
);
}
$error = sprintf(
'Deployment error on %s using %s!'
, $_SERVER['HTTP_HOST']
, __FILE__
);
error_log($error);
if (EMAIL_ON_ERROR) {
$output .= ob_get_contents();
$headers = array();
$headers[] = sprintf('From: Simple PHP Git deploy script
', $_SERVER['HTTP_HOST']);
$headers[] = sprintf('X-Mailer: PHP/%s', phpversion());
mail(EMAIL_ON_ERROR, $error, strip_tags(trim($output)), implode("\r\n", $headers));
}
break;
}
}
?>
Done.
Copy after login