Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple array formats in config in addition to serialize(array()) #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions deploy-config.orig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@
* PHP script for automatic code deployment directly from Github or Bitbucket to your server using webhooks
* Documentation: https://github.com/Lyquix/php-git-deploy
*/

/*
* For array options, you can use any of the following formats:
* define('OPTION', ['value1', 'value2', 'value3']); // works only in PHP 7.0+
* define('OPTION', 'value1, value2, value3');
* define('OPTION', serialize(array('value1', 'value2', 'value3'))); // for older PHP versions that disallow to use arrays as const value
* define('OPTION', ''); // for empty arrays
*
*/

/* DISABLED: Set to true to prevent the execution of this script. cript only when needed */
/* DISABLED: Set to true to prevent the execution of this script.
* Useful to control activation of this script only when needed.
*
*/
define('DISABLED', false);

/* IP_ALLOW:
Expand All @@ -17,8 +29,7 @@
* (https://confluence.atlassian.com/bitbucket/what-are-the-bitbucket-cloud-ip-addresses-i-should-use-to-configure-my-corporate-firewall-343343385.html)
*
*/
define('IP_ALLOW', serialize(array(
)));
define('IP_ALLOW', '');

/*
* REMOTE_REPOSITORY:
Expand All @@ -35,11 +46,7 @@
* Array of branch names allowed to deploy
* First name in array is considered the default branch and only one allowed for automatic deployments
*/
define('BRANCH', serialize(array(
'branch',
'mybranch',
'yourbranch'
)));
define('BRANCH', 'branch, mybranch, yourbranch');

/*
* ACCESS_TOKEN:
Expand Down Expand Up @@ -69,10 +76,10 @@
* Array of files excluded from rsync (they will appear in GIT_DIR, but not in TARGET_DIR)
* By default, only .git directory is excluded.
* It's recommended to leave '.git' excluded and add something more if needed.
* Example: define('EXCLUDE_FILES', serialize(array('.git', '.gitignore', '*.less', '*.scss')));
* Example: define('EXCLUDE_FILES', ['.git', '.gitignore', '*.less', '*.scss']);
*
*/
define('EXCLUDE_FILES', serialize(array('.git')));
define('EXCLUDE_FILES', '.git');

/* RSYNC_FLAGS:
* Custom flags to run rsync with
Expand All @@ -96,17 +103,17 @@
* Run commands before running rsync. Default: empty array
* This commands will be run under GIT_DIR after checkout from remote repository
* Useful for running build tasks
* Example: define('COMMANDS_BEFORE_RSYNC', serialize(array('composer install')));
* Example: define('COMMANDS_BEFORE_RSYNC', ['composer install']);
*/
define('COMMANDS_BEFORE_RSYNC', serialize(array()));
define('COMMANDS_BEFORE_RSYNC', '');

/* COMMANDS_AFTER_RSYNC:
* Run commands after running rsync. Default: empty array
* This commands will be run under TARGET_DIR after copying files from GIT_DIR
* Useful for doing some cleanups
* Example: define('COMMANDS_AFTER_RSYNC', serialize(array('rm cache/*.php -f')));
* Example: define('COMMANDS_AFTER_RSYNC', ['rm cache/*.php -f']);
*/
define('COMMANDS_AFTER_RSYNC', serialize(array()));
define('COMMANDS_AFTER_RSYNC', '');

/* CLEANUP_WORK_TREE:
* Clean GIT_DIR from leftovers after custom commands
Expand All @@ -125,8 +132,7 @@
* - Class names have to match their file-name (case-sensitive), e.g. "Discord.class.php" has class "Discord"
* - The array keys contain only the class name, e.g. array("Discord")
*/
define('CALLBACK_CLASSES', array(
));
define('CALLBACK_CLASSES', '');

/* PLUGINS_FOLDER:
* Folder containing all webhook plugins/classes, default: 'plugins/'
Expand Down
57 changes: 42 additions & 15 deletions deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ function endScript($msg = "",$display = false) {
if(defined('EMAIL_NOTIFICATIONS') && EMAIL_NOTIFICATIONS !== '') error_log($output, 1, EMAIL_NOTIFICATIONS);

// Send error callback
if(!empty($msg) && defined('CALLBACK_CLASSES') && !empty(CALLBACK_CLASSES)){
foreach (CALLBACK_CLASSES as $class) {
if(!empty($msg) && defined('CALLBACK_CLASSES') && !empty(getArray(CALLBACK_CLASSES))){
foreach (getArray(CALLBACK_CLASSES) as $class) {
if(is_callable(array($class,"errorWebhook"))){
$callback = $class::errorWebhook($msg);
// prevent outputting after errorPage()
Expand All @@ -92,6 +92,33 @@ function endScript($msg = "",$display = false) {
}
}

// Return new array from string (possible formats: serialized, json, comma-separated or just single item) or array itself
// To use with config entries
function getArray($x = '') {
// For empty value (false, null, '', etc) - return empty array
if (empty($x)) {
return array();
}
// For array - return array itself
if (is_array($x)) {
return $x;
}
if (is_string($x)) {
// Serialized array (a:<len>:{<elements>})
if (preg_match('/^a:\d+:\{.*\}$/', $x)) {
return unserialize($x);
}
// Json array ([<elements>])
if (preg_match('/^\[.*\]$/', $x)) {
return json_decode($x, true);
}
// Comma-separated list or single element
return array_map('trim', explode(',', $x));
}
// For anything else - push it into single-element array
return array($x);
}

/* Begin Script Execution */

// Prevent caching
Expand Down Expand Up @@ -120,7 +147,7 @@ function endScript($msg = "",$display = false) {
if (!defined('GIT_DIR') || GIT_DIR === '') $err[] = 'Git directory is not configured';
if (!defined('TARGET_DIR') || TARGET_DIR === '') $err[] = 'Target directory is not configured';
if (!defined('TIME_LIMIT')) define('TIME_LIMIT', 60);
if (!defined('EXCLUDE_FILES')) define('EXCLUDE_FILES', serialize(array('.git')));
if (!defined('EXCLUDE_FILES')) define('EXCLUDE_FILES', '.git');
if (!defined('RSYNC_FLAGS')) define('RSYNC_FLAGS', '-rltgoDzvO');

// If there is a configuration error
Expand All @@ -141,9 +168,9 @@ function endScript($msg = "",$display = false) {
fclose($fh);

// Check if IP is allowed
if(defined('IP_ALLOW') && count(unserialize(IP_ALLOW))) {
if(defined('IP_ALLOW') && count(getArray(IP_ALLOW))) {
$allow = false;
foreach(unserialize(IP_ALLOW) as $ip_allow) {
foreach(getArray(IP_ALLOW) as $ip_allow) {
if(strpos($ip_allow, '/') === false) {
// Single IP
if(inet_pton($_SERVER['REMOTE_ADDR']) == inet_pton($ip_allow)) {
Expand Down Expand Up @@ -265,7 +292,7 @@ function getallheaders() {
// Branch from webhook?
if($branch) {
// Only main branch is allowed for webhook deployments
if($branch != unserialize(BRANCH)[0]) {
if($branch != getArray(BRANCH)[0]) {
$msg = 'Branch ' . $branch . ' not allowed, stopping execution.';
echo "\n" . $msg . "\n</pre></body></html>";
endScript($msg);
Expand All @@ -276,13 +303,13 @@ function getallheaders() {
if(isset($_GET['b'])) {
$branch = $_GET['b'];
// Check if branch is allowed
if(!in_array($branch, unserialize(BRANCH))) {
if(!in_array($branch, getArray(BRANCH))) {
$msg = 'Branch ' . $branch . ' not allowed, stopping execution.';
echo "\n" . $msg . "\n</pre></body></html>";
endScript($msg);
}
} else {
$branch = unserialize(BRANCH)[0];
$branch = getArray(BRANCH)[0];
echo "No branch specified, assuming default branch $branch\n";
}
}
Expand Down Expand Up @@ -473,15 +500,15 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
echo "\nNOTE: repository files that have been modfied or removed in target directory will be resynced with repository even if not listed in commits\n";

// Run before rsync commands
if(defined('COMMANDS_BEFORE_RSYNC') && count(unserialize(COMMANDS_BEFORE_RSYNC))) {
if(defined('COMMANDS_BEFORE_RSYNC') && count(getArray(COMMANDS_BEFORE_RSYNC))) {
echo "\nRunning before rsync commands\n";
foreach(unserialize(COMMANDS_BEFORE_RSYNC) as $command) {
foreach(getArray(COMMANDS_BEFORE_RSYNC) as $command) {
cmd($command);
}
}

// Build exclusion list
$exclude = unserialize(EXCLUDE_FILES);
$exclude = getArray(EXCLUDE_FILES);
array_unshift($exclude, '');

// rsync all added and modified files (by default: no deletes, exclude .git directory)
Expand All @@ -498,9 +525,9 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
foreach($deleted as $file) unlink($file);

// Run after rsync commands
if(defined('COMMANDS_AFTER_RSYNC') && count(unserialize(COMMANDS_AFTER_RSYNC))) {
if(defined('COMMANDS_AFTER_RSYNC') && count(getArray(COMMANDS_AFTER_RSYNC))) {
echo "\nRunning after rsync commands\n";
foreach(unserialize(COMMANDS_AFTER_RSYNC) as $command) {
foreach(getArray(COMMANDS_AFTER_RSYNC) as $command) {
cmd($command, true, TARGET_DIR);
}
}
Expand All @@ -524,8 +551,8 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
));

// Send success callback
if(defined('CALLBACK_CLASSES') && !empty(CALLBACK_CLASSES)){
foreach (CALLBACK_CLASSES as $class) {
if(defined('CALLBACK_CLASSES') && !empty(getArray(CALLBACK_CLASSES))){
foreach (getArray(CALLBACK_CLASSES) as $class) {
if(is_callable(array($class,"successWebhook"))){
$callback = $class::successWebhook(array(
'remote' => REMOTE_REPOSITORY,
Expand Down