Skip to content

Instantly share code, notes, and snippets.

@dprevite
Last active December 15, 2015 16:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dprevite/5286585 to your computer and use it in GitHub Desktop.
Save dprevite/5286585 to your computer and use it in GitHub Desktop.
Pulls all feature branches to their own folders so they can be served by the web server for easy QA. These are run via cron. *.branches.projectdomain.com
#!/usr/bin/php
<?php
// Constants we need to do things
define('WEB_ROOT', '/var/www/branches.projectdomain.com/'); // Requires trailing slash!
define('GIT', '/usr/bin/git');
define('GIT_REPO', WEB_ROOT . '__base__/'); // Requires trailing slash!
define('GIT_DIR', GIT_REPO . '.git');
define('GIT_CMD', GIT . ' --git-dir=' . GIT_DIR . ' ');
// Prune dead branches
$command = GIT_CMD . 'remote prune origin';
$output = `$command`;
// Update our local repo
$command = GIT_CMD . 'fetch';
$output = `$command`;
// Get all of the branches from one of our local repos
$command = GIT_CMD . 'branch -a';
$output = `$command`;
$branches = explode("\n", $output);
// Loop through all of the branches
foreach ($branches as $branch) {
// We are only looking up remote branches
if (substr(trim($branch), 0, 15) == 'remotes/origin/') {
$branch_name = str_replace('remotes/origin/', '', trim($branch));
// We are only checking out feature branches
if (substr($branch_name, 0, 8) == 'feature/') {
$feature_name = str_replace('feature/', '', $branch_name);
if (!is_dir(WEB_ROOT . $feature_name)) {
$command = 'cp -R ' . GIT_REPO . ' ' . WEB_ROOT . $feature_name;
$output = `$command`;
$command = GIT . ' --git-dir=' . WEB_ROOT . $feature_name . '/.git --work-tree=' . WEB_ROOT . $feature_name . '/ checkout -b feature/' . $feature_name . ' origin/feature/' . $feature_name;
echo $command . PHP_EOL;
$output = `$command`;
}
}
}
}
$command = 'chown -R www-data:www-data ' . WEB_ROOT;
`$command`;
echo PHP_EOL . PHP_EOL;
echo `php /var/www/branches.projectdomain.com/update_all_branches.php`;
*/20 14-0 * * * 1,2,3,4,5 /var/www/branches.projectname.com/checkout_all_branches_to_dirs.php
server {
listen 80;
server_name _;
root /var/www;
index index.html;
}
server {
listen 80;
server_name ~^(?<domain>.+)(?<sep>\.+)branches\.projectdomain\.com$;
server_name branches.projectdomain.com;
set $dir_sep "";
if ($sep ~ "^\.$") {
set $dir_sep "/";
}
root /var/www/branches.projectdomain.com$dir_sep$domain;
index index.php;
location / {
auth_basic "Restricted";
auth_basic_user_file /var/www/branches.projectdomain.com/.htpasswd;
index index.php;
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
}
location /media/ {
alias /var/www/media/;
autoindex on;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
location ~/\.ssh* {
deny all;
}
}
#!/usr/bin/php
<?php
// Constants we need to do things
define('WEB_ROOT', '/var/www/branches.projectdomain.com/'); // Requires trailing slash!
define('GIT', '/usr/bin/git');
define('GIT_REPO', WEB_ROOT . '__base__/'); // Requires trailing slash!
define('GIT_DIR', GIT_REPO . '.git');
define('GIT_CMD', GIT . ' --git-dir=' . GIT_DIR . ' ');
$root = new DirectoryIterator(WEB_ROOT);
foreach ($root as $directory) {
if (!$directory->isDot() && $directory->isDir()) {
if ($directory->getFilename() != '__base__') {
$command = GIT . ' --git-dir=' . WEB_ROOT . $directory->getFilename() . '/.git --work-tree=' . WEB_ROOT . $directory->getFilename() . '/ pull';
$output = `$command`;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment