Created
April 28, 2020 03:11
-
-
Save mcnamee/d07145548a864a18ab786f66675efc66 to your computer and use it in GitHub Desktop.
Deploy to Staging
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 | |
/** ********************************* | |
* GIT Deploy to Pivotal Staging | |
* | |
* 1. Ensure this file exists in the root directory of your website | |
* on the staging server | |
* 2. Update the $config variables below | |
* 3. Browse to this file in your browser to "clone" the repo | |
* 4. Setup a Webhook to auto-deploy: | |
* URL: http://nbm:nbm@{{site_domain_name}}/stage.php | |
* Status: Active | |
* Triggers: Repo push | |
* | |
* Everytime you push to the branch specified below, it'll | |
* auto deploy to the Pivotal Staging server. | |
* | |
* ********************************* */ | |
putenv('HOME=/var/www'); | |
putenv('NVM_DIR=' . getenv('HOME') . '/.nvm'); | |
$config = array(); | |
// GIT Repo URL eg. git@bitbucket.org:pvtl/go-health-clubs.git | |
$config['repo_url'] = 'git_repo_url'; | |
// GIT Remote you're using eg. origin | |
$config['remote'] = 'origin'; | |
// Git Branch you'd like to deploy from eg. master | |
$config['branch'] = 'develop'; | |
// Posthook commands | |
$config['posthooks'] = array( | |
// # WORDPRESS (with Bedrock) ====== | |
'composer install --ignore-platform-reqs', | |
// '( cd web/app/themes/pvtl ; npm install )', | |
// '( cd web/app/themes/pvtl ; npm run build )', | |
// # WORDPRESS ====== | |
// 'git submodule update --init --remote --recursive', | |
// '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && npm install', | |
// '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && npm run compile-sass', | |
// # SITEHQ ====== | |
# Clear Cache | |
// 'rm -rf cache/page_blocks', | |
// 'rm -rf cache/img_resize' | |
// ====== | |
// # MAGENTO 2 ====== | |
// 'composer update', | |
// 'php bin/magento setup:upgrade', | |
// 'php bin/magento setup:di:compile', | |
// 'php bin/magento setup:static-content:deploy', | |
// ====== | |
); | |
// ============================================== | |
// If the Envionment is set, meaning this is currently the PVTL staging server | |
if (!empty($_SERVER['APPLICATION_ENV']) && $_SERVER['APPLICATION_ENV'] == 1) : | |
// Error checking | |
if (empty($config['repo_url'])) die('Please provide the \'Path to Repo\' (i.e. the URL to the Git repo)'); | |
if (empty($config['remote'])) die('Please provide the \'Remote\' to use (eg. origin)'); | |
if (empty($config['branch'])) die('Please provide the \'Branch\' to use (eg. master)'); | |
// Step 1. Setup ======================================== | |
if (!file_exists('.git')) : | |
// We can't 'git clone' because the dir isn't empty (this file exists in it) | |
$clone_commands = array( | |
"git init", | |
"git remote add origin {$config['repo_url']}", | |
"git fetch", | |
"git checkout -t {$config['remote']}/{$config['branch']}", | |
); | |
respondAndContinue('Beginning setup of stage'); | |
foreach ($clone_commands as $str_command) { | |
$output = shell_exec("{$str_command}"); | |
if (!empty($output)) { | |
echo "# {$output} (#$str_command)<br />"; | |
} | |
} | |
$this_url = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; | |
echo "<br /><strong><em>Cool cool cool. We just cloned the repo.</em></strong> <br /><br />"; | |
echo "Next step, is to setup a Webhook in Bitbucket:<br />"; | |
echo " - <strong>URL:</strong> http://nbm:nbm@{$this_url}<br />"; | |
echo " - <strong>Status:</strong> Active<br />"; | |
echo " - <strong>Triggers:</strong> Repo push<br />"; | |
// Step 2. Handle Deploy =============================== | |
else : | |
// Standard Commands | |
$arr_commands = array( | |
"git fetch {$config['remote']} {$config['branch']}", | |
"git reset --hard FETCH_HEAD", | |
); | |
respondAndContinue('Beginning deploy of branch'); | |
// Posthooks | |
if (!empty($config['posthooks'])) { | |
foreach ((array)$config['posthooks'] as $posthook) { | |
$arr_commands[] = "{$posthook}"; | |
} | |
} | |
foreach ($arr_commands as $str_command) { | |
$output = shell_exec("{$str_command}"); | |
echo "# ===> {$str_command} <br />"; | |
if (!empty($output)) { | |
echo "# {$output} <br /><br />"; | |
} | |
} | |
endif; | |
else : | |
// Otherwise fail | |
header('HTTP/1.0 403 Forbidden'); | |
echo 'Permission denied.'; | |
endif; | |
// Immediately send a HTTP response and close the connection | |
// Allow this script to continue running in the background | |
function respondAndContinue($msg) | |
{ | |
// Pad with spaces to force output buffers to flush | |
$msg .= str_repeat(' ', 4096); | |
// Buffer all upcoming output | |
ob_start(); | |
// Send response | |
echo $msg; | |
// Disable compression (in case content length is compressed) | |
header('Content-Encoding: none'); | |
// Set the content length of the response | |
header('Content-Length: ' . ob_get_length()); | |
// Close the connection | |
header('Connection: close'); | |
// Flush all output | |
ob_end_flush(); | |
ob_flush(); | |
flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment